• Post Reply Bookmark Topic Watch Topic
  • New Topic

error compiling this  RSS feed

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so just for fun i wrote this which (after it compiles) should take what the user enters and say "You have a blue mercedes eclass that has 300 horsepower and 19 inch wheels". - or whatever the user enters...

but i'm getting compiler errors starting with my first println asking the user for the first input and i'm not sure why. it looks like any other println that i've written so i'm confused as to why an error would be there.

any ideas?

 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why you're getting an error on the first println, but one thing I see wrong is your get statements. You're not returning the class attributes, you're instead returning the parameters declared in the CarAttributes function. You're also not using the object myCar in your last println statement.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please cut/paste the exact wording of the error message.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiler errors will tell you exactly what's wrong. you should learn to read them, not just ignore them.

But my guess is that it is complaining because you have executable statements that aren't in a method.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're creating duplicate variable names. You leave out the type of the reference the second time. IE

String color;
color = "red";
 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:You're creating duplicate variable names. You leave out the type of the reference the second time. IE

String color;
color = "red";


I didn't notice that until you pointed it out.

You declare the variables twice, once at the beginning of CarAttributesTesster and again in the code where you're assigning the values in CarAttributesTester.
 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll also need to add a scan.nextLine after the second scan.nextInt to consume the newline character that will be left in the buffer.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here are the crap load of errors

File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 59]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 59]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 62]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 62]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 65]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 65]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 68]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 68]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 71]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 71]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: ')' expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: ';' expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: ';' expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: ';' expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: ';' expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: ';' expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: <identifier> expected
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: illegal start of type
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 75]
Error: <identifier> expected


fred rosenberger wrote:compiler errors will tell you exactly what's wrong. You should learn to read them, not just ignore them.

But my guess is that it is complaining because you have executable statements that aren't in a method.


if i was ignoring them would i have asked why i was getting an error on a println? very very new to this...by executable statements you mean?

You're creating duplicate variable names. You leave out the type of the reference the second time. IE

String color;
color = "red";


i figured since they were in different classes it may not matter, apparently not the case. thanks.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:
i figured since they were in different classes it may not matter, apparently not the case. thanks.


They aren't in different classes.

But I also completely missed that you don't have a main method.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:
David Borchgrevink wrote:
i figured since they were in different classes it may not matter, apparently not the case. thanks.


They aren't in different classes.

But I also completely missed that you don't have a main method.


the string names in CarAttributes aren't in a different class than the string names in CarAttributesTester? not getting defensive, just trying to learn

and ugh. now i really feel stupid. but thank you for pointing that out

 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:
the string names in CarAttributes aren't in a different class than the string names in CarAttributesTester?


I was referring to just the variables in CarAttributesTester, but if you have a main method what I said doesn't apply because the main method can have its own local variables of the same name.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so that code had more holes than swiss cheese. i was missing quite a few things. this is what i have now:



and these are the compiler errors i'm getting now:

File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 71]
Error: cannot find symbol
symbol: variable getpaint
location: variable myCar of type CarAttributes
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 71]
Error: cannot find symbol
symbol: variable getbrand
location: variable myCar of type CarAttributes
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 71]
Error: cannot find symbol
symbol: variable getmodell
location: variable myCar of type CarAttributes
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 71]
Error: cannot find symbol
symbol: variable gethpower
location: variable myCar of type CarAttributes
File: C:\Users\David\Desktop\Source Files\CarAttributesTester.java [line: 71]
Error: cannot find symbol
symbol: variable getwhlsize
location: variable myCar of type CarAttributes
 
Brian Schuetz
Greenhorn
Posts: 25
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Borchgrevink wrote:
Tyson Lindner wrote:
David Borchgrevink wrote:
i figured since they were in different classes it may not matter, apparently not the case. thanks.


They aren't in different classes.

But I also completely missed that you don't have a main method.


the string names in CarAttributes aren't in a different class than the string names in CarAttributesTester? not getting defensive, just trying to learn

and ugh. now i really feel stupid. but thank you for pointing that out



You are declaring the same variables twice in CarAttributesTester.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you just forgot the parenthesis for your methods
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so now i have it compiling and running ( i was forgetting the "()" after calling each method in the println. but for some reason when it gets to the 4th question - it asks it - but asks the 5th one immediate before you can answer the 4th...



> run CarAttributesTester
What color is your car?
[DrJava Input Box]
What size wheels do you have?
[DrJava Input Box]
How much horsepower does your car have?
[DrJava Input Box]
What brand car do you have?
What model car do you have?
[DrJava Input Box]
You have a blue camry that has 300 horsepower, and 18 inch wheels,
>


EDIT: thanks Tyson, i caught that too and was fixing whilst you were replying
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When dealing with a gaggle of complier errors, always concentrate on the first one. The rest may just be a result of the first error (though not in this case)

myCar.getpaint is a method. So what are you missing?

(Also, conventions are important, the method name should be getPaint -- get used to this convention now before you get to a point where it is a problem.)
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
weird....i moved the println asking for the make/brand above all the other println statements and now it works...



What brand of car do you have?
[DrJava Input Box]
What model car do you have?
[DrJava Input Box]
What color is your car?
[DrJava Input Box]
What size wheels do you have?
[DrJava Input Box]
How much horsepower does your car have?
[DrJava Input Box]
You have a blue toyota camry that has 300 horsepower, and 18 inch wheels,


so hooray! thanks for any and all help!

but one question, why would moving that println above the other ones have made a difference?
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to admit that last question stumped me for a bit.

Basically nextInt() skips over leading whitespace and return characters (and probably some others) but doesn't skip over trailing ones.

nextLine() on the other hand skips over everything but then returns everything it skipped over. So when you call nextInt() you're leaving the pointer in front of a return character. If you call another nextInt() you will skip over that return character to keep looking for an int and it will wait for more user input. But nextLine() will treat that character as the end of the line, so it will return everything before it (which is nothing), so the user never has a chance to enter anything. One solution is to just call another scan.nextLine() immediately after nextInt() but with no need to save the data (since its empty anyway).

Another solution is to just always use nextLine() and then parse all the strings that you need to be ints.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:I have to admit that last question stumped me for a bit.

Basically nextInt() skips over leading whitespace and return characters (and probably some others) but doesn't skip over trailing ones.

nextLine() on the other hand skips over everything but then returns everything it skipped over. So when you call nextInt() you're leaving the pointer in front of a return character. If you call another nextInt() you will skip over that return character to keep looking for an int and it will wait for more user input. But nextLine() will treat that character as the end of the line, so it will return everything before it (which is nothing), so the user never has a chance to enter anything. One solution is to just call another scan.nextLine() immediately after nextInt() but with no need to save the data (since its empty anyway).

Another solution is to just always use nextLine() and then parse all the strings that you need to be ints.


thank you! that made complete sense. and i didn't know i could use scan.nextLine with ints, doubles, floats etc... i thought it was a method only to be used with strings. good to know and thank you again!
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, did you figure out why moving of the lines to top works but they dont work when at bottom of questions ? I am curious myself, since i haven't read about Scanner().
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam, Tyson explained the answer 2 posts above yours.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!