Extreme noob in java in need of help

andrewbabcock

Senior member
Oct 2, 2005
561
0
0
I need to call a method in an if statement. In other words, when specific characters are typed, a specific method will be called. I don't know what the problem is but i can't do it. I think one of the problems is that i am using TextIO, a class that allows a window for input with many methods. Again i am an extreme noob but i can't figure out why i can't call these methods. For the first if statement, it calls the method and does what its suppossed to but the next if statement doesn't work and when the other specific characters are typed it just calls the method in the first if statement. Any help is appreciated and i need this fast...
 

andrewbabcock

Senior member
Oct 2, 2005
561
0
0
Originally posted by: MrChad
It would be immensely helpful if you used the attach code feature.

Well i am a bit embarassed to post my code... I should be a lot better than this at java.
 

andrewbabcock

Senior member
Oct 2, 2005
561
0
0
Underneath the "if meters", when the user types "meters", a method from the Meters class will convert the meters into feet and yards. How do i call that method from here?
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
You're making a very common mistake when it comes to String comparisons in Java.

if (sourceunit == "meters") is not correct. What you need to use is either the equals(), equalsIgnoreCase(), compareTo() or compareToIgnoreCase() method of the String class. You can read more about them here.

I'm not familiar with the TextIO classes, but if you change your code to read:

if (sourceunit.equals("meters")){

}

you might have better luck.
 

andrewbabcock

Senior member
Oct 2, 2005
561
0
0
Originally posted by: MrChad
You're making a very common mistake when it comes to String comparisons in Java.

if (sourceunit == "meters") is not correct. What you need to use is either the equals(), equalsIgnoreCase(), compareTo() or compareToIgnoreCase() method of the String class. You can read more about them here.

I'm not familiar with the TextIO classes, but if you change your code to read:

if (sourceunit.equals("meters")){

}

you might have better luck.

Thanx for that but that doesn't answer the questions what type of method should convert be and how should i call it under the "if" statement?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: MrChad
Also, read more about if statement syntax here.
Ftw. I'd recommend reading the entire trail. I can barely even read the posted code and it will take a lot of changes before a compiler can either.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
I'm not sure what you're trying to use convert() for.

You can build your methods any way you like. Personally, I would have two methods, convertToFeet() and convertToYards(). Both methods would accept an int (or perhaps double) parameter and return an int or double result. In your main program, you prompt the user for value in meters, then call your convertToFeet() and convertToYards() methods and output the results.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: kamper
Originally posted by: MrChad
Also, read more about if statement syntax here.
Ftw. I'd recommend reading the entire trail. I can barely even read the posted code and it will take a lot of changes before a compiler can either.

Yeah that's a good point. It really does sound like you're struggling with some basic programming concepts. The Java Tutorial is very helpful for beginner programmers, and it will definitely help you understand these problems better.
 

duragezic

Lifer
Oct 11, 1999
11,234
4
81
Just a note, Java's Scanner class might be easier to use to grab words the users types. I've seen it used a lot by others and the couple of times I used it on my programs it was easier than whatever I was using before (StreamTokenizer possibly?). Output to the console/terminal window can be done with System.out.println("Output this on a new line"); or System.out.print("output this on the same line");

You need to read up on the basic syntax that MrChad linked.. your if statements are wrong (and I'm not entirely sure what some of the brackets are doing... maybe you meant to have them part of an if statement), so that's why they are not called, but with .equals() and correct if statements, you will make some progress.

Also you probably need to have some import statements at the top depending what Java classes you will use.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |