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.

M. Abbas
  • 6,409
  • 4
  • 33
  • 46
user2709885
  • 413
  • 2
  • 8
  • 16

6 Answers6

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
Rahul
  • 44,383
  • 11
  • 84
  • 103
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

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
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]);
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"));
        }
    }

}
AppX
  • 528
  • 2
  • 12
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] + " ");
        }
Matej
  • 7,728
  • 4
  • 23
  • 30
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.

resueman
  • 10,572
  • 10
  • 31
  • 45