Results 1 to 4 of 4
  1. #1
    bigbreez21 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default help with ArrayLists !

    here's the code i have so far for my assignement. So far as I can tell, only the findShortest and findLongest methods don't work -- but I have no idea how to get them to work! Can anyone please help?
    Java Code:
    import java.util.Scanner;  
    import java.util.*; 
    
    import java.util.Iterator;
    
    public class arrayListPractice
    {
    
        public static void main(String[] args){
            
            ArrayList<String> names = new ArrayList();
            
            System.out.println("How many students are in the class?");
            
            Scanner scan = new Scanner(System.in);
            
            int numStudents = scan.nextInt();
            
            System.out.println("Thank you.");
            
            for(int i = 1; i <= numStudents; i++){
                
                System.out.println("Please input student " + i + "'s name:");
                
                String name = scan.next();
                
                names.add(name);
            
            
            
        }
       
            System.out.println("Thank you.");
            
                    System.out.println();
            
            System.out.println("Here are the names in order:");
            
            System.out.println(printList(names));
            
                    System.out.println();
                    
                    
            
            System.out.println("Here are the names in reverse order:");
            
            System.out.println(printListInReverse(names));
            
                    System.out.println();
                    
                    
            
            System.out.println("Here is the longest name in the list:");
            
            System.out.println(findLongestName(names));
            
                    System.out.println();
                    
                    
            
            System.out.println("Here is the shortest name in the list:");
            
            System.out.println(findShortestName(names));
            
                    System.out.println();
                    
                    
            
            System.out.println("Here is the list with the BAD NAMES removed:");
            
            System.out.println(eliminate(names));
            
                    System.out.println();
                    
                    
     }
     
     
     
     
     
     public static ArrayList printList(ArrayList<String> names){
         
       ArrayList printList = names ;
      
       return printList;
         
        }
     
        
        
        public static ArrayList printListInReverse(ArrayList<String> names){
      
         
            ArrayList printListInReverse = new ArrayList() ;
        for ( int i = names.size() - 1 ; i >= 0 ; i-- )
        {
           Object obj = names.get( i ) ;
           printListInReverse.add( obj ) ;
        }
       return printListInReverse ;
        
        
     }
     
     
        public static ArrayList findLongestName(ArrayList<String> names){
            
            ArrayList longest = names;
            
            int longer = longest.size();
            
                for(int i = 0; i < longer; i ++){
                    
                    String named = longest.get(i);
                    
                    if(named[i].length > max){
                        
                        int longest;
                        i = longest;
                        
                        
                    }
            
        }
        
        
        return names( longest);
        
       }
        
        public static ArrayList findShortestName(ArrayList<String> names){
            
                int min = 0;
            
                ArrayList shortest = names;
            
                int size = shortest.size();
            
                for(int i = 0; i < size; i ++){
                    
                    String named = shortest.get(i);
                    
                    if(named[i].length < min){
                        
                        int shortest;
                        i = shortest;
                        
                        
                    }
            
                }
        
        
                return names( shortest);
        
            }
        
        
        
        public static ArrayList eliminate(ArrayList<String> names){
            
            for ( int i = names.size; i > 0; i -- ){
                
                   ArrayList eliminate = names ;
                
                if(eliminate(i) == Harrison || eliminate(i) == John || eliminate(i) == Dilpreet || eliminate(i) == Arman){
                    
                    eliminate.remove( i );
                    
                }
                
            }
            
            return eliminate ;
        }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: help with ArrayLists !

    Does your code compile? If not, please copy and paste the full text of the error messages here.

  3. #3
    bigbreez21 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: help with ArrayLists !

    Java Code:
    arrayListPractice.java:115: error: incompatible types
                    String named = longest.get(i);
                                              ^
      required: String
      found:    Object
    arrayListPractice.java:117: error: array required, but String found
                    if(named[i].length > max){
                            ^
    arrayListPractice.java:117: error: cannot find symbol
                    if(named[i].length > max){
                                         ^
      symbol:   variable max
      location: class arrayListPractice
    arrayListPractice.java:119: error: longest is already defined in findLongestName(ArrayList<String>)
                        int longest;
                            ^
    arrayListPractice.java:128: error: cannot find symbol
        return names( longest);
               ^
      symbol:   method names(ArrayList)
      location: class arrayListPractice
    arrayListPractice.java:142: error: incompatible types
                    String named = shortest.get(i);
                                               ^
      required: String
      found:    Object
    arrayListPractice.java:144: error: array required, but String found
                    if(named[i].length < min){
                            ^
    arrayListPractice.java:146: error: shortest is already defined in findShortestName(ArrayList<String>)
                        int shortest;
                            ^
    arrayListPractice.java:155: error: cannot find symbol
                return names( shortest);
                       ^
      symbol:   method names(ArrayList)
      location: class arrayListPractice
    arrayListPractice.java:163: error: size has private access in ArrayList
            for ( int i = names.size; i > 0; i -- ){
                               ^
    arrayListPractice.java:167: error: method eliminate in class arrayListPractice cannot be applied to given types;
                if(eliminate(i) == "Harrison" || eliminate(i) == "John" || eliminate(i) == "Dilpreet" || eliminate(i) == "Arman"){
                   ^
      required: ArrayList<String>
      found: int
      reason: actual argument int cannot be converted to ArrayList<String> by method invocation conversion
    arrayListPractice.java:167: error: method eliminate in class arrayListPractice cannot be applied to given types;
                if(eliminate(i) == "Harrison" || eliminate(i) == "John" || eliminate(i) == "Dilpreet" || eliminate(i) == "Arman"){
                                                 ^
      required: ArrayList<String>
      found: int
      reason: actual argument int cannot be converted to ArrayList<String> by method invocation conversion
    arrayListPractice.java:167: error: method eliminate in class arrayListPractice cannot be applied to given types;
                if(eliminate(i) == "Harrison" || eliminate(i) == "John" || eliminate(i) == "Dilpreet" || eliminate(i) == "Arman"){
                                                                           ^
      required: ArrayList<String>
      found: int
      reason: actual argument int cannot be converted to ArrayList<String> by method invocation conversion
    arrayListPractice.java:167: error: method eliminate in class arrayListPractice cannot be applied to given types;
                if(eliminate(i) == "Harrison" || eliminate(i) == "John" || eliminate(i) == "Dilpreet" || eliminate(i) == "Arman"){
                                                                                                         ^
      required: ArrayList<String>
      found: int
      reason: actual argument int cannot be converted to ArrayList<String> by method invocation conversion
    arrayListPractice.java:175: error: cannot find symbol
            return eliminate ;
                   ^
      symbol:   variable eliminate
      location: class arrayListPractice
    Note: arrayListPractice.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    15 errors
    Last edited by Norm; 11-20-2011 at 08:24 PM. Reason: added tags to save formatting

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: help with ArrayLists !

    Which of the error messages are you having trouble with? Most are self explanatory. For example:
    array required, but String found
    The compiler has found a variable in a statement that is a String but by the syntax of the statement the variable type should be an array.

    A technque to keep from getting so many errors at once is to type in the minimum code and do a compile, fix the errors and compile again. Continue until you get a clean compile. Then add a few more lines and do it again.
    Don't wait until you have typed in 170+ lines before trying to compile your program.

Similar Threads

  1. ArrayLists- generic?
    By katiebear128 in forum New To Java
    Replies: 3
    Last Post: 10-14-2011, 02:43 AM
  2. array of ArrayLists
    By imorio in forum New To Java
    Replies: 8
    Last Post: 09-24-2011, 01:05 PM
  3. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 04:27 AM
  4. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:38 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
  •