Results 1 to 4 of 4
Thread: Comparing in an array with ints
- 01-02-2012, 04:32 PM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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.
Here's the driver to run this..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 } }
I've also attached the txt file that the scanner reads from. I'd greatly appreciate your help. ThanksJava 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 } }
- 01-02-2012, 04:52 PM #2
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?
Use the ==, !=, etc operators to compare primitive values.with ints, but I don't know what to useLast edited by Norm; 01-02-2012 at 04:57 PM.
- 01-02-2012, 06:11 PM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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.
- 01-02-2012, 06:27 PM #4
Re: Comparing in an array with ints
Time to go back to chapter one in your text and read about boolean expressions.
http://docs.oracle.com/javase/tutori...bolts/op2.html
Expressions, Statements, and Blocks (The Java™ Tutorials > Learning the Java Language > Language Basics)
Similar Threads
-
Comparing elements of an array
By fluteattack in forum New To JavaReplies: 1Last Post: 05-09-2011, 05:43 PM -
reading in unsigned ints into a 2D array
By newToIt in forum New To JavaReplies: 9Last Post: 03-06-2009, 12:36 PM -
comparing elements in array
By garyscott101 in forum New To JavaReplies: 14Last Post: 12-10-2008, 03:01 PM -
comparing array elements
By Jeremy720 in forum New To JavaReplies: 2Last Post: 10-13-2008, 02:33 AM -
comparing array using character
By Anseki in forum New To JavaReplies: 7Last Post: 10-03-2008, 07:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks