Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having trouble populating a String array from user input data. Here is a simple example of my issue:

import java.util.Scanner;

public class TestArray {
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        System.out.println("Enter a number between 1 and 5");
        int number = input.nextInt();    

        System.out.println("Now enter " + number +  " names");    
        String names = input.next();

        String[] nameList = new String[number];

        for(int i = 0; i < number; i++){
            nameList[i] = input.next();
            System.out.println(nameList[i]);
        }
    }
}     

I do not know why this is only printing out the last name entered. What I ultimately am looking for is to print out every name. I have also tried using input.nextLine() which did not work.

Any tips or pointers would be greatly appreciated.

share|improve this question
2  
that's not the complete code. –  Rohit Jain Oct 18 '13 at 18:23
2  
What's the point of the names variable, and why does it consume the first input.next()? –  Kevin DiTraglia Oct 18 '13 at 18:24
2  
@RohitJain It's only lacking three closing brackets.... –  Tom Swifty Oct 18 '13 at 18:26
    
works fine for me, just remove the line that declares the names variable. –  ns47731 Oct 18 '13 at 18:28
    
Excuse me for not putting the closing brackets in my code above, I thought that would be understood. Anyway, this does work when the names variable is removed, however, they print after each input. I want them to be printed out all at once after the user types them all in. –  censortrip Oct 18 '13 at 18:31
show 1 more comment

4 Answers

up vote 2 down vote accepted

Just have a seperate loop after entering names to print them all out:

import java.util.Scanner;

public class TestArray {
    public static void main(String[] args){

        Scanner input = new Scanner(System.in);

        System.out.println("Enter a number between 1 and 5");
        int number = input.nextInt();    

        System.out.println("Now enter " + number +  " names");    
        //NOTE:  You may want to remove this line since it will mess up your data.
        //String names = input.next();

        String[] nameList = new String[number];

        for(int i = 0; i < number; i++){
            nameList[i] = input.next();
        }

        //print separate here.
        System.out.println("Here are the names you entered:  ");
        for(int i = 0; i<number; i++){
            System.out.println(nameList[i]);
        }
    }
} 
share|improve this answer
add comment

What's the purpose of

String names = input.next();

?

You don't use names at all and are catching the next input after the number because of it.

It should work if you remove it...

share|improve this answer
    
Yes, I had an error in logic with that. I appreciate the response though. it does work when I remove it, just not the way I want, but I should be good from here. –  censortrip Oct 18 '13 at 18:39
add comment

Your first input.next() call (after the prompt, "Now enter X names") is skewing your data. Just remove it.

public class TestArray {
    public static void main(String[] args){

       Scanner input = new Scanner(System.in);

       System.out.println("Enter a number between 1 and 5");
       int number = input.nextInt();    

       System.out.println("Now enter " + number +  " names");

       String[] nameList = new String[number];

        for(int i = 0; i < number; i++){
            nameList[i] = input.next();
            System.out.println(nameList[i]);
        }
    }
}
share|improve this answer
add comment

The line

 String names = input.next();

Is causing your problem. If you take that out, the program performs as you describe it should. Alternatively you could populate your array in one loop, and then loop through the list afterward printing them. This depends on what exactly you're looking to do.

 import java.util.Scanner;

 public class test{
  public static void main(String[] args){

      Scanner input = new Scanner(System.in);

      System.out.println("Enter a number between 1 and 5");
      int number = input.nextInt();    

      System.out.println("Now enter " + number +  " names");      
      //String names = input.next();

      String[] nameList = new String[number];

       for(int i = 0; i < number; i++){
                    nameList[i] = input.next();
          System.out.println(nameList[i]);
    }    
  }
}
share|improve this answer
    
Thank you for the alternate solution Tom. –  censortrip Oct 18 '13 at 19:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.