Results 1 to 5 of 5
Thread: bug in program flow?
- 07-23-2013, 10:43 PM #1
Member
- Join Date
- Jul 2013
- Posts
- 2
- Rep Power
- 0
bug in program flow?
Hello,
I'm new to Java and I have a problem with this code. When my program gets to the following piece of code it ignores the while loop and automatically goes to the next while loop:
Java Code:while (name[1] == null) { System.out.println("Enter your family's name: "); name[1] = scanner1.nextLine(); System.out.println(name[1]); }
Here's my class:
Java Code:import java.util.Scanner; public class test2 { public static void main(String[] args) { Scanner scanner1 = new Scanner(System.in); int[] age; String[] name; age = new int[2]; name = new String[2]; while (name[0] == null) { System.out.println("Enter your name: "); name[0] = scanner1.nextLine(); } while (age[0] == 0) { System.out.println("Enter your age: "); age[0] = scanner1.nextInt(); } while (name[1] == null) { System.out.println("Enter your family's name: "); name[1] = scanner1.nextLine(); System.out.println(name[1]); } while (age[1] == 0) { System.out.println("How much money yar have?"); age[1] = scanner1.nextInt(); } } }
Last edited by pizzapim; 07-23-2013 at 10:58 PM.
- 07-23-2013, 10:51 PM #2
Re: bug in program flow?
Forum Rules -- particularly the third paragraph.
http://www.java-forums.org/forum-gui...w-members.html
BB Code List - Java Programming Forum - Learn Java Programming
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 07-23-2013, 10:53 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: bug in program flow?
First,you should read the forum guidelines mentioned in the FAQ. One of the things it says to do is use code tags for formatting code. I have done it for you here.
Java Code:while (name[1] == null) { System.out.println("Enter your family's name: "); name[1] = scanner1.nextLine(); System.out.println(name[1]); }
Here's my class:
Java Code:import java.util.Scanner; public class test2 { public static void main(String[] args) { Scanner scanner1 = new Scanner(System.in); int[] age; String[] name; age = new int[2]; name = new String[2]; while (name[0] == null) { System.out.println("Enter your name: "); name[0] = scanner1.nextLine(); } while (age[0] == 0) { System.out.println("Enter your age: "); age[0] = scanner1.nextInt(); } while (name[1] == null) { System.out.println("Enter your family's name: "); name[1] = scanner1.nextLine(); System.out.println(name[1]); } while (age[1] == 0) { System.out.println("How much money yar have?"); age[1] = scanner1.nextInt(); } } }
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 07-23-2013, 11:12 PM #4
Member
- Join Date
- Jul 2013
- Posts
- 2
- Rep Power
- 0
Re: bug in program flow?
I figured out that I mustn't create the Scanner outside of the while loop, so I created it inside it and now it works.
Thanks to jim829!
Regards, pizzapim
- 07-23-2013, 11:17 PM #5
Senior Member
- Join Date
- Apr 2013
- Location
- Sweden
- Posts
- 272
- Rep Power
- 8
Similar Threads
-
how the flow works. Unable to find the flow of the code also need explanation in case
By mayankpant in forum New To JavaReplies: 6Last Post: 05-15-2013, 10:30 PM -
Flow-Charting?
By danthegreat in forum Forum LobbyReplies: 4Last Post: 09-12-2011, 07:24 AM -
Web Service flow
By sameerk in forum Web FrameworksReplies: 1Last Post: 05-06-2011, 10:56 AM -
How to generate Data Flow Diagram(Work Flow Diagram)
By stsivaraj in forum AWT / SwingReplies: 1Last Post: 12-23-2010, 12:21 AM -
program flow
By loktamu in forum AWT / SwingReplies: 1Last Post: 09-17-2008, 05:52 PM
Bookmarks