0

I have an array list, I type in something into it and it saves just the last thing I typed, but I need things that were typed before. How do I fix it?

Scanner input = new Scanner(System.in); 

ArrayList arrayListOne;
arrayListOne = new ArrayList();
ArrayList<String> letterArray = new ArrayList<String>();
for(int i = 0; i < letterArray.size(); i++)  {
    System.out.println(letterArray.get(i));
}

System.out.println("Type a string:");
letterArray.add(input.nextLine());
System.out.println("Number of string in array: " + letterArray.size());
4
  • 3
    Your code just asks for one line from user and prints it, so its working as expected. Secondly i don't understand purpose of arrayListOne Commented Dec 28, 2014 at 3:56
  • You're more likely to get help if you provide actual input, the actual output and the output you expected instead. Commented Dec 28, 2014 at 3:56
  • I don't think your question is clear! First of all what does arrayListOne does? second of all, everything you typed has been saved in letterArray! Commented Dec 28, 2014 at 3:56
  • 3
    your for loop will run 0 times. Commented Dec 28, 2014 at 3:58

3 Answers 3

3

You only add one String to the ArrayList. If you want it to display many Strings, first you need to add multiple Strings, probably in some sort of loop such as a for loop or while loop. Then after adding all text, create another for loop to display it all.

e.g. since this sounds like homework, much better to show what I mean in pseudo code (and shame to anyone who cheats you out of the experience of trying to code this yourself by spoon-feeding you a solution):

create array list
entry String equals ""
do this loop
   get input from user
   put it into entry String
   add entry String into array list
while entry String doesn't equal "quit"
for each item in array list
   println each item
end for loop
Sign up to request clarification or add additional context in comments.

1 Comment

Plus one on the fishing lesson.
0
private static List<String> getAndPrintInputFromUser() {

    Set<String> ids = new TreeSet<String>(); //used set for making list as unique
    Scanner input = new Scanner(System.in);
    System.out.println("\nEnter each value\n" +
            "and Put an extra ENTER .");
    do{

        String x = input.nextLine();

        if(x==null || StringUtils.isEmpty(x.trim())){
            break;
        }else {
            ids.add(x);
        }

    }while(true);

    for(String id : ids)
        System.out.println(id);

    return new ArrayList<String>(ids);
}

Output:

Enter each value
and Put an extra ENTER.
TP6100010
TP6100015
TP6100019


TP6100010
TP6100015
TP6100019

Comments

-2

Try the following to display the list and keep adding new letters:

Scanner input = new Scanner(System.in); 

ArrayList arrayListOne;
arrayListOne = new ArrayList();
ArrayList<String> letterArray = new ArrayList<String>();

while(true) {
  System.out.println("Type a string:");
  letterArray.add(input.nextLine());
  System.out.println("Number of string in array: " + letterArray.size());
  // Display eveything in the list
  displayList(letterArray);
}


// Somewhere in your class add this method
public void displayList(ArrayList letterArray) {
  for(int i = 0; i < letterArray.size(); i++) 
    System.out.println(letterArray.get(i));
}

full code:

import java.util.ArrayList;
import java.util.Scanner;

public class Practice {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 

    ArrayList arrayListOne;
    arrayListOne = new ArrayList();
    ArrayList<String> letterArray = new ArrayList<String>();

    while(true) {
      System.out.println("Type a string:");
      letterArray.add(input.nextLine());
      System.out.println("Number of string in array: " + letterArray.size());
      // Display eveything in the list
      displayList(letterArray);
    }
  }


  public static void displayList(ArrayList letterArray) {
    for(int i = 0; i < letterArray.size(); i++) 
      System.out.println(letterArray.get(i));
  }
}

2 Comments

Have you tried to run your code? Or, at least, try to compile?
Yes! Should I have posted my full code with the whole Class structure? I just showed an example of how the problem can be fixed! not showing the full solution!

Your Answer

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