GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Problem in looping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in looping" Watch "Problem in looping" New topic
Author

Problem in looping

Avinash Oak
Greenhorn

Joined: Jun 12, 2012
Posts: 15

Hello All,
I am facing problem with looping statement in my code, this code is for displaying even or odd numbers whatever user will input. Working fine but when I want it to ask for input next number it got failed. Explained below:
1. Console --> Please enter no:
2. User --> 25
3. console --> Even
4. console --> Do you want to continue?
5. User --> Yes
6. //Will repeat same//
7. Console -->Do you want to continue?
8. User --> No
9. //Program Should exit//

I have pasted my code here, please suggest me what to do?
Avinash Oak
Greenhorn

Joined: Jun 12, 2012
Posts: 15

Hi, I think its solved.
Please check my code if there any bug:
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Have you tried entering a non-numeric value ?
Avinash Oak
Greenhorn

Joined: Jun 12, 2012
Posts: 15

I tried using try and catch statement with entering "A" as input, but its got fail with some error log:




Please suggest what to do, when someone enter Non-numeric values.
Avinash Oak
Greenhorn

Joined: Jun 12, 2012
Posts: 15

Hi, I think its solved, please have a look on my code and check if there still any bug.

and below is the output:
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

Since your "oK" variable is used only once in the program, it's perfectly fine to use the literal string in place. Line 22 can be

Also, you would do well to give a better name than "scnr" -- why are you afraid of vowels? There's no prize for abbreviating variable names. There is a penalty though: "scnr" is much harder to read than "scanner". Besides, you've already declared that variable as a Scanner, why repeat yourself? A name like "userInput" conveys the purpose much better, don't you think?


Junilu - [How to Ask Questions] [How to Answer Questions]
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
I'd put the try/catch inside the while loop. Let your user try again without having to rerun the program.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Have you tried running the program and entering some numbers?

It will print "Odd" for even numbers, and "Even" for odd numbers. Isn't that odd?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Greg Ferguson
Ranch Hand

Joined: Jun 04, 2012
Posts: 34
Jesper de Jong wrote:Have you tried running the program and entering some numbers?

It will print "Odd" for even numbers, and "Even" for odd numbers. Isn't that odd?


I ran the program and saw that too. Fortunately, that's a very easy fix. Other than a few minor things, it works well.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Avinash Oak wrote:Hi, I think its solved, please have a look on my code and check if there still any bug.

and below is the output:


Are you doing this because that is the desired requirement? Or are you doing it because it is the easiest thing to do?

Wouldn't a better option be ... "invalid input. Please try again." followed by the prompt for another number?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Avinash Oak
Greenhorn

Joined: Jun 12, 2012
Posts: 15

Thanks to all !!

Henry, can you please show me how can I do this.
Avinash Oak
Greenhorn

Joined: Jun 12, 2012
Posts: 15

But I still want my program prompt for "Please enter no:" even user enter a nun-numeric value.

Avinash Oak
Greenhorn

Joined: Jun 12, 2012
Posts: 15

also there is another problem, I cann't use "num" variable without initialization.
Greg Ferguson
Ranch Hand

Joined: Jun 04, 2012
Posts: 34
Avinash Oak wrote:also there is another problem, I cann't use "num" variable without initialization.

It seems to work fine without having to initialize it. I just removed the "=0" part and it still ran. What kind of error did you receive?
Avinash Oak
Greenhorn

Joined: Jun 12, 2012
Posts: 15

Buenny Fry wrote:
Avinash Oak wrote:also there is another problem, I cann't use "num" variable without initialization.

It seems to work fine without having to initialize it. I just removed the "=0" part and it still ran. What kind of error did you receive?


But can it prompt for "enter number" again after user entered a non-numeric value?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Avinash Oak wrote:But can it prompt for "enter number" again after user entered a non-numeric value?

I think part of your problem is that all your code is in one place. Why not write a method that prompts the user to enter a number, keeps looping until they do, and then returns it when they finally get it right? Maybe something like:(I leave the implementation up to you ).

Then in your main() method, all you have to do is:
num = enterNumber(userInput, "Please enter no:");

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem in looping