Answers for "space separated int string in java"

0

how to read space separated integers in java

# if you read from console

	Scanner scanner = new Scanner(System.in);
	List<Integer> list = new ArrayList<Integer>();
	while (scanner.hasNextInt())
  		list.add(scanner.nextInt());
	int[] arr = list.toArray(new int[0]);

# if you read from file

	try {
      	File myObj = new File("filename.txt");
      	Scanner myReader = new Scanner(myObj);
      	while (myReader.hasNextInt()) { // check for next integer in file
        	String data = myReader.nextInt(); // read the integer
        	System.out.println(data);
      	}
      	myReader.close();
    } catch (FileNotFoundException e) {
      	System.out.println("An error occurred.");
      	e.printStackTrace();
    }
Posted by: Guest on May-26-2021
0

how to convert string to space separated int in java

resetting temp to empty string after you parse it to create place for new digits.
that at the end of your string will be no space, so if (numbers.charAt(i) == ' ') { ary[j] = Integer.parseInt(temp); j++; } will not be invoked, which means you need invoke ary[j] = Integer.parseInt(temp);
Posted by: Guest on August-10-2021

Code answers related to "space separated int string in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language