Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Problem in looping

 
Greenhorn
Posts: 15
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I think its solved.
Please check my code if there any bug:
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried entering a non-numeric value ?
 
Avinash Oak
Greenhorn
Posts: 15
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I think its solved, please have a look on my code and check if there still any bug.

and below is the output:
 
Sheriff
Posts: 17489
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd put the try/catch inside the while loop. Let your user try again without having to rerun the program.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 23936
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Avinash Oak
Greenhorn
Posts: 15
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all !!

Henry, can you please show me how can I do this.
 
Avinash Oak
Greenhorn
Posts: 15
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I still want my program prompt for "Please enter no:" even user enter a nun-numeric value.

 
Avinash Oak
Greenhorn
Posts: 15
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also there is another problem, I cann't use "num" variable without initialization.
 
Greg Ferguson
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what i currently got. it seems to keep asking for numbers for odd but when an even number is entered it spams you entered an even number.

 
Gus Flores
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what i currently got. it breaks out of the program now when i enter an even number and it keeps going for odd numbers but i don't understand the c.getEvenValue() at the end.

 
Marshal
Posts: 77532
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use quote tags round the whole post. I shall remove them.
You do not want to create new objects just to call a method on. What is more, the getEvenValue method does not do what it says on the tin. It simply prints a message. So why have you got it at all? I can see no point to it.
You have a very poor name for your class: Journal4. You also have a very poor structure to your for loop; I have never seen anybody use % 1 before.
What you want is to start the loop and run it once, getting input as you go. There is a special type of loop which does that. Then you can put the text in the place you want it: the end of the loop.
 
Gus Flores
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry about the quote tags, i didn't know.

that what i'm trying to figure out how to make the proper for loop to end it.

i just named the class journal 4 because thats the homework assignment.

the val%1 seems to end it just fine. it keeps asking for numbers and once an even number is entered it stops the program and outputs you entered an even number.
i have to use the method getEvanValue which is the other part i'm trying to understand.
 
Campbell Ritchie
Marshal
Posts: 77532
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, i % 1 == 0 does not terminate the loop. Whichever integer number you put to the left of the % you will always get 0 as the remainder, so you might as well write while (true).... There is a difference between code which works and good code. There is a type of loop which will run once then repeat if a certain condition is fulfilled. Remind yourself about the different types of flow control in the Java™ Tutorials.
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic