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.

4
  • Would be nice if you could show what you have tried so far. Commented Feb 15, 2016 at 0:36
  • Have you already created a Scanner? How much have you done already? Commented Feb 15, 2016 at 0:38
  • 1
    try String[] numbers = scanner.nextLine().split(" "); // or .split(",") for csv input then convert them to int using Integer.parseInt() Commented Feb 15, 2016 at 0:40
  • Try to use split() method this: String[] nmbr = "2,2,10".split("\\,"); Commented Feb 15, 2016 at 0:43

4 Answers 4

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();
}
Sign up to request clarification or add additional context in comments.

Comments

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]);
}

1 Comment

or just int[] values = Arrays.stream(scan.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
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 );
}

7 Comments

Would be nice if you could explain where the commas went!
Why the BufferedReader?
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?
@Tacocat ... topic starter provided example "2 2 10". If you want to support commas, then alter input.readLine() to input.readLine().replaceAll(",", " ");
@ChiefTwoPencils, I as far as I understand java.util.InputMismatchException will happen.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.