This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

input  RSS feed

 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get these methods to work right. User inputs Tsubo or square feet and a number so it can convert to the other. I have no more hair to pull out.


i'm also learning parallel.js in a meteor.com environment, along with a lot of other things. Maybe an information overload.


 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried
keyboard.nextDouble() ?

Note that 'float' is very rarely used. Use doubles instead.

"Enter Tsubo or square feet.", how is your program going to determine which of Tsubo or square feet has been entered?
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If else statements based on Scanner keyboard = new Scanner(System.in);
I have yet to get a response from my tutor.
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


gets...

run:
Enter which you want to convert from Tsubo to square feet or from square feet to Tsubo.
Enter Tsubo or square feet.
Tsubo
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at javaapplication7.JavaApplication7.main(JavaApplication7.java:31)
C:\Users\Jeremy Olsen\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 16 seconds)


 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. I hope I'm going in the right direction.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your posts. Always use the tags: doesn't it look better.
Remove the following two keywords from your vocabulary:-
static float.
You will sometimes have to make things static, e.g. the main method, but if you don't know why something is static, it is a mistake. If you do it because the compiler complains about things non‑static, then it is still a mistake. You will never need to use the float datatype in a beginner's course. It has all the disadvantages of doubles but more so. Do all that arithmetic with doubles.
Don't give variables names starting with Capital Letters.

You cannot read a number and use it after if or while. You can only put booleans there. You might be able to use numbers in C or C++ like that but you can't in Java┬«. Only booleans. You might be able to get that to work if you add != 0 but I can't actually see why you need that.

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Olsen wrote:...
Enter Tsubo or square feet.
Tsubo

Look at the output/input above and then look at your code.

You display a prompt and then call nextDouble(), but you enter "Tsubo" in response to the prompt. Does "Tsubo" look like a valid double value to you?

If you want to be able to enter "Tsubo" or "square feet" as a response, what method do you think you might need to call? Have a look at the Scanner API and see if you can find one that fits the bill.

HIH

Winston
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I'm working on boolean:




 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You're still reading in an int, and Tsubo will always be true.

What do you want the user to input after they are asked "Enter Tsubo or square feet."?
That should be your start point, then write a bit of code that can accept that user input.
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help.
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before I spend hours trying to get one if else statement to work right, is it possible to just use
one if else statement for getting input for Tsubo or square feet and then performing the conversion?
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have
keyboard.nextInt();
'nextInt()' returns an int. You are not assigning the int to anything or comparing to any thing, therefore the input value disappears. I don't think this is what you want.
Additionally, nextInt takes an integer string and won't accept 'Tsubo'.

You could change your prompt to
System.out.println("Enter (1) Tsubo or (2) square feet.")
Then an int would make sense.

boolean Tsubo = true;
if (Tsubo == true){

Think through what you are telling the computer to do. Assign true to variable name Tsubo. Test the Tsubo variable to see if it's true. Well, of course it will be true, you just set it.
You need to do something with the response to your prompt.
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I;m goin' craaaaaazy!!!
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have
keyboard.nextInt();
'nextInt()' returns an int. You are not assigning the int to anything or comparing to any thing, therefore the input value disappears. I don't think this is what you want.

You have to assign the return value to a variable or compare it to something.
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure you understand the way calling methods works. If a method has been declared to return void then nothing is returned, in all other cases something is returned from the method. You have to choose between: ignoring the return value, in which case it will be lost, assigning the return value to variable to be used later, or compare the returned value to something.

Look up your Scanner API documentation, what does its method nextInt() return? What do you want to do with it?
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have

There are a couple of problems with this.
The two methods you have written return doubles not ints.
The two methods both take an argument of type double which you have not supplied when you call them.
This code is placed in the beginning of the method before you've asked the user if they want Tsubo or square feet.

I suggest removing them at this time. You will need to rewrite these properly after you prompt the user.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You have

There are a couple of problems with this.
The two methods you have written return doubles not ints.
The two methods both take an argument of type double which you have not supplied when you call them.
This code is placed in the beginning of the method before you've asked the user if they want Tsubo or square feet.

I suggest removing them at this time. You will need to rewrite these properly after you prompt the user.

Sorry, they return void. You can't assign them to an int.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Olsen wrote:I;m goin' craaaaaazy!!!

Then it's time to stop. Turn off your computer; chill out; have a beer; play a video game and forget about this.

Right now you're just banging your head against the terminal and not thinking. And you will never, ever, EVER solve a programming problem that way.

Step #2: When you've calmed down and come back to this. DO NOT TURN YOUR COMPUTER BACK ON.

Get out lots of paper and a pencil and write down in English (or your native language) every single step that you want your program to do.

Do not; reapeat: DO NOT write one more line of Java code until you've done that.

HIH

Winston
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston, you make me laugh so hard. Thanks. That's exactly what I would have said to someone.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Olsen wrote:Winston, you make me laugh so hard. Thanks. That's exactly what I would have said to someone.

Glad to be of service

Winston
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I just got wires taken out of my neck that have been in there since I was 16
and my brain isn't working like it is supposed to. I just "knead" a few leaders
to get me writing code in the right direction. Thanks, my grades thank you too.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables on lines 23-26 aren't used anywhere, and are in fact, totally unnecessary. Don't add code unless you know what you need it for.

Your two methods, showTsubo and showSquaare_Feet, don't return anything. The reason you'd call them is for the side effect of printing the result of the calculation. This is OK.

Did you look up the documentation for Scanner.nextInt() ? This method returns an int. You have to do something with that int, e.g. compare it to '1' or '2'.
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is this not printing the answer ?
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yeeeeaah !!! Got it!!!
 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for putting up with my retardedness.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations!
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tweak. You might want this. It's a little more user friendly.
choice.equalsIgnoreCase("Tsubo")
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!