2Devnull
2Devnull

Reputation: 1

How do you have the user set the variable?

I am trying to have the user set the name for how many numbers they set. for example if they set 3 the program asks for name 3 times and then sets those names to a different varible inside an array.

 public static void main(String[] args) {
    Scanner Num = new Scanner(System.in);
    Scanner Name = new Scanner(System.in);
    
    
    System.out.println("How many names do you want to enter: ");
    int number = Num.nextInt();
    int[] numbs = new int[number];
    
    for (int i = 0; i < numbs.length; i++) {
        
        System.out.println("What is your name");
        
        String [] nameArray = Name.nextLine();

Upvotes: 0

Views: 59

Answers (3)

Titan Demon
Titan Demon

Reputation: 33

Create a String array of number length outside the loop and initialize each index with name as input.
By the way you only need to create one Scanner object for input(Not needed to create various objects for different input).
Edit - There was no use of numbs array.

Scanner sc=new Scanner(System.in);
System.out.println("How many names do you want to enter: ");
int number = sc.nextInt();
String []nameArray=new String[number];
/*Since nextInt() does not read the newline character in your input  
created by hitting "Enter"*/.
sc.nextLine();  
for (int i = 0; i < number; i++) {   
    System.out.println("What is your name");  
    nameArray[i]=sc.nextLine();
}
sc.close();  //To prevent memory leak

Upvotes: 0

Kaan
Kaan

Reputation: 5794

Starting point

Here are several things happening in your posted code:

  • You have two different Scanners, each reading from System.in
  • After prompting for user input (int number = Num.nextInt()), you're using that number to create an array of size "number"
  • When you call nextLine(), you are assigning the result to a string array (String []).

Working solution

Here's a variation which addresses those issues, with a few additions, too:

  • Use one Scanner, and give it a general name ("scanner", not "Num") since a scanner has nothing to do with one specific data type. Any scanner can read strings, integers, booleans, bytes, etc. Look in Javadoc for the full set of supported data types.
  • Check if user input is valid before trying to create an array – if user enters "-1" that's a valid integer value, but not valid for allocating an array – new int[-1]) would throw a java.lang.NegativeArraySizeException at runtime.
  • Use "next()" instead of "nextLine()" – with the rest of your example, using "nextLine()" results in skipping one line without direct interaction from the user (so the first "name" is always an empty string)
  • Assign the result of "next()" to a String (not String[]), matching the return type from the method
  • Use "System.out.print()" instead of "println()", a little tidier program output
  • Use lowercase names to follow Java naming conventions ("scanner", not "Scanner")
Scanner scanner = new Scanner(System.in);

System.out.print("How many names do you want to enter: ");
int times = scanner.nextInt();

if (times < 0) {
    System.out.println("negative numbers not allowed");
} else {
    String[] names = new String[times];
    for (int i = 0; i < times; i++) {
        System.out.print("What is your name: ");
        names[i] = scanner.next();
    }
    System.out.println(Arrays.toString(names));
}

And here's a sample run:

How many names do you want to enter: 3
What is your name: one
What is your name: two
What is your name: three
[one, two, three]

Upvotes: 1

Keshavram Kuduwa
Keshavram Kuduwa

Reputation: 1060

Just a small improvement over Titan's answer.

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("How many names do you want to enter: ");
    int numberOfTimes = sc.nextInt();
    String[] names = new String[numberOfTimes];
    sc.nextLine();
    for (int i = 0; i < numberOfTimes; i++) {
      System.out.println("What is your name");
      names[i] = sc.nextLine();
    }
    System.out.println(Arrays.toString(names));
  }

Upvotes: 1

Related Questions