1

I want the user input to be a string added to my array list each time the user inputs a new string. I think I am on the right track but I'm not entirely sure because I can't output my array. Here is my code.

System.out.println("input your word list");

Scanner scanner = new Scanner(System.in);
String phrase;
//not sure if phrase =""; is necessary but eclipse suggested it at one point
phrase = ""; 
ArrayList<String[]> wordlist = new ArrayList<String[]>();   

for(int i=0; i<phrase.length();i++){
  phrase = scanner.nextLine();
  wordlist.add(new String[] {phrase});
}

System.out.println(phrase);
System.out.println(Arrays.toString(wordlist));
4
  • I would think some more about the condition on your for loop. It currently says "loop once for each character in the string phrase". Is that what you expected? If not, what should it be? How would you then translate that into a loop condition? Commented Feb 23, 2016 at 2:56
  • oh whoops, I want it to just loop every time a string is entered, not every time a character is entered in the string. When it loops, I want the Arraylist wordlist to now hold another string Commented Feb 23, 2016 at 2:59
  • Ok, so we know that you've got the wrong condition. How could you translate your actual condition into code? Should the loop ever stop? Is there a special phrase users can enter to end the loop? Commented Feb 23, 2016 at 3:02
  • Well making it to where it only runs on the input is fine, I guess I could do a do loop? and then just make a while condition for the user to say done. Commented Feb 23, 2016 at 3:05

2 Answers 2

1

Would using ArrayList<String> instead of ArrayList<String[]> be a possible solution? In that case you could just do something like this:

Scanner scan = new Scanner(System.in);
ArrayList<String> wordList = new ArrayList<String>();
for (int i=0; i<3; i++){       //not sure what you're doing with your loop
                               //so will just get 3 inputs
    wordList.add(scan.next());
}

Then to print them each out you would do:

for (int i=0; i<wordList.size(); i++){
    System.out.println(wordList.get(i));
}

OR if you prefer for-each:

for (String word : wordList){
    System.out.println(word);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Edit :- here you go this takes all input to array list until the user types quit and displays all the inputs:-

public static void main(String[] args)
{
     System.out.println("input your word list");

     Scanner scanner = new Scanner(System.in);
     ArrayList<String> wordlist = new ArrayList<String>();

     String input = "";
     while(!input.toLowerCase().equals("quit"))
     {
         input = scanner.nextLine();

         if(!input.toLowerCase().equals("quit"))
             wordlist.add(input);
     }

     for(int i = 0; i < wordlist.size(); i++)
     {
         System.out.println(wordlist.get(i));
     }
}

4 Comments

Sorry but this still isn't doing , I want to print the entire array list just so that I can see that all of the strings entered are in the array list
I want the user to enter like "potato" and the array prints [potato]. and if he enters every single vegetable ever I want it to print [potato, carrot, etc, etc]
@Justin please check the edit, is that how you need?
Yes the problem was that i was using .length and not .size...Thank you very much for your time @NullSaint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.