Results 1 to 15 of 15

Thread: ArrayList Help

  1. #1
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default ArrayList Help

    Im am taking one of my last classes for my Bachelors Degree in Computer Info Systems. My weak point is programming and unfortunately for me Java happened to be in my last semester. Im stuck on a programming homework assignment and I can not figure it out.

    Basically the point of this is to allow the user to input a number that is the length of the ArrayList. Then the program will ask the user for names until that length for ArrayList is reached. At which point it prints the size of the ArrayList, the names, and position in the list. As of right now I am getting an error code. Id appreciate any help I can get.

    Here is my coding:

    Java Code:
    //ArrayListMod.java by Tyler 10/27/2012
    
    import java.util.*;
    
    public class ArrayListMod
    {
       public  static void main(String[] args)
       {
    	int numNames;
    	int[] names;
    	
    	Date today = new Date();
    	System.out.println("ArrayListMod.java" + "\nby Tyler " + today);
    	Scanner input = new Scanner(System.in);
    	
    	//Enter number of names to have in array
    	System.out.println("Enter number of names to input: ");
    	numNames = input.nextInt();
    	
    	//Create array to hold number of names to enter
    	names = new int[numNames];
    	
    	ArrayList<String>namesList = new ArrayList<String>();
    	
    	for (int x = 0; x < names.length; x++)
    	{
    		System.out.print("Enter name " + (x + 1) + ": ");
    		names[x] = input.nextInt();
    		namesList.add(input);
    	}
        
    	display(namesList);
        
       }
       public static void display(ArrayList namesList)  
       {
          System.out.println("\nThe size of the list is " + namesList.size());
          for(int x = 0; x < namesList.size(); ++x)
             System.out.println("Position " + x + " Name: " + namesList.get(x));
       }
    }
    Java Code:
    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    C:\Users\Tyler>cd C:\Users\Tyler\Documents\School Work\CIS280\Project 5
    
    C:\Users\Tyler\Documents\School Work\CIS280\Project 5>javac ArrayListMod.java
    ArrayListMod.java:29: cannot find symbol
    symbol  : method add(java.util.Scanner)
    location: class java.util.ArrayList<java.lang.String>
                    namesList.add(input);
                             ^
    1 error
    
    C:\Users\Tyler\Documents\School Work\CIS280\Project 5>

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default Re: ArrayList Help

    You're trying to add the Scanner to the ArrayList, while the ArrayList is set to only accept Strings.
    Java Code:
    Scanner input = new Scanner(System.in);
    ArrayList<String>namesList = new ArrayList<String>();
    namesList.add(input);

  3. #3
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList Help

    Quote Originally Posted by Toll View Post
    You're trying to add the Scanner to the ArrayList, while the ArrayList is set to only accept Strings.
    Java Code:
    Scanner input = new Scanner(System.in);
    ArrayList<String>namesList = new ArrayList<String>();
    namesList.add(input);
    I guess I dont understand. Whats the best practice? To convert Scanner to a String and add said string to namesList?

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default Re: ArrayList Help

    Scanner only helps you get the strings, so you wouldn't convert a Scanner to a String, no. I haven't worked much with Scanner though, so I can't give any example on how exactly you should go about getting the String, but reading the Javadoc for Scanner seems to suggest Scanner.nextLine(). I'm sure someone better versed with the inner workings of Scanner can help you with that part though.

  5. #5
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList Help

    Quote Originally Posted by Toll View Post
    Scanner only helps you get the strings, so you wouldn't convert a Scanner to a String, no. I haven't worked much with Scanner though, so I can't give any example on how exactly you should go about getting the String, but reading the Javadoc for Scanner seems to suggest Scanner.nextLine(). I'm sure someone better versed with the inner workings of Scanner can help you with that part though.
    I tried using nextLine(); and that was a no go as well. Ive been working on this for 2 days. Its starting to drive me nuts.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: ArrayList Help

    Quote Originally Posted by whitedragon551 View Post
    and that was a no go as well.
    What does that mean? Did you get an error, then copy and paste the full error here so we can see it. Plus provide a full explanation of what is going on. We cannot see your screen and most people won't copy your code to run it.

  7. #7
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList Help

    Quote Originally Posted by Junky View Post
    What does that mean? Did you get an error, then copy and paste the full error here so we can see it. Plus provide a full explanation of what is going on. We cannot see your screen and most people won't copy your code to run it.
    Everything is in the original post.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: ArrayList Help

    No it isn't!

    Originally your tried inserting a Scanner reference into a String List. Then after some advice you said you tried nextLine and it didn't work. So therefore you must have changed your code and got another error. What is the changed code and what was the error? We don't read minds.

  9. #9
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList Help

    Quote Originally Posted by Junky View Post
    No it isn't!

    Originally your tried inserting a Scanner reference into a String List. Then after some advice you said you tried nextLine and it didn't work. So therefore you must have changed your code and got another error. What is the changed code and what was the error? We don't read minds.
    That change wasnt saved. Im working off of what is posted in the original post. Not sure where to go from there. Not sure if the nextLine() is the right option? I have no idea what Im doing. Im completely new to this.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: ArrayList Help

    :headdesk:

    Yes it is the correct option. Just because you got another error when you tried it doesn't mean it was wrong and you should go back to your orignal code. Post the changed code and the new error message here so someone can help you.

  11. #11
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList Help

    New code is as follows. Notice line 28 is pretty much the only change aside from adding documentation.

    Java Code:
    //ArrayListMod.java by Tyler 10/27/2012
    
    import java.util.*;
    
    public class ArrayListMod
    {
       public  static void main(String[] args)
       {
    	int numNames;
    	int[] names;
    	
    	Date today = new Date();
    	System.out.println("ArrayListMod.java" + "\nby Tyler " + today);
    	Scanner input = new Scanner(System.in);
    	
    	//Enter number of names to have in array
    	System.out.println("Enter number of names to input: ");
    	numNames = input.nextInt();
    	
    	//Create array to hold number of names to enter
    	names = new int[numNames];
    	//Defined ArrayList
    	ArrayList<String>namesList = new ArrayList<String>();
    	//Enter names until x is greater than number entered for array length
    	for (int x = 0; x < names.length; x++)
    	{
    		System.out.print("Enter name " + (x + 1) + ": ");
    		names[x] = input.nextLine();
    		namesList.add(input);
    	}
        //Calls display method.
    	display(namesList);
        
       }
       public static void display(ArrayList namesList)  
       {
          System.out.println("\nThe size of the list is " + namesList.size());
          for(int x = 0; x < namesList.size(); ++x)
             System.out.println("Position " + x + " Name: " + namesList.get(x));
       }
    }
    New Error code:

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Tyler\Documents\School Work\CIS280\Project 5>javac ArrayListMod.java
    ArrayListMod.java:28: incompatible types
    found : java.lang.String
    required: int
    names[x] = input.nextLine();
    ^
    ArrayListMod.java:29: cannot find symbol
    symbol : method add(java.util.Scanner)
    location: class java.util.ArrayList<java.lang.String>
    namesList.add(input);
    ^
    2 errors

    C:\Users\Tyler\Documents\School Work\CIS280\Project 5>

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: ArrayList Help

    Read the error messages. They are there for a reason.

    First one: the nextLine method returns a String and you are trying to insert it into a int array. just like the messages says. Found: String, required: int

    Second one is the same as before you cannot insert the Scanner into your String ArrayList, you must insert a String which is obtained from........

  13. #13
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList Help

    Quote Originally Posted by Junky View Post
    Read the error messages. They are there for a reason.

    First one: the nextLine method returns a String and you are trying to insert it into a int array. just like the messages says. Found: String, required: int

    Second one is the same as before you cannot insert the Scanner into your String ArrayList, you must insert a String which is obtained from........
    Have no idea what you said, but I some how figured it out. I added another scanner to get names and changed the logic a little bit. Here is the final code:

    Java Code:
    //ArrayListMod.java by Tyler 10/27/2012
    
    import java.util.*;
    
    public class ArrayListMod
    
    {
       public  static void main(String[] args)
       {
    	int numNames;
    	int[] names;
    	String name;
    	
    	Date today = new Date();
    	System.out.println("ArrayListMod.java" + "\nby Tyler " + today);
    	Scanner input = new Scanner(System.in);
    	Scanner nameEntered = new Scanner(System.in);
    	
    	//Enter number of names to have in array
    	System.out.println("Enter number of names to input: ");
    	numNames = input.nextInt();
    	
    	//Create array to hold number of names to enter
    	names = new int[numNames];
    	//Defined ArrayList
    	ArrayList<String>namesList = new ArrayList<String>();
    	//Enter names until x is greater than number entered for array length
    	for (int x = 0; x < names.length; x++)
    	{
    	System.out.print("Enter name " + (x + 1) + ": ");
    		name = nameEntered.nextLine();
    		namesList.add(name);
    	}
        //Calls display method.
    	display(namesList);
        
       }
       public static void display(ArrayList namesList)  
       {
          System.out.println("\nThe size of the list is " + namesList.size());
          for(int x = 0; x < namesList.size(); ++x)
             System.out.println("Position " + x + " Name: " + namesList.get(x));
       }
    }

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: ArrayList Help

    You could have used a single Scanner you just needed to call the correct methods which you weren't. But for some bizarre reason once you created another Scanner object you managed to call the correct methods. :shrugs:

  15. #15
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: ArrayList Help

    Quote Originally Posted by Junky View Post
    You could have used a single Scanner you just needed to call the correct methods which you weren't. But for some bizarre reason once you created another Scanner object you managed to call the correct methods. :shrugs:
    I never had a String name; declared to hold the names entered. I think thats what was throwing me off.

    Thanks everyone for the help.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 06:07 AM
  3. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  4. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

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