Reputation: 4050
import java.util.Scanner;
public class Question3 {
public static void main(String[] args) {
String input;
int i1,i2,i3;
Scanner keyboard = new Scanner(System.in);
System.out.println("Input a 3 digit number ");
input = keyboard.next();
String[] numbers = input.split("\\s+");
i1= Integer.parseInt(numbers[0]);
i2= Integer.parseInt(numbers[1]);
i3= Integer.parseInt(numbers[2]);
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
}
}
Here is what it should do: somebody types 1 2 3 and the program should output
1
2
3 however, it throws an arrayindexoutofbounds execpetion at the line i2= ....
I need them to be INTs btw because i need to do stuff with them afterwards... How can i fix this? (The question for my class is...)
Write a program that uses a Scanner to read three integers (positive) displays the biggest number of three. (Please complete without using either of the operators && or ||. These operators will be covered in class shortly. Similarly loops are not required.)
Some sample run:
Please input 3 integers: 5 8 3
The max of three is: 8
Upvotes: 0
Views: 2679
Reputation: 1
import java.util.Scanner;
class Question3 {
public static void main(String[] args) {
String input;
int i1,i2,i3;
Scanner keyboard = new Scanner(System.in);
System.out.println("Input a 3 digit number ");
input = keyboard.next();
int i=0;
for (String result: input.split("\\s+")){
System.out.println(result.charAt(i++));
System.out.println(result.charAt(i++));
System.out.println(result.charAt(i++));
}
}
}
Your original code contains the following error: numbers[0]
contains all the elements of the String without spaces, and therefore throws an error when trying to access numbers[1]
. In this code, the for
loop is executed only once, and the String without spaces is stored in String reference result, so displaying the digits using result.charAt()
gives the correct result.
Upvotes: 0
Reputation: 85779
Use keyboard.nextInt()
to read an int value. If you need to read 3 values, use it 3 times.
Documentation link:
Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.
If you don't know if the next data could be an integer, you can use Scanner#hasNextInt
method.
Upvotes: -1
Reputation: 301
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int i1,i2,i3;
Scanner keyboard = new Scanner(System.in);
System.out.println("Input a 3 digit number ");
i1= Integer.parseInt(keyboard.next());
i2= Integer.parseInt(keyboard.next());
i3= Integer.parseInt(keyboard.next());
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
}
}
Upvotes: 2
Reputation: 51030
Instead of input = keyboard.next();
try input = keyboard.nextLine();
According to Scanner
docs the default delimiter is white space -
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
And the method next()
-
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.
So in our case keyboard.next()
only returns the first number as string.
Upvotes: 2