Results 1 to 10 of 10
- 04-26-2009, 02:48 AM #1
[SOLVED] Can anyone explain this error?
Java Code:Note: C:\Users\Mark\Documents\Desktop\Programming2\Assignmentattempt\TestCarHire.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Tool completed successfully
Its wrecking my head -
Could you also tell me if it will affect my program?
- 04-26-2009, 03:09 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
It's a warning, not an error. (Maybe you truncated that bit when you posted the message. Or if whatever you used to compile the code didn't clearly report it as a warning then throw that IDE away.)Can anyone explain this error?
Anyway, the compiler will explain. Just do what it says: recompile using adding -Xlint:unchecked then it will ping the offending line as an error and will tell you what is wrong with it.
Or read about Generics in Sun's Tutorial. The warning message is discussed in the section on erasure.
Either way you could also construct and post a simple program illustrating the problem.
-
What happens when you recompile the program with the -Xlint:unchecked suggested switch?
edit: too slow!
- 04-26-2009, 03:30 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Shouldn't do your work for you, I guess. But here is an example of the problem. Lifted from the pdf tutorial that came out with 1.5 (when generics were introduced).
This code compiles fine, although it gives the unchecked warning at the place indicated. But if you read the code, you'll see that its crazy. You put an Integer into a list and pull it out as a String.Java Code:import java.util.LinkedList; import java.util.List; public class Erasure { public static void main(String[] args) { String result = loophole(42); System.out.println("The result was(n't): " + result); } private static String loophole(Integer x) { List<String> ys = new LinkedList<String>(); List xs = ys; xs.add(x); // compile-time unchecked warning return ys.iterator().next(); } }
Now, if you could change the type like this with banknotes, you'd become a millionaire. Or receive a long jail sentence. It's just not going to work. You'll get a runtime error message (run it to see what and where).
The point of the warning is to ... warn ... you at compile time of the potential for problems that could occur at runtime.
Edit: I should add it came from section 6.2 of that Tutorial because I realise no-one has said whether it is fatal for your code or how (or if) your code should be changed to deal with the unchecked warning.Last edited by pbrockway2; 04-26-2009 at 03:40 AM.
- 04-26-2009, 03:51 AM #5
Àh sorry Im a real Noob to this.
What ive gathered so far is this is not an error - more a warning?
Apologies for the thread title if thats the case. \thanks for the help guys :)
-
I don't think that this is a crime yet.
usually (I think always) errors halt your program but warnings don't. Does your code run?What ive gathered so far is this is not an error - more a warning?
Sure beats the ubiquitous title: "Help!"Apologies for the thread title if thats the case. \thanks for the help guys :)
- 04-26-2009, 07:04 AM #7
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0
> What ive gathered so far is this is not an error - more a warning?
Yep, it's a warning... Post your code (if it's short) and we can give you some pointers on fixing your specific problems.
- 04-26-2009, 10:59 PM #8
@ Fubarable yeh it runs.. After I compile it - I need to run it twice, before it will work, dont know why
Ok here it isYep, it's a warning... Post your code (if it's short) and we can give you some pointers on fixing your specific problems.
Base File
And then the test fileJava Code:class CarHire{ private String type; private int year; private String reg; private int days; public double cost; CarHire(){ type = "Toyota Glanza"; year = 2008; reg= "08-d-0000"; days = 0; cost = 0; } CarHire(String t, int y, String r, int d, double c){ type = t; year = y; reg = r; days = d; cost = c; } public void display(){ System.out.println("\n\n\n\t\t\tType : " + type); System.out.println("\t\t\tYear : " + year); System.out.println("\t\t\tRegistration number : " + reg); System.out.println("\t\t\tAmount of days : " + days); } public void cost(){ cost = 40*days; if(year < 2005) cost = 30 * days; else if(days > 6) cost =(40 * days) - (40 * days /100*9); else if(days > 6 && year < 2005) cost=(30*days)-(30 * days / 100*9); System.out.println("Cost" + cost + "0"); } // Access / Modifier Methods // Get / Set Methods public String getType(){ return type; } public void setType(String t){ type = t; } public int getYear(){ return year; } public void setYear(int y){ year = y; } public String getReg(){ return reg; } public void setReg(String r){ reg = r; } public int getDays(){ return days; } public void setDays(int d){ days = d; } public double getCost(){ return cost; } public void setCost(double c){ cost = c; } }
Java Code:import java.util.*; import java.io.*; //import java.awt.*; class TestCarHire { public static void main(String[]args) { Vector v = new Vector(); loadvector(v); char ans; char again=0; System.out.println(); System.out.println(); System.out.print("\t------------------- MAIN MENU ------------------------- "); do{ System.out.println(); System.out.println("\n\t\t\tA : HIRE A CAR"); System.out.println("\t\t\tB : DELETE A TRANSACTION"); System.out.println("\t\t\tC : MODIFY A TRANSACTION"); System.out.println("\t\t\tD : DISPLAY ALL"); System.out.println("\t\t\tE : SAVE"); System.out.println("\t\t\tF : QUIT"); do{ ans = Keyboard.readChar(); ans = Character.toUpperCase(ans); }while(ans!= 'A' && ans!= 'B' && ans!= 'C' && ans!= 'D' && ans!= 'E' && ans!= 'F'); //////SWITCH STATEMENT/////// switch(ans) { case 'A': hirecar(v);break; case 'B': delete(v);break; case 'C': modify(v);break; case 'D': display(v);break; case 'E': save(v);break; case 'F': System.out.println();break; } if( ans != 'F') again = again(); } while (ans!='F' && again == 'Y'); System.out.println("Thank you,,Safe Driving"); } static void loadvector(Vector v){ String str; StringTokenizer t; try { File f1 = new File("Cars.txt"); /////Used to establish a path to specific file FileReader fr = new FileReader(f1); //////Used to create a file stream from that file object BufferedReader in = new BufferedReader(fr); /////slows down the stream so its can be read in lines str = in.readLine(); // Read the first line of the file while(str!=null) // Continue as long as data there { CarHire c = new CarHire(); t = new StringTokenizer(str); ///<<<---- passes string to tokenizer String ty = t.nextToken(); /////////changes lines into tokens(words) c.setType(ty); int y = Integer.parseInt(t.nextToken()); //////converts intergers to strings so that they can be tokenised c.setYear(y); String r = t.nextToken(); c.setReg(r); int d = Integer.parseInt(t.nextToken()); c.setDays(d); Object o = (Object) c; v.add(c); str = in.readLine(); } in.close(); // Closes the file }catch(IOException e) //////Cathches the exceptions {System.out.print("Non-Existant File");} System.out.println(); System.out.print("\t\t\t FILE LOADED!!!!"); } static void hirecar(Vector v){ CarHire c = new CarHire(); String t = new String(); int y = 0; String r = new String(); int d = 0; System.out.print("\t\t\tEnter the type: "); t = Keyboard.readString(); c.setType(t); System.out.print("\t\t\tEnter the year: "); y = Keyboard.readInt(); c.setYear(y); System.out.print("\t\t\tEnter the reg: "); r = Keyboard.readString(); c.setReg(r); do { System.out.print("\t\t\tEnter the days: "); d = Keyboard.readInt(); c.setDays(d); if (d < 1) System.out.println("You must hire a car for at least 1 day"); else if(d >10) System.out.println("You cannot rent a car for more than 10 days"); }while(d <1 || d >10); c.display(); System.out.print("\t\t\t"); c.cost(); v.add((Object) c); ////adds to the object within the vector } static void delete(Vector v){ String reg = new String(); char a; System.out.print("Enter your registration number :"); reg = Keyboard.readString(); for(int i =0; i < v.size(); i++) /////searches through the vector { Object o = v.elementAt(i); ///////// finds obeject at i CarHire c = (CarHire)o; ////upcasts if(c.getReg().equals(reg)){ c.display(); System.out.print("Do you wish to delete this transaction y / n? "); a = Keyboard.readChar(); a = Character.toLowerCase(a); if(a!= 'y' && a!= 'n') System.out.println("Invalid please enter Y or N"); else if(a == 'y') v.removeElementAt(i); /// removes the element from the VECTOR System.out.println("Your transaction has been deleted"); } } } static void modify(Vector v){ String reg = new String(); char a; System.out.print("Enter your registration number :"); reg = Keyboard.readString(); for(int i =0; i < v.size(); i++) { Object o = v.elementAt(i); ///////// finds obeject at i CarHire c = (CarHire)o; if(c.getReg().equals(reg)){ c.display(); System.out.print("Would you like modify this transaction y / n? "); a = Keyboard.readChar(); a = Character.toLowerCase(a); if(a!= 'y' && a!= 'y') System.out.println("Invalid please enter Y or N"); if(a =='y') v.removeElementAt(i); ////removes the element you searched for hirecar(v); save(v); } } } static void display(Vector v ){ for(int i=0;i <v.size(); i++) { Object o = v.elementAt(i); CarHire c = (CarHire)o; c.display(); System.out.print("\t\t\t"); c.cost(); } } static void save(Vector v){ try{ FileWriter f1 = new FileWriter("Cars.txt"); PrintWriter dataFile = new PrintWriter(f1); /////print writes effectivly same as system.out.print for(int i=0; i<v.size(); i++) ////////for loop searches through the vector { CarHire c = new CarHire(); c = (CarHire) v.elementAt(i); dataFile.print(" " + c.getType()); /////prints out the variable dataFile.print(" " + c.getYear()); /////prints out the variable dataFile.print(" "+ c.getReg()); /////prints out the variable dataFile.print(" " + c.getDays()); /////prints out the variable dataFile.println(" " + c.getCost()); /////prints out the variable } dataFile.close(); /////closes the file System.out.print("\t\t\tSAVE Complete"); } // end try catch(Exception e){ e.printStackTrace(); } // end catch System.out.println(); } // end save static char again(){ /////////prompts the user back to the main menu char answer; do{ System.out.println(); System.out.print("Do you wish to return to the main menu 'Y' or 'N': "); answer = Keyboard.readChar(); answer = Character.toUpperCase(answer); if(answer!= 'Y' && answer!= 'N') System.out.println("Invalid please enter Y or N"); }while (answer!= 'Y' && answer!= 'N'); return answer; } } /////END OF PROGRAM
-
Yep, it seems that most all of your warnings are due to using non-generic Vectors. You can solve this by using Vector<CarHire> in most of the spots where you declare plain Vectors. This would allow you to get rid of your CarHire casts on your vectors.
- 04-27-2009, 12:01 AM #10
Similar Threads
-
Please explain Java
By MarkWilson in forum New To JavaReplies: 7Last Post: 07-02-2008, 08:38 AM -
Need Help Can anyone explain what this means
By Clemenza1983 in forum New To JavaReplies: 6Last Post: 02-16-2008, 03:13 AM -
Can anyone briefy explain what does that mean?
By Clemenza1983 in forum New To JavaReplies: 6Last Post: 01-29-2008, 07:05 AM -
Iam new in Java Please explain to me
By vinaytvijayan in forum AWT / SwingReplies: 1Last Post: 12-30-2007, 11:35 AM -
need to explain this code
By reached in forum New To JavaReplies: 3Last Post: 12-03-2007, 10:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks