• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accepting user input as a integer?

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write a program that lets people input the amount of packages they want, and then apply a discount depending on the amount they order. I started to write this code and I am not sure how what do to do next. On my current code I am getting a errors on lines 6 and 7 that say

" [line: 6] Error: Type mismatch: cannot convert from java.lang.String to int"

" [line: 7] Error: The operator <= is undefined for the argument type(s) boolean, int"

I know I am doing something wrong but I only know how accept user input through a string. I am not sure how to convert this to a integer. Can someone please help me?


 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few ways of doing this (seeing your code would allow us be more specific with our help) such as using a Scanner to read in the user input and call it's getNextInt() method or you could use the Integer classes parseInt() method.
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow I can't belive I forgot to post the code... Sorry about that here it is.

 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store the returned value in a String variable and use the Integer.parseInt() method to convert it to an int.

Your if statement needs looking at - is there supposed to be "and" logic in there somewhere.
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Store the returned value in a String variable and use the Integer.parseInt() method to convert it to an int.

Your if statement needs looking at - is there supposed to be "and" logic in there somewhere.



Here is the new code that I made

Now it says that I am getting a error on the "and" and it tells me to delete it. How can I fix this? Also is my logic for applying the discount correct? I think it is but I am still a beginner and I am not sure.
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to stop and think carefully about each line of code before you write the next line - you should also compile and run your code after every new line or two.

If I was you I would delete everything after and add a line printing out the value of 'userinput' ie Then compile the code and if it compiles run it and see what happens.
Once you are able to input text and print it to the console then add the line converting it to an int and print out the int value. Once that works add the if statement and suitable print statements and run the code for several inputs to make sure your logic is correct etc etc
 
Campbell Ritchie
Marshal
Posts: 52549
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using option panes in the first place? That is very old‑fashioned programming. Many of use consider that has been superseded by the Scanner class.
 
Lars Englund
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first, you cannot calculate or even compare anything when it still sits in a String.
Therefore you will need to convert it to something else.
An integer is a poor choice here since you cannot store the value 0.2 nor correctly store the result from any calculation involving 0.2 in it.
A floating point value of some sort is better hence the doubles.

I took the liberty of beating your code up a bit, have a look and see if you can see what is improved.
What you are trying to achieve is a bit unclear but at least it compiles.

The if statement might need some extra explaining.
You cannot assume that the comparisons will be against a variable twice, you will have to specify it both times.
The two ampersands are the AND operator that separates the two comparisons or expressions using a better word.
They can come out as either true or false. In this case both must be true in order to execute the discount modification.
Which is why you will have to use the variable twice.

I also assumed that discount was percent that will be off the value so I subtracted from 1.
I do not, however, think the discount should come off the quantity as much as the price?

Hope this helps a bit.

 
Campbell Ritchie
Marshal
Posts: 52549
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Beware of using relational or equality operators on floating‑point values. The >= test can fail unexpectedly because of the imprecision of floating‑point arithmetic.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!