How to Read Multiple Integer Values from a Single Line of Input in Java

How to read 3 Integer values in single line of input in Java?

Here is small example for you

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

String[] input = new String[3];
int x;
int y;
int z;

System.out.print("Please enter Three integers: ");
input = in.readLine().split(" ");

x = Integer.parseInt(input[0]);
y = Integer.parseInt(input[1]);
z = Integer.parseInt(input[2]);

System.out.println("You input: " + x + ", " + y + " and " + z);
}

How to read multiple inputs on same line in Java?

Start with assigning the first scanned integer to largest and secondLargest variables and then process the remaining nine integers in a loop as follows:

num = sc.nextInt();
if (num > largest) {
secondLargest = largest;
largest = num;
}

Demo:

import java.util.Scanner;

public class SecondLargest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter 10 integers separated by single spaces: ");

// Scan the first integer and assign it to largest and secondLargest
int num = sc.nextInt();
int largest = num;
int secondLargest = largest;

// Input 9 more integers
for (int i = 1; i < 10; i++) {
num = sc.nextInt();
if (num > largest) {
secondLargest = largest;
largest = num;
}
}

System.out.println("The second largest number is: " + secondLargest);
}
}

A sample run:

Enter 10 integers separated by single spaces: 10 23 4 12 80 45 78 90 105 7
The second largest number is: 90

Note: This is just a sample program assuming that the input is in the correct format. I leave it to you to work on how to deal with the wrong input. It will be a good exercise for you.

How to read multiple integer values from one line in Java using BufferedReader object?

Try the next:

int a[] = new int[n];
String line = br.readLine(); // to read multiple integers line
String[] strs = line.trim().split("\\s+");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(strs[i]);
}

How to read multiple integers from single line java

A small example which splits after a blank, so a example input could be:

---> 3 4 9 10

String input = scanner.nextLine();
String integers[] = input.split(" ");
if(integers.length > 13 || integers.length < 1){
//ErrorHandling
}
for(String number : integers){
try {
int num = Integer.parseInt(number);
//Add to array
} catch(NumberFormatException e){
//number String input was not a number
}
}

Reading multiple integers in multiple lines

I think that your problem comes from that :

while (in.hasNextLine()){
for (int i =1; i<= numberEdges; i++)
{

First, iteration is redundant (while or for are unitary enough for reading each line. You have to do choice between them).

Besides if your file has less line than numberEdges, a java.util.NoSuchElementException will be raised.

If the number of line is constant in the file, use a for:

for (int i =1; i<= numberEdges; i++)

remove the enclosing while (in.hasNextLine()). It is not required. Iteration control is already done by the for.

If the number of lines in the file may vary, use only a while :

    while (in.hasNextLine()){

But anyway, don't use both.

read multiple variables with scanner in one line

One way to do this is to use nextInt instead of nextLine:

int hight = reader.nextInt();
int width = reader.nextInt();
int depth = reader.nextInt();

Your input can then be like this:

1 2 3

Note that you have to enter all three numbers before pressing ENTER, or else your input will look like this again:

1
2
3

Another way to do this is to use a regex to specify the exact format that you want your input to be in, call nextLine, and match the line that the user entered to the regex pattern:

Matcher matcher = Pattern.compile("(\\d+)\\s+(\\d+)\\s+(\\d+)").matcher(reader.nextLine());
if (matcher.matches()) {
int height = Integer.parseInt(matcher.group(1));
int width = Integer.parseInt(matcher.group(2));
int depth = Integer.parseInt(matcher.group(3));
} else {
// input was invalid!
}


Related Topics



Leave a reply



Submit