10

What I am trying to do is have multiple inputs that all have different variables. Each variable will be part of different equations. I am looking for a way to do this, and I think I have an idea. I just want to know if this would be legal, and if there is a better way to do this.

import java.util.*;

public class Example{

public static void main(String args[]){
    
    Scanner dd = new Scanner(System.in);
    
    System.out.println("Enter number.");
    int a = dd.nextInt();
    System.out.println("Enter number.");
    int b = dd.nextInt();
    System.out.println("Enter number.");
    int c = dd.nextInt();
  }
}
2
  • 2
    Yes, that is a valid approach. I'm not aware of a better way to do that. It would be more usable I think if you used nextString() and read them all in as a single line and parsed them Commented Dec 12, 2011 at 4:22
  • 6
    The main "gotcha" that I have to warn you about when using Scanner is that you have to be careful handling the End Of Line (EOL) token. The nextInt() method does not deal with the EOL token, while nextLine() does. There may be some times when you'll need to call nextLine() without saving the input just to handle the EOL token. It's not an issue in your current code, and there's no need to change it just yet. Commented Dec 12, 2011 at 4:26

1 Answer 1

17

If every input asks the same question, you should use a for loop and an array of inputs:

Scanner dd = new Scanner(System.in);
int[] vars = new int[3];

for(int i = 0; i < vars.length; i++) {
  System.out.println("Enter next var: ");
  vars[i] = dd.nextInt();
}

Or as Chip suggested, you can parse the input from one line:

Scanner in = new Scanner(System.in);
int[] vars = new int[3];

System.out.println("Enter "+vars.length+" vars: ");
for(int i = 0; i < vars.length; i++)
  vars[i] = in.nextInt();

You were on the right track, and what you did works. This is just a nicer and more flexible way of doing things.

Sign up to request clarification or add additional context in comments.

5 Comments

well the questions are not right i just used them to show what im doing they are all different just wondering do i have to make a new scanner for each input or can i just make one scanner and give each input a different variable
No, Scanner is a Stream. In other words, it takes all input from a source and sends the earliest information available when you call it. So in.nextInt() waits for system input (the keyboard), and then returns the next available data, as an int.
What I am doing is practicing building object classes and im making a back account object the starts an account with a starting balance and name and then a deposit and withdraw method so i kinda need the different variables for the different inputs
well do you have a suggestion of how to go about this im in school for computer science but I am not in any programming classes yet so i have no teacher to ask but i still am trying to dable into java
Then you had it right originally. That would be the best way to do this, but obviously with different messages to the user.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.