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

    Default Comparing in an array with ints

    In our class, we were given some code in an assignment and asked what are the last four things to be printed out. I have tried to run this program and I know that one of the problems is that you can't use compareTo with ints, but I don't know what to use instead. I also don't understand why we were given incorrectly written code since debugging wasn't part of the assignment, but anyway, could someone please help me with this. Here's the first part that's written.

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class IntList implements Serializable{
    	
    	private static int length;
    	// Data fields
    protected int[] values;
    protected int numItems;
    protected int currentPos;
    
    {
    	IntList intList = new IntList(15);}
    	IntList(int maxItems){
    	  // Constructor
    	  // Post: Empty list is created with maxItems cells
    	numItems = 0;
    	values = new int[maxItems];
    	currentPos = 0;
    }
    	 // Methods
    	    public void add(int values){
    	  // Pre:  The list is not full 
    	  if (!isFull())
    	  {
    		  int index = numItems - 1;
    	  }
    	  }
    	 // Post: item is in the list
    	  public void printList(){
    	  // Post: If the list is not empty, the elements are
    	  //       printed on the screen; otherwise "The list
    	  //       is empty" is printed on the screen
    		  int  index;
    
    		    for (index = 0; index < numItems; index++)
    		       System.out.println(values[index]);
    		   }
    	  public int getLength(Scanner inFile){
    	  // Post: return value is the number of items in the list
    		 return numItems;
    	  }
    	  public boolean isEmpty(){
    	  // Post: returns true if list is empty; false otherwise
    	    	return(numItems ==0);
    	    }
    	  public boolean isFull(){
    	  // Post: returns true if there is no more room in the
    	  //       list; false otherwise
    		    return(IntList.length == numItems);
    	  }
    	  public boolean contains(IntList item){
    		  //returns true if item is in the list: false otherwise
    	  int index = 0;
    	  while (index < numItems && values[index].compareTo(item) != 0)//<----Problem because of int
    		  index++;
    	  return (index < numItems);
    	  }
    	  public void remove(int item){
    		  //removes a value from the list. 
    		  //The precondition is that the value is in the list.
    		  int index = 0;
    		  boolean found = false;
    		  while (index < numItems && !found)
    		  {
    			  if (values[index].compareTo(item)==0) //<----Problem because of int
    				  found = true;
    			  else
    				  index++;
    		  }
    		  if (found)
    		  {
    			  for (int count = index; count < numItems-1; count++)
    				  values[count] = values[count+1];
    			  numItems--;
    		  }
    		}
    	public void getList(Scanner inFile) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    Here's the driver to run this..

    Java Code:
    import java.util.Scanner;
    import java.io.*;         // Access file classes
    
    public class ReadInts {
    
    	
    	public static void main(String[] args) throws FileNotFoundException {
    		
    		// Data file for this test is on "real.dat"
    	    Scanner inFile = new Scanner(new FileReader("int.txt"));
    	    IntList  list;
    	    
    	   // Instantiate list with 15 cells
    	    list = new IntList(15);
    	    
    	 // Input values into list
    	    ((IntList) list).getLength(inFile);   //<-------error on this line
    	    // Print values on System.out
    	    list.printList();
    	    inFile.close();      // Close the input file 
    	}
    
    }
    I've also attached the txt file that the scanner reads from. I'd greatly appreciate your help. Thanks
    Attached Files Attached Files

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

    Default Re: Comparing in an array with ints

    If there were errors, please copy and paste the full text of the error messages here.
    Can you explain what problems you are having?

    with ints, but I don't know what to use
    Use the ==, !=, etc operators to compare primitive values.
    Last edited by Norm; 01-02-2012 at 04:57 PM.

  3. #3
    queenz68 is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Re: Comparing in an array with ints

    The error message is "Line breakpoint: IntList - contains (IntList). and "Cannot invoke compareTo(IntList) on the primitive type int. Now that part I know, but how do you compare them. You said using== or!=, but would I just replace the compareTo part with and or not? Excuse my lack of java knowledge, I'm still very new to this.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,581
    Rep Power
    25

Similar Threads

  1. Comparing elements of an array
    By fluteattack in forum New To Java
    Replies: 1
    Last Post: 05-09-2011, 05:43 PM
  2. reading in unsigned ints into a 2D array
    By newToIt in forum New To Java
    Replies: 9
    Last Post: 03-06-2009, 12:36 PM
  3. comparing elements in array
    By garyscott101 in forum New To Java
    Replies: 14
    Last Post: 12-10-2008, 03:01 PM
  4. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 02:33 AM
  5. comparing array using character
    By Anseki in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 07:28 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
  •