Thread: ArrayList Help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    ArrayList Help


    My instructions were to open a default file and modify it. The directions said to:

    1. Change the application so that the main method prompts the user for the number of elements the ArrayList Should hold.
    2. Then, create a loop that continues to prompt the user for the name and populates the list until the list has been filled.
    3. Call existing display method to display the names in the list.

    Here is what I have so far.

    PHP 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>names = new ArrayList<String>(); 
             
    for 
    (
    int index 0index names.lengthindex++)     

    {         
    System.out.print("Enter name " + (index 1) + ": ");         

    names[index] = input.nextInt();         
    names.add(input);     
    }      
        
    display(names);         
    }    
    public static 
    void display(ArrayList names)     
     {       
    System.out.println("\nThe size of the list is " names.size());       
    for(
    int x 0names.size(); ++x)  

            
    System.out.println("Position " " Name: " names.get(x));    } 

    When compiling Im getting 2 errors and I cant seem to figure out what they are referring to. Ive tried googling them, but have no clue what Im looking at.

    C:\Users\Tyler\Documents\School Work\CIS280\Project 5>javac ArrayListMod.java

    ArrayListMod.java:23: names is already defined in main(java.lang.String[]) ArrayList<String>names = new ArrayList<String>();

    ArrayListMod.java:29: cannot find symbol symbol : method add(java.util.Scanner) location: class int[] names.add(input);

    ArrayListMod.java:32: display(java.util.ArrayList) in ArrayListMod cannot be app lied to (int[]) display(names); ^ 3 errors
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    4
    After a quick look, you don't seem to be using ArrayLists, just plain old arrays. ArrayLists, if my memory serves, are dynamic.

    Edit: Sorry, you code is difficult to read: please align it to typical Java conventions. You've wrapped your code in a PHP tag too.

    Here's how your code should look:
    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(); // Are you extending or importing the date class? It doesn't look like it to me.
              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(); // If you're catching integers from the scanner, always try and catch the errors
              //Create array to hold number of names to enter      
              names = new int[numNames];
              ArrayList<String>names = new ArrayList<String>();
              for(int index = 0; index < names.length; index++)
              {
                   System.out.print("Enter name " + (index + 1) + ": ");
                   names[index] = input.nextInt();
                   names.add(input);
              }
              display(names);
         }
         public static void display(ArrayList names)
         {
              System.out.println("\nThe size of the list is " + names.size());
              for(int x = 0; x < names.size(); ++x)
              // Missing brace here, which is why you should line up your braces
              {
                   System.out.println("Position " + x + " Name: " + names.get(x));
              }
         }
    }


    Regards,

    Shaun.
    Last edited by Shaun_B; October 27th, 2012 at 11:55 AM. Reason: Code was unreadable, and missed the arrayList as a result.
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Look at these two lines

    Code:
    names = new int[numNames]; 
    
             ArrayList<String>names = new ArrayList<String>();
    Note that a name is unique.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by bullet
    Look at these two lines

    Code:
    names = new int[numNames]; 
    
             ArrayList<String>names = new ArrayList<String>();
    Note that a name is unique.
    I think I have it. Is it because I have effectively made names an integer rather than a String? I need to create a new variable to hold the String for name thats input by the user?
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by whitedragon551
    I think I have it. Is it because I have effectively made names an integer rather than a String? I need to create a new variable to hold the String for name thats input by the user?
    No, the problem is that you have made names refer to an array of integers. You cannot use that identifier again in the same scope.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0
    I changed around some things. Down to 1 error. Do I need to define namesList as something? Thats the only thing I can think of. Usually I see this error when I try to use a variable that hasnt been declared yet.

    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
    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));
       }
    }
  12. #7
  13. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Yes, you have to declare a variable before you use it.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0
    Originally Posted by bullet
    Yes, you have to declare a variable before you use it.
    I declared a variable and the error it gave me was that it was already defined in the main when I defined the ArrayList.

    Im out of guesses. Im officially stumped.

IMN logo majestic logo threadwatch logo seochat tools logo