Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Scanner Loop not stoping when Character input rather then Int

 
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is one such example I apologies, I could not find it.

If one introduces a character, the program should output the default.
Unless the user inputs int 3, program shall not exit.


 
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are testing for scanner.hasNextLine(). Did you mean hasNextInt() ? If it is not an int, how are you going to handle it?
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank you for being interested.
I have put scanner.hasNextLine() so it will get to the default before exiting.
If I press any int it will go to default,
if I press a char, it is caught butt he program ends.
I want to stay in the loop, rather the exit.
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should have said I tried that.
It still exits the loop.
It has to do with this line int choice = scanner.nextInt()

As soon as that field is not meet, the loop ends

Moved above the try{}catch{}, the exception is thrown and loop ends

I need a way to handle the character input, and go back in the loop if character is received
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put all this in place of where you have hasNextLine().
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the implementation
It give me one more chance, I guess I need another loop there

 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Put the entire block I gave you near the top instead of while( scanner.hasNextLine() ).
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realised that. Did not see the choice = scanner.nextInt();

It goes again, but when I input one of the choices, it exists.
If I introduce a char, throws exception
 
Marshal
Posts: 68113
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Lg Long wrote:. . .

You have misread what Carey told you. You need to get rid of the next token, which isn't an int. What you are doing in line 8 is reading something you know isn't an int as if it were an int. Then you suffer an exception and exit your loop. You need scanner.next(); in line 8. Then you finish the loop.
Don't catch an input mismatch exception. That form of loop ensures it will never be thrown, so you don't need the try‑catch.

I shall challenge you to get rid of the label in line 18; and the labelled break; . You can use two loops.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for welcoming.
This is my code.
It is completely broken.
Infinite loop
Conditions not working anymore.

What am i missing?! lol
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Put all this in place of where you have hasNextLine().


You haven't implemented this completely. You only put  in the first line.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There you go.
Start to think this is trolling

 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This what you mean?

Proble is it works untill I go to the next loop.
once I move from pressing characters I am back to the same problem.
If I press comands and then press characters again, it's out.
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And.... you are missing the closing brace I had. When following suggestions every character counts.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,
I almost gave up and went to my previous version.
Did a bunch of other versions.
Code ranch hill billies rules. Definitely better then StackOverflow for complete newbies like me, at least!

 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slight tweak would improve this.

Put at line 3
int choice;
so that it is declared outside of the loop.

On line 11 remove 'int'. This forces it to use 'choice' that you just declared in the above step.

Line 28
}while( choice != 3 ); // if it is 3 then we're done here
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside for some indentation cleanup...

Do you understand how this works? Do you have any questions?
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was cleaning up and got the line 3 correction before hand.
I was struggling with while(choice !=3)

I was trying while(choice == 3), which was pushing me out of the loop. Daaa!

so, to explain in plain english:

the loop:
the boby is being evaluated as long as choice is not 3

the body inside the loop:

- print the menu(will tidy that up)
- as long as the input is not of type int, the scanner.next() will take the next token - in out case the Try again: Invalid character token, because we are in a loop
- when the condition is meet, as an integer is passed the loop is terminated
- we need to declare a field of type int, used in the switch case
- the field needs to be assigned the value of scanner.next(), so the next tokens can be evaluated

- when the condition is meet, the loop exits

Do you think I missed anything?

I do have a question about the flow control, i am not sure I represent it correctly:

I am confused how to draw it.

Start - Option:choose option
if option 1, display message and menu again
if option 2, display list and menu
if option 3 exit

I am
Thank you!
bleh.jpeg
[Thumbnail for bleh.jpeg]
 
Carey Brown
Saloon Keeper
Posts: 6939
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good. Just to be clear.
- we need to declare a field of type int, used in the switch case
Your variable 'choice' serves two purposes, as an input to the switch() and as a value checked by the outer loop in the while(). This is why it has to be declared outside the loop, otherwise the compiler would complain when you try to use it in the while() statement.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, I think i understand the variable scope. I need more practice with loops and logic conditions.

I just updated my previous post.

Could you please give me some feedback for the flow diagram. I understand that I should have a no, for the condition as well. In my case the no, doesn't do anything.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll come back tmrw with complete code for feedback. need to go for now!
thank you
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean you left out the !hasNextInt() loop, that I think happens to a more granular level, which doesn't need to be displayed
 
Campbell Ritchie
Marshal
Posts: 68113
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:. . . I just updated my previous post. . . .

Please don't edit posts like that. Post the changes as a new post; I think the changes can stand in this instance.
 
Campbell Ritchie
Marshal
Posts: 68113
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:Thank you,

That's a pleasure

I almost gave up and went to my previous version.
Did a bunch of other versions.

Careful. That sounds as if you were trying out multiple guesses. You can guess 1,000,000× and some of the guesses will turn out to be correct. Or you can plan the code carefully and get something correct straight away.

Code ranch hill billies rules. Definitely better then StackOverflow for complete newbies like me, at least!

We're better. Full stop

That code is a lot better, but I don't like to see while (true); because you need an artificial exit from the loop. It is also not a good idea to write number literals; in fact the old Sun Style Guide says only to use 0 and ±1, but I think that is a little too restrictive. Let't get rid of the ”magic numbers”:-That code might look verbose, but the constants make it much more reliable; if you write anything wrong the compiler will probably pick it up: with number literals you might get no error messages at all.Now let's see if we can't get rid of the while (true); I shall need an extra pair of {} because you want to restrict the scope of choice. Otherwise you have an infinite loop.Your indentation is inconsistent. We go on about indentation a lot because bad indentation makes it difficult for you to read your code, even more than everybody else.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell,

Thank you for the comments!
I like your approach!

At the moment I need to draw the flow diagram for the code I have.
Could I get some comments on that or some help?

As I said yesterday I brought the code over. for what I needed the scanner.

This is the scenario:

There is a reservation that accepts Animals(baseProduct).
At the moment the reservation will be having:
- birds of different breeds: wild: aquatic warble can have a price between 3 and 5, artic squa will have a price of 5, artic tern a price of 5,
                                      domestic: aquatic warble of price 4
                                      cubs: avocet of price 3

The customer should be able to see what is to view/purchase. Be able to purchase. See what is left to purchase after a purchase is done.
The reservation can only allow 300 items in it.

This is one of the versions of code I came with, that could be build on top of it.
I would be looking to draw a flow diagram chart for it and talk avout how could I expand the project.
Ex: bring interfaces for the methods, use enums for the products that have fixed price, use enums for different errors, have lists of errors.
I think it's a good project I can expand on and build on my Java skills




hope my indentation improved.
 
Marshal
Posts: 6869
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one problem that you caught yourself:
In general, you should declare your variables just as you use them.  There are exceptions -- and you've run into one of those already -- but this is not one. Declare and initialize countSameProduct right before the for-loop and remove the static declaration near the top of the class.  Declaring a variable static means that there is one variable per class and you want a new variable every time the method is called.
 
Knute Snortum
Marshal
Posts: 6869
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared this field public.  Usually you do not want to expose the internals of the class that way -- use a "getter" method.  Fortunately, you already have one near the bottom of the class, so just make the field private.
 
Knute Snortum
Marshal
Posts: 6869
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things here.  First, don't use "var" as a variable name.  It's confusing, since it's a reserved word in Java 11.  Also, I don't see where you use the variable in your class.  Probably you should just delete the line.
 
Lg Long
Ranch Hand
Posts: 90
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes @Knute, you are right. what should a boolean variable called when needed?
I think I was trying to build the method and I realised I don't need it.

Plan to bring some unit tests so I can get some feedback.

I think I should test that shop is emty, shop has one item, remove and count method. I should bring the remove method over.
 
Knute Snortum
Marshal
Posts: 6869
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lg Long wrote:what should a boolean variable called when needed?


It is often a good idea to name a boolean after what it represents, such as allItemsHaveBeenProcessed.  Then you can write
and the code becomes clear.
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!