sums a list of numbers entered by the user : Convert from String « Data Type « Java Tutorial






import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class ParseDemo {
  public static void main(String args[]) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str;
    int i;
    int sum = 0;

    System.out.println("Enter numbers, 0 to quit.");
    do {
      str = br.readLine();
      try {
        i = Integer.parseInt(str);
      } catch (NumberFormatException e) {
        System.out.println("Invalid format");
        i = 0;
      }
      sum += i;
      System.out.println("Current sum is: " + sum);
    } while (i != 0);
  }
}








2.36.Convert from String
2.36.1.Number Parsing
2.36.2.Integer.valueOf: Converting String to Integer
2.36.3.Integer.parseInt(): Converting String to int
2.36.4.String.ValueOf
2.36.5.sums a list of numbers entered by the user
2.36.6.Convert string of time to time object
2.36.7.Converting a String to a byte Number
2.36.8.Converting a String to a short Number
2.36.9.Converting a String to a int(integer) Number
2.36.10.Convert a String to Date
2.36.11.Convert String to character array
2.36.12.Convert base64 string to a byte array
2.36.13.Parse basic types