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

program crashes on letter  RSS feed

 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys
a newbie to java here and this is first post
I am trying to create a simple program which asks the user to enter a number from 1 to 10
if he enters it , then the program says valid
if he enters above 10, program says invalid

Now so far I have no problems
now the program needs to say invalid if I enter characters or a word

this is where I am stuck
how do you write something like that because when I enter a letter , the program crashes

here is what I got so far

 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using a Scanner? A long time ago Rob Spoor pointed out that you can wrap a nextXXX call in a loopBeware if you are using nextLine after nextAnythingElse.

And welcome to the Ranch
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vin, welcome to Java Ranch! In the future, when you post code examples, please UseCodeTags (⇐click). They make code much easier to read. I've added them for you here.

I think the best way to handle your problem would be to use a try-catch. Have you studied about that yet?
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have a look at the javadoc for Scanner and see exceptions could be thrown by the method you called. Maybe there's something you can do about those error cases. It may also come in handy to see what other methods Scanner offers you. Maybe one of those can help you figure out whether or not there really is a number in the input stream.

EDIT: Drat! Twice beaten to an answer.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys
Only doing java for about a week
Still figuring out how to input what campbell Ritchie wrote in my equations
sorry Greeg..first time in this forum so I will try to use code tags
Jason, going through the scanner glossary but so far no luck
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Hi Vin, welcome to Java Ranch! In the future, when you post code examples, please UseCodeTags (⇐click). They make code much easier to read. I've added them for you here.

I think the best way to handle your problem would be to use a try-catch. Have you studied about that yet?


no sorry didn't do that yet
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vin Ash wrote:no sorry didn't do that yet

Hmmm. Seems odd that your tutor would give you a task like this then - but maybe s/he's trying to see if you can work out how to do it without a try...catch.

Here's one suggestion:
1. Get a String from your user, instead of an int.
2. Go through that String, character by character, and see if it contains anything other than digits (0-9). You might also want to trim() it first.
3. If you find a character that is not a digit, display an appropriate message.
4. Otherwise, if all the characters ARE digits, convert the String to an int (Hint: have a look at Integer.parseInt(String)).

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vin Ash wrote:. . . Still figuring out how to input what campbell Ritchie wrote in my equations
sorry Greeg..first time in this forum so I will try to use code tags
Jason, going through the scanner glossary but so far no luck
Simply read the names of the methods for Scanner. You seem to want an int, so look for methods which supply an int. Remember you want hasNextXXX as well as nextXXX.

The appropriate hasNextXXX method does internally exactly what Winston suggests, so why write the code again? It is already written so use what somebody else has supplied rather than spending ages trying to write your own. If you really want to read it (and I bet you don't), somebody posted it a few weeks ago. Here. My technique (or more precisely Rob's) has the advantage that you never need to handle Exceptions. It all runs in a simple loop.

Winston and I have spent years disagreeing about Scanner, but there is one thing we do agree about. Tricks like that loop are poorly documented, if you can find them anywhere.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The appropriate hasNextXXX method does internally exactly what Winston suggests, so why write the code again?

Hmmm. Not quite. It's actually more like what Greg suggested, except that instead of throwing an Exception, it returns false.

Question: Shouldn't that loop be:I could be wrong (you know how confused I get with Scanner ) but it doesn't make sense to me to call nextXXX() when you already know it ain't gonna work.

Also: Doesn't RML (Rob's Magic Loop) print out an error message before you enter a number for the first time? And if not, why not

Winston
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right and I was mistaken. The 3rd line should read myScanner.next().

Sorry for my mistake. Since the print statement is inside the loop, you only get an error message when you write something which isn't an XXX.

The hasNextInt method appears to scan the input with a regex, then if it passes the regex passes it to Integer.parseInt. If you get an Exception, which means the input would be outwith the range of an int, return false. I would prefer to pass the input to a BigInteger constructor then compare the value with new BigInteger(String.valueOf(Integer.MAX_VALUE)) which will show that you are in range without risking an Exception. Similarly for MIN_VALUE.

But I didn't write the Scanner class.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys
Thanks to all
Thanks to Campbell and Winston for your help with the last posts but what you suggested , well it is not working or I am not doing it right
Only 2 weeks in java so I am still dumb to many things

So after banging my head for hours on end and reading left to right I managed to get it to not crash when I enter a letter but the problem now is that everything after the "while" line does not work properly
I still need it to write valid when I put any number from 1 to 10 and invalid when I go higher than 10
For any number it is writing valid
So any thoughts
Here is the new code

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You actually have a few errors ... so, I will give you a hint towards fixing one of them.

Hint... Where in your latest version of code, do you read the input into your num variable?

Henry
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this line reads

 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Hint... Where in your latest version of code, do you read the input into your num variable?


Another hint: the only way to assign a value to a variable looks like this:



So... do you see anything like that in the code you're working with? If not, then you aren't ever assigning a value to the input variable.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have yet to use "input" in my programs so far.
I thought when you put such (like in line 2 of the code) in a program that this was asking you the input or something similar.
 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah. It asks the user to input some data. But it doesn't automatically assign that data to a variable named "input", or any other variable. In that code fragment you assigned it to a variable named "nombre", however. So perhaps you should be using that variable instead of "input"? Or perhaps you need to write more code to ask the user to input something else which will be assigned to that variable?
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please format your code. Winston and I carefully showed you a code block with hasNextXXX occupying 6 lines. You have tried to squeeze it into one line. The compiler might guess what you are trying to do, and in this case it would guess correctly. But you are going to come back to this code next week and not be able to read what you have written. We have some suggestions about formatting. Your line 13 looks like this:-
            while(!clavier.hasNextInt()) clavier.next();
but it should look like this:-
while (!clavier.hasNextInt())
{
    clavier.next();
}
. . .

And I would suggest you add a line to print an error message inside the {} as well. It can go first or second; either will work. If you use K&R indentation your block will look like this:-
while (!clavier.hasNextInt()) {
    clavier.next();
}
. . .

Don't copy and paste any of that code.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I did some formatting but my problem is why is it not recognising my ifs because if
I enter 1 -10 it says valid and if I enter 42 it is supposed to say invalid but it says valid anyway
I fell it is doing the first if but the second not
Also can you give me an example of an input in java form
thanks everyone
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the line 18 to read else {. That is what you want. If then else. If it is valid, then otherwise.

And see what happens. I don't expect it to work.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes tried "else" a while back and it does not work
that is why I went with 2 "if" s
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what was the error message? else without if?

You must have exactly one statement between the if (...) and the else. Remembering that a block {...} counts as one statement, have another look at your code.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well
I did like you said but the same problem arises

When I put else it does not read the 1st "if"
when I put two "if"s , it only read the 1st if
the second one is invisible it seems

I tried taking off brackets and so on...no difference
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hint: pay attention to punctuation. Even an semicolon out of place (depending on the semicolon) can mess you up.

Henry
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vin Ash wrote:I tried taking off brackets and so on...no difference

Vin, TellTheDetails (←click).

Specifically: show us ALL your code as it is now, correctly indented and enclosed in '[code] ... [/code]' tags, along with any error messages you're getting exactly as they appear.

Trying to keep up with a narrative of all the changes you've made is virtually impossible.

Winston
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what it looks like now


 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You still haven't fixed the previous problem... the input hasn't been read into the "num" variable.

Henry
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
but I never used input before
how can I put it here or give me another example on how it is used in another code

thanks
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i = myScanner.nextInt();
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vin Ash wrote:
but I never used input before
how can I put it here or give me another example on how it is used in another code


That's not a true statement. You did it just fine in your earlier codes (included in this very topic). You just removed it in your later codes (not sure why though).

Henry
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys forgive me..only 2 weeks doing this
I thought int was integer
never thought of it as input
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An int is a kind of integer. There are other kinds of integer.
An int is not an input, but you can get input from a Scanner object pointing to System.in. There are lots of other ways to get input. A Scanner can turn the input into an int automatically without your having to worry about the details.
 
Vin Ash
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys
Thanks for all the help
I managed to resolve the problem finally
With what input you gave me and some reading here and there
was able to make the program work
here is the code
thanks again everybody
 
Liutauras Vilda
Marshal
Posts: 4635
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vin Ash,

Think about the case what would happen if user would enter 0, -1, -2, ...? What would be printed out "Valid", "Invalid"?
Think about, maybe you could change second if to something different?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!