I am supposed to do a car rental program in java code but I have so many errors and every time I fix one, another that I fixed no longer is good. pulling my hair out here. can someone take a look at my code and give some pointers.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
Not sure what other problems there are, but you should never compare strings using the "==" operator, but always using the "equals" method.
Sara Bear wrote:I am supposed to do a car rental program in java code but I have so many errors and every time I fix one, another that I fixed no longer is good. pulling my hair out here. can someone take a look at my code and give some pointers.
When the compiler finds an error, it reports it, then it assumes the everything is okay, and continues looking for errors. This means that a compile error can actually triggers another compiler error that may or may not exist. This means that a compile error can also hide another compiler error.
So... as a pointer, I recommend that you only look at the first few errors. Fix the first few compile errors and then recompile. Do not try to fix a compile error when there is a previous one that has not been fixed. As you get better, you can do more and more before recompilng, and can quickly determine when a recompile is needed... but until then, just fix a few from the beginning only (if needed, only the first one) and recompile.
This would cause all sorts of false compile errors. I think if you add the ending bracket you'll get better results.
All I did was look at the brackets to see each beginning bracket have a matching ending bracket. When you work in an IDE it will flag it for you so that you can see something is missing or there is an extra ending bracket, etc. From the command line you're on your own to do that visually and to be thorough with it.
also...Never never never NEVER write this much code before you compile. I never write more than about three lines before I compile. That way, I know within three lines where the error has to be.
Never ascribe to malice that which can be adequately explained by stupidity.
By looking at your code the only thing I could suggest is you go back to the very basics of Java and start studying again. Not to say I'm advising this with no arguments, here is a list of some things that made me jump to that conclusion:
Line 01: Import statement is not correct, because Java is case sensitive. There is no java.util.scanner class.
Lines 10-18 (and some others in your code): Again, Java is case sensitive. There is no string type.
Lines 10-14: Initializing String variables to integer value?
Lines 20-24: This is not the way methods are invoked in Java.
Lines 26, 34, 55, 68: This is not the way methods are declared in Java (check the argument list).
Lines 31, 43, 65: This is not the way you return values from methods.
There might be some other error(s) I've missed, but I think you got the point. Don't move forward unless you're sure you understand the basics.
The quieter you are, the more you are able to hear.
Sara Bear
Greenhorn
Joined: Nov 05, 2012
Posts: 8
posted
0
i did put in the end closing bracket but still have 33 errors. I realize that one can make several false errors so I do always start at the top but I can not seem to clear up any.
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\bearsnakes\Desktop\PL\A07>javac CarRental.java
CarRental.java:26: error: ')' expected
getUserInfo(string name, sting age);
^
CarRental.java:26: error: illegal start of expression
getUserInfo(string name, sting age);
^
CarRental.java:26: error: ';' expected
getUserInfo(string name, sting age);
^
CarRental.java:26: error: not a statement
getUserInfo(string name, sting age);
^
CarRental.java:26: error: ';' expected
getUserInfo(string name, sting age);
^
CarRental.java:27: error: ')' expected
rental = displayRentalMessage(string age, rental);
^
CarRental.java:27: error: illegal start of expression
rental = displayRentalMessage(string age, rental);
^
CarRental.java:27: error: ';' expected
rental = displayRentalMessage(string age, rental);
^
CarRental.java:28: error: ')' expected
validateCeditCard(string creditCard);
^
CarRental.java:28: error: illegal start of expression
validateCeditCard(string creditCard);
^
CarRental.java:29: error: ')' expected
cost = calculateCost(string rental);
^
CarRental.java:29: error: illegal start of expression
cost = calculateCost(string rental);
^
CarRental.java:30: error: ')' expected
displayReceipt(string name, rental, cost );
^
CarRental.java:30: error: illegal start of expression
displayReceipt(string name, rental, cost );
^
CarRental.java:30: error: ';' expected
displayReceipt(string name, rental, cost );
^
CarRental.java:32: error: <identifier> expected
public string getUserInfo(string name,age) // get user name and
age
^
CarRental.java:37: error: ';' expected
return string name, age;
^
CarRental.java:37: error: not a statement
return string name, age;
^
CarRental.java:37: error: ';' expected
return string name, age;
^
CarRental.java:37: error: not a statement
return string name, age;
^
CarRental.java:40: error: <identifier> expected
public string displayRentalMessage(string age, rental) //determi
ne rental car based on age
^
CarRental.java:49: error: ';' expected
return string rental;
^
CarRental.java:49: error: not a statement
return string rental;
^
CarRental.java:59: error: not a statement
13 System.out.println("Credit Card number is not valid");
^
CarRental.java:59: error: ';' expected
13 System.out.println("Credit Card number is not valid");
^
CarRental.java:61: error: <identifier> expected
public string calculateCost(string rental, cost) // calculate co
st of the car
^
CarRental.java:69: error: not a statement
else (rental = NO_CAR)
^
CarRental.java:69: error: ';' expected
else (rental = NO_CAR)
^
CarRental.java:71: error: ';' expected
return string cost;
^
CarRental.java:71: error: not a statement
return string cost;
^
CarRental.java:74: error: <identifier> expected
public static void displayReceipt(name, rental, cost ) // displa
y the detail of what the end-user rented
^
CarRental.java:74: error: <identifier> expected
public static void displayReceipt(name, rental, cost ) // displa
y the detail of what the end-user rented
^
CarRental.java:74: error: <identifier> expected
public static void displayReceipt(name, rental, cost ) // displa
y the detail of what the end-user rented
^
CarRental.java:76: error: reached end of file while parsing
}
^
34 errors
C:\Users\bearsnakes\Desktop\PL\A07>javac CarRental.java
CarRental.java:26: error: ')' expected
getUserInfo(string name, sting age);
^
CarRental.java:26: error: illegal start of expression
getUserInfo(string name, sting age);
^
CarRental.java:26: error: ';' expected
getUserInfo(string name, sting age);
^
CarRental.java:26: error: not a statement
getUserInfo(string name, sting age);
^
CarRental.java:26: error: ';' expected
getUserInfo(string name, sting age);
^
CarRental.java:27: error: ')' expected
rental = displayRentalMessage(string age, rental);
^
CarRental.java:27: error: illegal start of expression
rental = displayRentalMessage(string age, rental);
^
CarRental.java:27: error: ';' expected
rental = displayRentalMessage(string age, rental);
^
CarRental.java:28: error: ')' expected
validateCeditCard(string creditCard);
^
CarRental.java:28: error: illegal start of expression
validateCeditCard(string creditCard);
^
CarRental.java:29: error: ')' expected
cost = calculateCost(string rental);
^
CarRental.java:29: error: illegal start of expression
cost = calculateCost(string rental);
^
CarRental.java:30: error: ')' expected
displayReceipt(string name, rental, cost );
^
CarRental.java:30: error: illegal start of expression
displayReceipt(string name, rental, cost );
^
CarRental.java:30: error: ';' expected
displayReceipt(string name, rental, cost );
^
CarRental.java:32: error: <identifier> expected
public string getUserInfo(string name,age) // get user name and
age
^
CarRental.java:37: error: ';' expected
return string name, age;
^
CarRental.java:37: error: not a statement
return string name, age;
^
CarRental.java:37: error: ';' expected
return string name, age;
^
CarRental.java:37: error: not a statement
return string name, age;
^
CarRental.java:40: error: <identifier> expected
public string displayRentalMessage(string age, rental) //determi
ne rental car based on age
^
CarRental.java:49: error: ';' expected
return string rental;
^
CarRental.java:49: error: not a statement
return string rental;
^
CarRental.java:59: error: not a statement
13 System.out.println("Credit Card number is not valid");
^
CarRental.java:59: error: ';' expected
13 System.out.println("Credit Card number is not valid");
^
CarRental.java:61: error: <identifier> expected
public string calculateCost(string rental, cost) // calculate co
st of the car
^
CarRental.java:69: error: not a statement
else (rental = NO_CAR)
^
CarRental.java:69: error: ';' expected
else (rental = NO_CAR)
^
CarRental.java:71: error: ';' expected
return string cost;
^
CarRental.java:71: error: not a statement
return string cost;
^
CarRental.java:74: error: <identifier> expected
public static void displayReceipt(name, rental, cost ) // displa
y the detail of what the end-user rented
^
CarRental.java:74: error: <identifier> expected
public static void displayReceipt(name, rental, cost ) // displa
y the detail of what the end-user rented
^
CarRental.java:74: error: <identifier> expected
public static void displayReceipt(name, rental, cost ) // displa
y the detail of what the end-user rented
^
33 errors
C:\Users\bearsnakes\Desktop\PL\A07>
Sara Bear
Greenhorn
Joined: Nov 05, 2012
Posts: 8
posted
0
i was able to knock it down to 11 errors yippee!!
CarRental.java:37: error: ';' expected
return string name, age;
^
CarRental.java:37: error: not a statement
return string name, age;
^
CarRental.java:37: error: ';' expected
return string name, age;
^
CarRental.java:37: error: not a statement
return string name, age;
^
CarRental.java:40: error: <identifier> expected
public string displayRentalMessage(string age, rental) //determi
ne rental car based on age
^
CarRental.java:49: error: ';' expected
return string rental;
^
CarRental.java:49: error: not a statement
return string rental;
^
CarRental.java:59: error: not a statement
13 System.out.println("Credit Card number is not valid");
^
CarRental.java:59: error: ';' expected
13 System.out.println("Credit Card number is not valid");
^
CarRental.java:61: error: <identifier> expected
public string calculateCost(string rental, cost) // calculate co
st of the car
^
CarRental.java:69: error: not a statement
else (rental = NO_CAR)
^
CarRental.java:69: error: ';' expected
else (rental = NO_CAR)
^
CarRental.java:71: error: ';' expected
return string cost;
^
CarRental.java:71: error: not a statement
return string cost;
^
CarRental.java:74: error: <identifier> expected
public static void displayReceipt(name, rental, cost ) // displa
y the detail of what the end-user rented
^
CarRental.java:74: error: <identifier> expected
public static void displayReceipt(name, rental, cost ) // displa
y the detail of what the end-user rented
^
CarRental.java:74: error: <identifier> expected
public static void displayReceipt(name, rental, cost ) // displa
y the detail of what the end-user rented
^
A method can only return one value, and you don't specify that value's type as part of the return statement. Look at any text or tutorial for examples.
Sara Bear
Greenhorn
Joined: Nov 05, 2012
Posts: 8
posted
0
I have redid my code, got it down to the last two errors, then got 42 more!!!
Sara Bear wrote:I have redid my code, got it down to the last two errors, then got 42 more!!!
In my opinion, you probably had all those errors all along. Remember that errors can hide other errors. So basically, you had errors that was so bad that the compiler got really confused. It wasn't until you fixed enough erors that the compiler was able to find the other errors.
Anyway, it looks like you removed the parameters to all of your methods -- which I assumed were to get rid of the parameter declaration compile errors. This was *not* a good idea because all you did was get rid of the parameters -- you kept all the code that used those parameters, the parameters that now no longer exist.
Henry
Paul Witten
Ranch Hand
Joined: Oct 10, 2012
Posts: 86
posted
0
Sara Bear wrote:I have redid my code, got it down to the last two errors, then got 42 more!
Remember the earlier advice, Sara: Solve the topmost problem first, and then recompile. Like recompile immediately. If you solve one problem it could cure a dozen others, so you don't necessarily have 42 more bugs. Read the output from the compiler from the topmost first and solve that problem, and only then proceed to recompile to see what the new topmost problem is.
It's much easier to compile after only writing a few key lines. Make sure that what you've written is legal. If you wait until you have a whole program you end up with a confused situation.
Paul Witten
Ranch Hand
Joined: Oct 10, 2012
Posts: 86
posted
0
Henry Wong wrote:In my opinion, you probably had all those errors all along.
This is bringing back memories of how I would write line after line after line and never compile until the bitter end. Then I'd be holding my breath in an anxious state. I was afraid to compile. What if there was .... gasp .... a problem? I felt like it was the Grim Reaper coming to ruin my show. I didn't like knowing what was wrong. I wanted it to just work, like using fuzzy-logic. I wanted a Fuzzy-Logic-Java that intuited what I was trying to do and give me a break here and there for being sloppy.
Later I became a Compile-a-holic, hehe. I wouldn't write ANYTHING without compiling it. If I made a mistake I wanted to know NOW.
Now I don't have to compile every line, but my trigger finger sure is itchy. Don't make a move or the sheriff gets it.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32667
4
posted
0
Welcome to the Ranch
Another thing, is you should be using code tags, which I shall add to one of your posts, if possible. That makes the post look a lot better, and also shows indentation problems. The compiler might not notice indentation, but we do, and correct indentation makes errors like unbalnced {} obvious. As well as only writing a few lines before compiling, you should use spaces, not tabs for indenting, and write backwards.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32667
4
posted
0
Have you been writing C# code, too? The {0} and {1} are not usual constructs in Java code. Also you cannot mix + and , as you are doing in that line. Read about how you format printing in Java, starting here.