1

Hi I am new to Java and I was experimenting with the Scanner class. I am trying to figure out a small problem in which I want to enter two inputs such as: 4 5 6 and 8 9 0. I want to store 4,5,6 in one array and 8,9,0 in another array and then print these arrays. But I am unable to do so. I wrote the following code :

public class scanner {

public static void main(String[] args) {

    int[] array = new int[3];
    int[] array2 = new int[3];
    Scanner scan = new Scanner(System.in);

    int i = 0;
    while(scan.hasNextInt()){
        array[i] = scan.nextInt();
        i++;
        if(i == 3){
            break;
        }
    }

    i = 0;
    while(scan.hasNextInt()){
        array2[i] = scan.nextInt();
        i++;
        if(i == 3){
            break;
        }
    }

    for(int j  = 0; j < array.length; j++){
        System.out.println(array[j]);
    }

    for(int j  = 0; j < array2.length; j++){
        System.out.println(array2[j]);
    }
}

}

But this doesn't takes the input 4 5 6 in one single line. I want to enter 4 5 6 in one line so that all the three digits are stored in the array. Can someone please help me. I assume I should use delimiter to remove the white space but I am not sure how to go about it.

9
  • 1
    You code works just fine for me. ideone.com/JRfM2J Commented Sep 19, 2013 at 7:24
  • You code works just fine! How do you run your program? Commented Sep 19, 2013 at 7:30
  • @sam_codes - The code works fine, but the OP wanted to read the input differently. That was the question! Commented Sep 19, 2013 at 7:32
  • I don't understand what doubt could you possibly have in running a java program. Commented Sep 19, 2013 at 7:33
  • @R.J He said that he wants to store them in different arrays and print them and he is unable to do so. I am saying his code does the thing he wants. Commented Sep 19, 2013 at 7:35

6 Answers 6

2

You can try something like this, instead of the 2 while loops you've to populate your arrays.

Here the scanner reads line by line and each line is split on space (as you mentioned in your question) and then each splitted element is converted to an integer and the array is populated.

String line1 = scan.nextLine(); // Read 1st line
String[] numbers1 = line1.split(" "); // Split based on space
for(int i=0;i<numbers1.length;i++){
    array[i] = Integer.parseInt(numbers1[i]);
}

String line2 = scan.nextLine(); // Read 2nd line
String[] numbers2 = line2.split(" "); // Split based on space
for(int i=0;i<numbers2.length;i++){
    array2[i] = Integer.parseInt(numbers2[i]);
}

Sample I/O:-

Input:
1 2 3
4 5 6

Output:
1
2
3
4
5
6
Sign up to request clarification or add additional context in comments.

1 Comment

If that's what you're looking for you should accept the answer.
1

To do this you have to get the String as a hole 4 5 6 and use split(" ") to get an array:

String val="4 5 6";
String [] arr = val.split(" ");

Then go on and loop your arry as you do now

Comments

1
int[] array = new int[3];
int[] array2 = new int[3];
Scanner scan = new Scanner(System.in);

for(int i = 0 ; i < array.length ; i++){
    array[i] = scan.nextInt();
}

for(int i = 0 ; i < array2.length ; i++){
    array2[i] = scan.nextInt();
}

for(int j  = 0; j < array.length; j++){
    System.out.println(array[j]);
}

for(int j  = 0; j < array2.length; j++){
    System.out.println(array2[j]);

Comments

0

Is this what you are looking for?

public class InputScanner {

    public static void main(String[] args) {

        int[] array = new int[3];
        int[] array2 = new int[3];
        Scanner scan = new Scanner(System.in);

        int i = 0;
        while (scan.hasNextInt()) {
            array[i] = scan.nextInt();
            i++;
            if (i == 3) {
                break;
            }
        }

        i = 0;
        while (scan.hasNextInt()) {
            array2[i] = scan.nextInt();
            i++;
            if (i == 3) {
                break;
            }
        }

        for (int j = 0; j < array.length; j++) {
            System.out.print("" + array[j] + (j<(array.length-1)?" ":"\n"));
        }

        for (int j = 0; j < array2.length; j++) {
            System.out.print("" + array2[j] + (j<(array2.length-1)?" ":"\n"));
        }
    }

}

Comments

0

Your code is ok. The default delimiter for Scanner is whitespace according to javadoc.

The input is broken into tokens by the delimiter pattern, which is whitespace by default

What probably confuse you is that the print put each integer in a new line. To fix that you can simply write this:

        for(int j  = 0; j < array.length; j++){
            System.out.print(array[j] + " ");
        }
        System.out.println();
        for(int j  = 0; j < array2.length; j++){
            System.out.print(array2[j] + " ");
        }

Comments

0
import java.util.*;
class Ab
{
    public static void main(String[] args)
    {
        Scanner s=new Scanner(System.in);
        String str=s.nextLine();
        String [] arr=str.split(" ");
        Integer [] a=new Integer[arr.length];
        for(int i=0;i<arr.length;i++)
        {
            a[i]=Integer.parseInt(arr[i]);
        }
        for(int i=0;i<a.length;i++)
        {
            System.out.println(a[i]);
        }
    }
}

You may try this out and can make changes accordingly.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.