Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: While Loop Issue

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default While Loop Issue

    Hello everyone. I'm new to Java and I'm trying to check if the user is inputting a word, or a number. We were just introduced to while loops in class and I figured I'd try one out. My program worked up until I tried to validate what the user is inputting.

    My problem is that the input doesn't stop and the user can just keep hitting return.

    *** Disclaimer, I'm just messing around and the text doesn't mean anything.

     
    import java.util.Scanner;
     
    public class SchoolDegree 
    {
    public static void main(String[] args)  
        {
        System.out.print("Please enter your school name: ");
        Scanner in = new Scanner(System.in);   
        String schoolName = in.nextLine();
     
            while (in.hasNextLine())
                {                          
                if (schoolName.equals("Syracuse"))
                    {                
                    schoolName += ", aka CUUUUUSE.";         
                    }
                else if (schoolName.equals("Ohio"))
                    {   
                    schoolName += ", aka NO SOUP FOR YOU.";
                    }
                else schoolName += ", aka who?";            
                }
            while (in.hasNextInt())
                 {
                   System.out.println("How can your school be a number?");
                 }
        System.out.println("You attend " + schoolName);     
        }
    }


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: While Loop Issue

    when you have got the user to successfully finish the loop. then use

    break;

    to break the loop and end it..

Similar Threads

  1. frustrating loop/array issue
    By knightsb78 in forum Loops & Control Statements
    Replies: 6
    Last Post: August 11th, 2012, 05:47 PM
  2. Unending Loop Issue? Can't find it.
    By computerguy38 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 4th, 2011, 07:22 PM
  3. Basic loop issue
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 23rd, 2011, 04:10 PM
  4. Java uberNoob, requesting help with simple loop issue
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 08:13 PM
  5. Weird issue with while loop ending/being skipped
    By ang3c0 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 25th, 2009, 11:09 AM