Results 1 to 20 of 37
 02262011, 01:02 AM #1Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
Every Input File I Use Returns "Unsorted"
Bottom line, I have to input two files (with polynomials), ensure they are sorted correctly, and merge them into one sorted output file. The code below compiles and runs smooth; however, no matter what I input, the files come back unsorted. All of the input files were provided and are supposed to work (except one, but that's not the problem). Here's my code:
Java Code:import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class Project1 { public static void main(final String[] args) throws FileUnsorted, IOException { Scanner scanner = new Scanner(System.in); String inputFile1; String inputFile2; System.out.println("Enter first file name with extension: "); inputFile1 = scanner.nextLine(); System.out.println("Enter second file name with extension: "); inputFile2 = scanner.nextLine(); try { BufferedReader file1 = new BufferedReader(new FileReader(inputFile1)); BufferedReader file2 = new BufferedReader(new FileReader(inputFile2)); checkSorted(file1); checkSorted(file2); } catch (FileUnsorted ex) { System.exit(1); } } private static BufferedReader checkSorted(BufferedReader fileName) throws FileUnsorted, IOException { Polynomial p1 = null; Polynomial p2 = null; String line; while ((line = fileName.readLine()) != null) { p2 = new Polynomial(line); if (p1 != null) { if (p1.compareTo(p2) > 0) { throw new FileUnsorted(); } } p1 = p2; } return new BufferedReader(fileName); } }
Java Code:import java.io.BufferedReader; import java.io.IOException; import java.util.Scanner; public class Polynomial implements Comparable<Polynomial> { int[] coef; int deg; public Polynomial input(BufferedReader fileName) throws IOException { return new Polynomial(fileName.readLine()); } public Polynomial(String polynomial) { Scanner line = new Scanner(polynomial); int coefficient = line.nextInt(); int deg = line.nextInt(); coef = new int[deg + 1]; coef[deg] = coefficient; deg = degree(); while (line.hasNextInt()) { coefficient = line.nextInt(); deg = line.nextInt(); coef[deg] = coefficient; } } public int degree() { int d = 0; for (int i = 0; i < coef.length; i++) { if (coef[i] != 0) d = i; } return d; } public String toString() { if (deg == 0) return "" + coef[0]; else if (deg == 1) return coef[1] + "x + " + coef[0]; String s = coef[deg] + "x^" + deg; for (int i = deg  1; i >= 0; i) { if (coef[i] == 0) continue; else if (coef[i] > 0) s = s + " + " + (coef[i]); else if (coef[i] < 0) s = s + " + " + (coef[i]); if (i == 1) s = s + "x"; else if (i > 1) s = s + "x^" + i; } return s; } public int compareTo(Polynomial arg) { if (coef.length != arg.coef.length) { return arg.coef.length  coef.length; } for (int i = coef.length  1; i >= 0; i) { if (coef[i] != arg.coef[i]) { return arg.coef[i]  coef[i]; } } return 0; } }
Java Code:public class FileUnsorted extends RuntimeException { public FileUnsorted() { System.out.println("File is not sorted."); } }
I cannot figure out if I need to look at the checkSorted method, compareTo method, or some other method. I've looked through the code many times, but cannot find the issue(s). I have a feeling its somewhere in one of the loops since I tend to mess those up.
Any guidance and/or direction is greatly appreciated.Last edited by Cod; 02262011 at 01:04 AM.
 02262011, 01:19 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
Why does checkSorted() return a BufferedReader? If it is supposed to check something just have it return boolean (ie sorted or not). Also its a good idea to close streams when you're finished with them.
As to your question, I'd start by seeing what comparison is resulting in the conclusion that the file is unsorted. Ie, in checkSorted() print p1 and p2 and the result of the comparison. If you see a result from compareTo() that is not what you would expect for a given pair of polynomials you can be sure that it is the Polynomial class that is wrongly implemented and start to look at that.
(If it turns out that compareTo() is misbehaving and you can't see why, it might be a good idea to describe the polynomial ordering you are trying to implement.)Last edited by pbrockway2; 02262011 at 01:22 AM.
 02262011, 01:30 AM #3Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
We met again pbrockway2 :)
It returns a BufferedReader because it was part of the shell which was provided.
Both p1 and p2 are coming out as the same value, 7, which is the first value of the first file. The value is correct since the following number in the first file is 0. So the first number of the output should be 7. So, when you say the Polynomial class is wrongly implemented, what does that mean? The call in the main method or checkSorted method are incorrect? I don't quite understand what you're saying.
Thanks again.
EDIT: For the compareTo() method, it should put the polynomials in ascending order.Last edited by Cod; 02262011 at 02:33 AM.
 02262011, 02:39 AM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
I'm not saying compareTo() in the Polynomial class is wrongly implemented: I'm just saying it might be. And the way to find out is to print the polynomials that are being compared.
What happens (what output) when you change check checkSorted() to
Java Code:private static BufferedReader checkSorted(BufferedReader fileName) throws FileUnsorted, IOException { Polynomial p1 = null; Polynomial p2 = null; String line; while ((line = fileName.readLine()) != null) { p2 = new Polynomial(line); if (p1 != null) { [b]System.out.println("Comparing " + p1); System.out.println("to " + p2); System.out.println("p1.compareTo(p2)>0 is" + (p1.compareTo(p2) > 0));[/b] if (p1.compareTo(p2) > 0) { throw new FileUnsorted(); } } p1 = p2; } return new BufferedReader(fileName); } }
Basically the idea is to see if compareTo() is returning the value you expect.
 02262011, 03:09 AM #5Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
Oh, I see what you're saying. Let me try that. I was putting the print statements after the p1 = p2 line, which now explains why I was getting the same result for both. I'll check it again right now.
EDIT: So I found out that 7 is comparing to 0. According to the first test file, the first two digits are 7 and 0. So shouldn't the program be reading the next line vs the next int?Last edited by Cod; 02262011 at 03:11 AM.
 02262011, 03:20 AM #6Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
By the way, two of the files I have to merge are attached to give you an idea as to what I'm trying to accomplish. I know what the output should be, but I just can't get the program to provide it.
 02272011, 01:14 AM #7Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
After some doing some digging into an old textbook, I found utilizing an array list may be better in the checkSorted method. However, I cannot figure out how to get p1 and p2 to update accordingly in the while loop. I'm sure there is more wrong, so any more assistance is greatly appreciated.
Updated code:
Java Code:public class Project1 { public static void main(final String[] args) throws FileUnsorted, IOException { Scanner scanner = new Scanner(System.in); String inputFile1; String inputFile2; System.out.println("Enter the first file name: "); inputFile1 = scanner.nextLine(); System.out.println("Enter second file name: "); inputFile2 = scanner.nextLine(); try { Polynomial[] p1 = checkSorted(inputFile1); Polynomial[] p2 = checkSorted(inputFile2); } catch (FileUnsorted ex) { System.exit(1); } } private static Polynomial[] checkSorted(String fileName) throws FileUnsorted, IOException { ArrayList alst = new ArrayList(); String line = ""; Polynomial p1 = null; Polynomial p2 = null; BufferedReader br = new BufferedReader (new StringReader(fileName)); while ((line = br.readLine()) != null) { alst.add(line); if (p1 != null) { if (p1.compareTo(p2) > 0) { throw new FileUnsorted(); } } p1 = p2; } return (Polynomial[])alst.toArray(new Polynomial[] {}); }
Again, any guidance is greatly appreciated.
EDIT: Here are the error messages I'm receiving (all runtime):
Java Code:[COLOR="red"]Exception in thread "main" java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at java.util.Arrays.copyOf(Arrays.java:2763) at java.util.ArrayList.toArray(ArrayList.java:305) at Project1.checkSorted(Project1.java:56) at Project1.main(Project1.java:24)[/COLOR]
Last edited by Cod; 02272011 at 01:17 AM. Reason: Added error messages.
 02272011, 01:58 AM #8Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
I have no clue about the second version of the code you posted.
In particular:
Java Code:BufferedReader br = new BufferedReader (new StringReader(fileName));
seems to be trying to get the data from the file name rather than the file's contents. That can't be right.

Back to what you said before that "7 is comparing to 0". (You didn't actually post the output or the code that produced it, so the following involves some guesswork)
Notice that the polynomial 0 is not in the file file1.txt. For reference the contents are
Java Code:7 0 4 1 1 6 5 2 6 1 1 6 5 2 6 1 4 0
which correspond (again, guesswork: you haven't specified the file format explicitly) to the polynomials
Java Code:7 4x x^6+5x^2+6x x^6+5x^2+6x+4
Again, add code to the check sorted method to see how come you end up comparing 7 (the first polynomial) with 0 (which isn't in the file).
Java Code:private static BufferedReader checkSorted(BufferedReader fileName) throws FileUnsorted, IOException { Polynomial p1 = null; Polynomial p2 = null; String line; while ((line = fileName.readLine()) != null) { p2 = new Polynomial(line); [b]System.out.println("p2 set to " + p2); System.out.println("(using line=" + line + ")");[/b] if (p1 != null) { [b]System.out.println("Comparing " + p1); System.out.println("to " + p2); System.out.println("p1.compareTo(p2)>0 is " + (p1.compareTo(p2) > 0));[/b] if (p1.compareTo(p2) > 0) { throw new FileUnsorted(); } } p1 = p2; } return new BufferedReader(fileName); } }
Be explicit about the situation that leads to the problem. Am I right in interpreting the polynomials from the file contents in the way I did? What are the criteria for saying one polynomial is "bigger" than another? What code are you using? What is the (actual and complete) output of that code?Last edited by pbrockway2; 02272011 at 02:01 AM.
 02272011, 02:26 AM #9Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
Java Code:p2 set to 7 (using line=7 0) p2 set to 0 (using line=4 1) Comparing 7 to 0 p1.compareTo(p2)>0 is true File is not sorted.
I'm lost.
 02272011, 02:49 AM #10Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
p1 is getting its value at the end of the first time around the loop (it becomes 7).
The weird bit is
Java Code:p2 set to 0 (using line=4 1)
because the line "4 1" (the second line in the file) should be the polynomial 4x not the polynomial 0.
Do you see this?
So we need to look at the constructor to see why the line
Java Code:p2 = new Polynomial(line);
is assigning the wrong polynomial to p2. Or, at any rate, is not initialising the polynomial properly.
The technique is the same: add some debugging info to see why the line "4 1" is not being converted into the polynomial 4x. (Ie the coef array should be [0,4]  I think  and the degree should be 2).
Java Code:public Polynomial(String polynomial) { [b]System.out.println("Polynomial constructor using: " + polynomial);[/b] Scanner line = new Scanner(polynomial); int coefficient = line.nextInt(); int deg = line.nextInt(); coef = new int[deg + 1]; coef[deg] = coefficient; deg = degree(); while (line.hasNextInt()) { coefficient = line.nextInt(); deg = line.nextInt(); coef[deg] = coefficient; } [b]System.out.println(" coef array set to " + Arrays.toString(coef));[/b] [b]System.out.println(" deg set to " + deg);[/b] }
 02272011, 03:15 AM #11Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
I understand what you're saying, but I cannot figure out why the output isn't spitting out right. The message from the last run I did is below:
Java Code:Polynomial constructor using: 7 0 coef array set to [7] deg set to 0 Polynomial constructor using: 4 1 coef array set to [0, 4] deg set to 1 File is not sorted.
 02272011, 03:25 AM #12Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
Your Polynomial class and the constructor both have variables deg. This is a bad practice as you think you are assigning to one of the variables but it is "hidden" by the other.
I would reccommend changing the Polynomial constructor so it uses a different local variable.
Java Code:public Polynomial(String polynomial) { [b]System.out.println("Polynomial constructor using: " + polynomial);[/b] Scanner line = new Scanner(polynomial); int coefficient = line.nextInt(); int [color=blue]d[/color] = line.nextInt(); coef = new int[[color=blue]d[/color] + 1]; coef[[color=blue]d[/color]] = coefficient; deg = degree(); // < this one is meant to set the instance variable while (line.hasNextInt()) { coefficient = line.nextInt(); [color=blue]d[/color] = line.nextInt(); coef[[color=blue]d[/color]] = coefficient; } [b]System.out.println(" coef array set to " + Arrays.toString(coef));[/b] [b]System.out.println(" deg set to " + deg);[/b] }
 02272011, 03:29 AM #13Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
New output:
Java Code:Polynomial constructor using: 7 0 coef array set to [7] exp set to 0 Polynomial constructor using: 4 1 coef array set to [0, 4] exp set to 1 File is not sorted.
 02272011, 03:32 AM #14Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
Oh, I meant to add that you should replace the debugging code you had in the other method. You'll need that again.
Do that and post the actual code you are using with its output.
 02272011, 03:40 AM #15Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
Here's what I came up with:
Java Code:Polynomial constructor using: 7 0 coef array set to [7] exp set to 0 p2 set to 7 (using line=7 0) Polynomial constructor using: 4 1 coef array set to [0, 4] exp set to 1 p2 set to 0 (using line=4 1) Comparing 7 to 0 p1.compareTo(p2)>0 is true File is not sorted.
Should p2 be set to the exponent and p1 set to the next line? I'm still stumped.
 02272011, 03:46 AM #16Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
I'm still stumped.
So am I because I have not seen your code. This exp variable is a total mystery, for instance. And other stuff. Please post the code that generated that output.
 02272011, 03:51 AM #17Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
Here's everything I got, along with the output at the bottom (in red):
Java Code:import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class Project1 { public static void main(final String[] args) throws FileUnsorted, IOException { Scanner scanner = new Scanner(System.in); String inputFile1; String inputFile2; System.out.println("Enter the first file name: "); inputFile1 = scanner.nextLine(); System.out.println("Enter second file name: "); inputFile2 = scanner.nextLine(); try { BufferedReader file1 = new BufferedReader(new FileReader(inputFile1)); BufferedReader file2 = new BufferedReader(new FileReader(inputFile2)); checkSorted(file1); checkSorted(file2); } catch (FileUnsorted ex) { System.exit(1); } } private static BufferedReader checkSorted(BufferedReader fileName) throws FileUnsorted, IOException { Polynomial p1 = null; Polynomial p2 = null; String line = null; while ((line = fileName.readLine()) != null) { p2 = new Polynomial(line); System.out.println("p2 set to " + p2); System.out.println("(using line=" + line + ")"); if (p1 != null) { System.out.println("Comparing " + p1); System.out.println("to " + p2); System.out.println("p1.compareTo(p2)> 0 is " + (p1.compareTo(p2) > 0)); if (p1.compareTo(p2) > 0) { throw new FileUnsorted(); } } p1 = p2; } return new BufferedReader(fileName); } }
Java Code:import java.io.BufferedReader; import java.io.IOException; import java.util.Arrays; import java.util.Scanner; public class Polynomial implements Comparable<Polynomial> { int[] coef; int deg; public Polynomial input(BufferedReader file) throws IOException { return new Polynomial(file.readLine()); } public Polynomial(String polynomial) { System.out.println("Polynomial constructor using: " + polynomial); Scanner line = new Scanner(polynomial); int coefficient = line.nextInt(); int exp = line.nextInt(); coef = new int[exp + 1]; coef[exp] = coefficient; exp = degree(); while (line.hasNextInt()) { coefficient = line.nextInt(); exp = line.nextInt(); coef[exp] = coefficient; } System.out.println(" coef array set to " + Arrays.toString(coef)); System.out.println(" exp set to " + exp); } public int degree() { int d = 0; for (int i = 0; i < coef.length; i++) { if (coef[i] != 0) d = i; } return d; } public String toString() { if (deg == 0) return "" + coef[0]; else if (deg == 1) return coef[1] + "x + " + coef[0]; String s = coef[deg] + "x^" + deg; for (int i = deg  1; i >= 0; i) { if (coef[i] == 0) continue; else if (coef[i] > 0) s = s + " + " + ( coef[i]); else if (coef[i] < 0) s = s + " + " + (coef[i]); if (i == 1) s = s + "x"; else if (i > 1) s = s + "x^" + i; } return s; } public int compareTo(Polynomial arg) { if (coef.length != arg.coef.length) { return arg.coef.length  coef.length; } for (int i = coef.length  1; i >= 0; i) { if (coef[i] != arg.coef[i]) { return arg.coef[i]  coef[i]; } } return 0; } }
Java Code:public class FileUnsorted extends RuntimeException { public FileUnsorted() { System.out.println("File is not sorted."); } }
Java Code:Polynomial constructor using: 7 0 coef array set to [7] exp set to 0 p2 set to 7 (using line=7 0) Polynomial constructor using: 4 1 coef array set to [0, 4] exp set to 1 p2 set to 0 (using line=4 1) Comparing 7 to 0 p1.compareTo(p2)> 0 is true File is not sorted.
I'll say it again, your guidance has and continues to be greatly appreciated. Slowly and surely I'm learning this stuff. Thanks again.
 02272011, 03:58 AM #18Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
OK. Using exp for the local variable is fine. But you changed it in a couple of other places where the instance variable deg was meant. The Polynomial constructor should be:
Java Code:public Polynomial(String polynomial) { System.out.println("Polynomial constructor using: " + polynomial); Scanner line = new Scanner(polynomial); int coefficient = line.nextInt(); int exp = line.nextInt(); coef = new int[exp + 1]; coef[exp] = coefficient; [b]deg = degree();[/b] while (line.hasNextInt()) { coefficient = line.nextInt(); exp = line.nextInt(); coef[exp] = coefficient; } System.out.println(" coef array set to " + Arrays.toString(coef)); [b]System.out.println(" deg set to " + deg);[/b] }
What output do you get then?
 02272011, 04:01 AM #19Member
 Join Date
 Nov 2009
 Location
 Honolulu, HI
 Posts
 59
 Rep Power
 0
Java Code:Polynomial constructor using: 7 0 coef array set to [7] deg set to 0 p2 set to 7 (using line=7 0) Polynomial constructor using: 4 1 coef array set to [0, 4] deg set to 1 p2 set to 4x + 0 (using line=4 1) Comparing 7 to 4x + 0 p1.compareTo(p2)> 0 is true File is not sorted.
 02272011, 04:13 AM #20Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,640
 Rep Power
 13
Progress! Now we are seeing the polynomials being set correctly. The zero is there becuase of how you implemented toString()
Java Code:else if (deg == 1) return coef[1] + "x + " + coef[0]; // < coef[1] is 4, and coef[0] is 0
But that's no big deal.
The problem is now seen as being that when you compare the polynomial 7 to the polynomial 4x you get a number bigger than zero. (This is the traditional way of reporting the "fact" that 7>4x). Now do you expect 7 to be bigger than 4x? If not  if you expect 4x to be bigger because it has a higher degree  then you have to look at that compareTo() method to see if it is doing the right thing.
And what, you ask, is the right thing? Here it is from the Comparable API docs: "Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object". Check your compareTo() to make sure it is doing that and not ... being ass backwards with those subtractions.
Similar Threads

Getting access denied error while importing file using input type="file" with IE7
By sarang1 in forum Advanced JavaReplies: 6Last Post: 02102011, 10:55 AM 
trouble with Scanner(new File("input"));
By ronyosi in forum New To JavaReplies: 9Last Post: 10272010, 11:34 PM 
getDisplayLanguage returns "en" not "English"
By DD70 in forum New To JavaReplies: 6Last Post: 08122009, 11:22 PM 
Count lines cointaining "word" in input file
By gwithey in forum New To JavaReplies: 5Last Post: 04022009, 05:23 AM 
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10202008, 07:35 AM
Bookmarks