1

I'm working on a java assignment and was stuck on this part. Basically we are to get the user to input three positive non-zero integers using the scanner.

It's supposed to look something like this

Enter three integer values: 2 2 10

The numbers (2, 2, 10) cannot form a triangle.

I was wondering how can I code it so that entering the "2 2 10" could be read as three different integers that are separated by a comma. Thank you very much in advance.

rcv40
  • 37
  • 2
  • 8

4 Answers4

1

Read the input with java.util.Scanner and a for loop:

Scanner sc = new Scanner(System.in);
int[] values = new int[3];

for (int i = 0; sc.hasNextInt() && i < 3; i++) {
    values[i] = sc.nextInt();
}
pp_
  • 3,435
  • 4
  • 19
  • 27
0

Try this:

Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
String[] inValues = line.split(" ");
int[] values = new int[inValues.length];
for(int i = 0; i < inValues.length; i++){
    values[i] = Integer.parseInt(inValues[i]);
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 2
    or just `int[] values = Arrays.stream(scan.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();` – Bohemian Feb 15 '16 at 01:04
0

You can make it as follows:

String sentence = scanner.nextLine();

And you can make:

String[] splittedByComma = sentence.split(","); 

Or by space:

String[] splittedBySpace = sentence.split(" ");

Or an array list like the following example:

How to split a comma-separated string?

Then parse each of them to integers.

Community
  • 1
  • 1
i3rd
  • 66
  • 8
0

scanner.nextInt is your answer

final BufferedReader input = new BufferedReader(
    new InputStreamReader(System.in));

// enter yours 2 2 10 and press Enter
final String line = input.readLine();

final Scanner s = new Scanner(line);
int a = s.nextInt(); // reads 2
int b = s.nextInt(); // reads 2
int c = s.nextInt(); // reads 10

if (a + b > c || a + c > b || b + c > a ) {
    System.out.println(
        "No triangle can be composed from " + a + ", " + b + ", " + c );
}
ya_pulser
  • 2,620
  • 2
  • 16
  • 21
  • 1
    Would be nice if you could explain where the commas went! – Tacocat Feb 15 '16 at 01:11
  • Why the `BufferedReader`? – pp_ Feb 15 '16 at 01:31
  • I agree this is *an* answer; but not *the* answer. Don't you take for granted the input is pure. What happens when I input `1 Two 3` or `123`? – ChiefTwoPencils Feb 15 '16 at 01:32
  • @Tacocat ... topic starter provided example "2 2 10". If you want to support commas, then alter input.readLine() to input.readLine().replaceAll(",", " "); – ya_pulser Feb 15 '16 at 01:36
  • @ChiefTwoPencils, I as far as I understand java.util.InputMismatchException will happen. – ya_pulser Feb 15 '16 at 01:38
  • @pp_ "buffered reader - single String - scanner over the String" provides opportunity to have all input in one piece to clear up it if needed (like convert commas to spaces) or perform validation (like Two) mentioned earlier and show error message with full unmodified input from user. – ya_pulser Feb 15 '16 at 01:40
  • @ya_pulser "I was wondering how can I code it so that entering the "2 2 10" could be read as three different integers that are separated by a comma." --- I think we have a different interpretation of what this means. To be honest, I am a little confused as to what the asker seeks since he has edited his post a couple of times -_-. – Tacocat Feb 15 '16 at 01:42