Results 1 to 6 of 6
  1. #1
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Error with ArrayList?

    Okay so here is my code

    Java Code:
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class Problem1
    {
    	public static void main(String[] args)
    	{
    		Scanner scan = new Scanner(System.in);
    
    		ArrayList<String> list = new ArrayList<String>();
    
    		System.out.println("Enter a string: ");
    
    		String stop = "Quit";
    		String inputStop = "";
    		String arrayString = scan.nextLine();
    		inputStop = scan.nextLine();
    
    		while (!inputStop.equals(stop))
    		{
    			list.add(arrayString);
    		}
    
    		if (inputStop.equals(stop))
    		{
    			for (int x = 0; x < list.size; x++)
    			{
    				System.out.println(list.get(x));
    			}
    		}
    	}
    }
    There is an error and says that my ArrayList has private access. I can't figure out how to fix it.

    Edit: Okay I fixed it; however, now I am facing a new problem. The code runs but when I enter "Quit", the program just stops. The arraylist isn't printed out?
    Last edited by Mostpured; 01-30-2015 at 01:21 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Error with ArrayList?

    Is this your current code? How can the following loop possibly stop? It keeps on adding the same String to the list.

    Java Code:
    while (!inputStop.equals(stop)){
    
      list.add(arrayString);
    
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Error with ArrayList?

    Quote Originally Posted by jim829 View Post
    Is this your current code? How can the following loop possibly stop? It keeps on adding the same String to the list.

    Java Code:
    while (!inputStop.equals(stop)){
    
      list.add(arrayString);
    
    }
    Regards,
    Jim
    Yes, what I am trying to do is continuously add the value to the ArrayList until the user types "Quit". Then I want the values to be displayed and then the program should stop.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Error with ArrayList?

    You need to prompt for the input inside the loop.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Mostpured is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: Error with ArrayList?

    Quote Originally Posted by jim829 View Post
    You need to prompt for the input inside the loop.

    Regards,
    Jim
    I don't think the prompt is the problem because I only want the user to be prompted once. The user then enters a value and that same values continues to be added to the array list. The users next input will be "Quit". After this, all of the values the array list contains (which is the the same) will be printed out.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Error with ArrayList?

    First you prompt for arrayString. Then you prompt for inputStop (i.e. "Quit"). Then you reach the while loop. how may times do you think the loop will execute, adding arrayString to the list? Remember, inputStop now equals "Quit"

    One more observation. If you are only inputting a single value, then why use a loop?

    Regards,
    Jim
    Last edited by jim829; 01-30-2015 at 04:48 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. ArrayList - Logic Error?
    By dinosaur1993 in forum New To Java
    Replies: 5
    Last Post: 03-23-2014, 10:14 AM
  2. String to ArrayList error
    By anoorally in forum New To Java
    Replies: 3
    Last Post: 03-04-2012, 06:47 PM
  3. ArrayList size error
    By wepwep in forum New To Java
    Replies: 9
    Last Post: 03-29-2011, 09:57 AM
  4. Java Heap space error using ArrayList.
    By AcousticBruce in forum New To Java
    Replies: 13
    Last Post: 01-03-2011, 01:06 PM
  5. ArrayList IndexOutOfBounds... error
    By Arius in forum New To Java
    Replies: 7
    Last Post: 01-17-2010, 10:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •