0

I am new to Java. I have to show to terminal int coefficients from a 2D array. I would like to have each value for the same seller in the same line. There is a line break (due to Scanner ?). I have been looking for delimiter for system.in but I do not understand how to use it and if that is appropriate.

Please, may you help me ? Thank you in advance

import java.util.Scanner;

public class Ventes {
    public static void main(String[] args) {
        Scanner myObj = new Scanner(System.in);
        System.out.print("Enter the number of sellers ");
        int nbrSellers = myObj.nextInt();
        System.out.print("Enter the number of models ");
        int nbrModels = myObj.nextInt();

        int[][] sales = new int[nbrSellers][nbrModels];

        for(int i = 1; i <= nbrSellers; i++) {
            System.out.print("Seller " + i + " ");
            for(int j = 0; j < nbrModels; j++) {
                sales[i][j] = myObj.nextInt();
            }
            System.out.println();
        }
    }
}

Terminal result :

Enter the number of sellers 5

Enter the number of models 4

Seller 1 0

3

2

0

Final result in terminal terminal

  • 1
    You mean you wanna import the number of seller and the number of models from the same line using scanner? – Mehran B Dec 16 '20 at 18:12
  • 1
    The line break is because you're pressing Enter after each number. You could type the numbers separated by spaces before pressing Enter. – David Conrad Dec 16 '20 at 18:14
  • Hello @DavidConrad thank you for your feedback. I am not sure to understand how I can valid user input without pressing Enter ? – Mathieu Seligmann Dec 16 '20 at 19:19
  • I don't see anywhere in the code where you validate user input, you just use the values directly. But if you need to check the value and then prompt the user to re-enter it, then, yes, you need to press Enter after each value. – David Conrad Dec 16 '20 at 20:14
  • 1
    If you want more control over your input, then use a Reader (I use BufferedReader). Scanner is designed for beginners, and has a number of annoying quirks. – NomadMaker Dec 16 '20 at 20:42

1 Answers1

1

If I understand correctly, you wanna receive both inputs in the same line. If so, Daveid's comment is correct. you don't have to press enter so you can go with the following lines:


System.out.print("Enter the number of sellers and models (separated by a space): ");

int nbrSellers = myObj.nextInt();
int nbrModels = myObj.nextInt();

And just enter both numbers on the same line like so:

5 4

or you can use delimiters like this:

Scanner myObj = new Scanner(System.in);
System.out.print("Enter the number of sellers and models (separated by a comma): ");
String input = myObj.nextLine();
String[] splitValue = input.split(",");

int nbrSellers = Integer.parseInt(splitValue[0]);
int nbrModels = Integer.parseInt(splitValue[1]);

int[][] sales = new int[nbrSellers][nbrModels];

for(int i = 1; i <= nbrSellers; i++) {
    System.out.print("Seller " + i + " ");
    for(int j = 0; j < nbrModels; j++) {
        sales[i][j] = myObj.nextInt();
    }
    System.out.println();
}

or (based on your comment below) if you have to use a for loop, use this:


for(int i = 0; i < nbrSellers; i++) {
    System.out.print("Please enter " + nbrModels + " values for Seller " + (i + 1) + " (separated by a space): ");
    for(int j = 0; j < nbrModels; j++) {
        sales[i][j] = myObj.nextInt();
    }
    System.out.println();
}

and just input the numbers like so:

3 4 6 2 1

Mehran B
  • 1,007
  • 2
  • 4
  • 14
  • Hello @Mehran Behbahani, thank you vrey much for your support and your feedback. I have to use the for loop because that is an exercise for my Java beginner course. I wanted to have in the same line all the inputs for each seller. Is there a way to skip the line break instead of spliting comma ? Thank you very much – Mathieu Seligmann Dec 16 '20 at 18:36
  • 1
    Just edited my answer. the last part of my answer is the for loop. hope it helps. – Mehran B Dec 16 '20 at 18:45
  • Hi again @Mehran Behbahani, thank you very much. I have tested your solution and edited my question adding a screen shot from the terminal. I still have the results with a line break. I have copied and paste your solution. – Mathieu Seligmann Dec 16 '20 at 19:16
  • bro when you enter the first number for seller one, just don't press enter, hit space and enter the next number again space and next number and after typing the last number, hit enter. for example if you are entering 4 numbers, you write all the numbers in one line like this: `0 3 2 0`, then press enter and enter all the numbers for seller 2 again separated by a space like: `6 2 3 4` and then again press enter. – Mehran B Dec 16 '20 at 22:24