1

I am new to java and I am trying to write the code as followed: user inputs numbers one by one and the code needs to print right away each number and continue to receive the next number on the same row

this is my code so far:

        Scanner user = new Scanner(System.in);
        String value = user.nextLine();
        String matrix = "";
        int point = 0;
        int pos = 0;
        while(pos <= 5)
        {
            if(isInteger(value))
            {
                System.out.print(matrix.substring(point) + "\t");
                matrix = matrix + value+",";
                point+=2;
                pos++;
            }
            else
            {
                System.out.print("Not a number");
            }
            value = user.next();
        }

but every time I input another number to the scanner when the program is running, it goes down to the next row. so after I type 1 , 2 , 3 , 4 , 5 the output is:

1
     2
     3
     4
     5

I want to make it like this:

1     2     3     4     5

is there a way to make the scanner read another number and still remain on the same line?

Davis8988
  • 258
  • 3
  • 14
  • Maybe [Read every key pressed on console at a time](http://stackoverflow.com/questions/21069857/java-read-every-key-pressed-on-console-at-a-time) will give you some hints. – ifloop Apr 08 '14 at 13:18

3 Answers3

0

You have to use nextInt() instead of nextLine() as specified here.

Quillion
  • 5,757
  • 10
  • 55
  • 83
  • That will not consume the line break, but it is still there as the user confirms his input with IMO – ifloop Apr 08 '14 at 13:09
  • @ifLoop nextInt returns int, so yes at first he will have to enter `1 2 3 4 5(enter character)`, but his program will print afterwards `1 2 3 4 5` all in one line without newline character at the end. – Quillion Apr 08 '14 at 13:15
  • _but every time I input another number to the scanner when the program is running, it goes down to the next row_ That sounds as OP wishes to read them one by one while typing, not when confirmed with enter. – ifloop Apr 08 '14 at 13:17
  • @ifLoop yes and his problem will be solved by using `nextInt()` because it will return an integer without a newline. His current solution employs reading the whole string typed in. If he switches to `nextInt()` and prints int instead of String then he will not get the newline character included. – Quillion Apr 08 '14 at 13:24
0

Try it this way:

public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Type in all your numbers and hit return");
    while (scanner.hasNext()) System.out.println(scanner.next());
}

The user inserts all numbers in one line. Afterwards you scan the input, integer per integer and print them. As separator I used space.

Harmlezz
  • 7,524
  • 23
  • 34
0

Here you can input all the numbers at one line separated by space & hit enter,the output will be at one line.Check it out..

    int num; 
    int pos = 0;
    Scanner scanner = new Scanner(System.in);
    System.out.println("Type all the integers and hit return :");

    while (pos < 5) {

         if(scanner.hasNextInt()) {

         num = scanner.nextInt();
         System.out.print(num + "\t");
         pos++;

     } else { 

             scanner.next();  
             System.out.print("Not an integer number! ");
         }
    }

Example :

input : 1 2 3 4 5

output : 1 2 3 4 5


input : 1 2 3 M 5

output : 1 2 3 Not an integer number! 5

Ashraful Haque
  • 529
  • 2
  • 10