Answers for "how to read space separated integers in java"

4

how to take space separated input in java

String s[]= scanner.nextLine().split(" ");for(int i =0 ;i < s.length;i++){    a[i]= Integer.parseInt(s[i]);}
Posted by: Guest on June-18-2020
1

how to take space separated input in java

Scanner scanner = new Scanner(System.in);
 int numOfBlocks = scanner.nextInt();
 int weightArray[] = new weightArray[numOfBlocks];
 for(int i=0;i<numOfBlocks;i++)
       {
        weightArray[i] = scanner.nextInt();
       }
 scanner.close();
//your logic
Posted by: Guest on June-18-2020
0

how to read space separated integers in java

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

class BufferedReaderTest
{
    public static void main(String[] args) throws IOException {
        BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));

        int num[] = new int[1000];
        String[] strNums;
        long startTime, endTime;


        /*________ TEST STARTS ________*/
        startTime = System.nanoTime();
        strNums = bi.readLine().split("\\s");
        for(int i=0; i<strNums.length; i++) {
            num[i] = Integer.parseInt(strNums[i]);
        }
        endTime = System.nanoTime();
        /*________ TEST ENDS ________*/

        System.out.println("Total Time Taken: " + (endTime - startTime));
    }
}
Posted by: Guest on May-26-2021
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

Code answers related to "how to read space separated integers in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language