• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone tell me whats wrong with this?

 
John Swain
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am making a Temperature conversion program. Its pretty simple but i keep getting these errors and i cant figure out what im doing wrong. Any advice? Here is my code
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these the errors you keep getting (with different line numbers)?[code]TempConversion.java:31: 'class' or 'interface' expected
}
^
TempConversion.java:33: 'class' or 'interface' expected
^
2 errors[/code}If so, remove the final two }s. If you line up each pair of open and close braces, it will be easier to tell when this happens. Once you fix that, you can address the other syntax errors.

Otherwise, don't keep us in suspence! Post the errors you get so people can help you. Not everyone can and wants to take the time to copy your code and compile it themselves. Make it as easy as possible for people to help to maximize your chance of getting help.
 
John Swain
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry that code is alittle in correct. The errors im recieving are: Assignment 2/TempConversion.java [28:1] incompatible types
found : java.lang.String
required: char
cUnit = keyboard.readLine();
and
Assignment 2/TempConversion.java [29:1] char cannot be dereferenced
if (cUnit.equalsIgnoreCase("c"))

I dont understand why i get these errors. Please help. I have attatched my code the way its supposed to look.

 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
readLine() returns a String and equalsIgnoreCase() is a method of String, yet you have declared cUnit as a char. The first error means you cannot assign a String to a char. The second means that you cannot call methods on primitive types like char and int.
 
John Swain
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, I changed equalsIgnoreCase() to ((cUnit == 'f') || (cUnit == 'F')). there are no errors for that. I am getting confused on how to get keyboard input for type char.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use readLine() to get a String and pull off the first character (make sure there is one!) with charAt(int). I recommend familiarizing yourself with the JavaDocs for the String class as it will be very important in your coding.

There are ways to respond to the first key press without waiting for the user to hit enter, but only go down that road if you must for this assignment.
 
John Swain
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help again. I looked it up in another book and i found: cUnit = (char) System.in.read(); this seems to work. Just one more question, after i compile and run the program i get the error stating: java.lang.NoClassDefFoundError: Assignment
Exception in thread "main"

any ideas what this means?
thanks once again.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, InputStream.read() will return the next byte, usually an ASCII character, so you should be okay with that for a simple program.

You need to run your program using the name of the class:It looks like you are not specifying a package name in the class, so that's all you should need. Make sure that directory has a TempConversion.class file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic