Infinite Loop in general output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andrea Sanchez
    New Member
    • Nov 2011
    • 1

    Infinite Loop in general output

    What's causing the error message for the InputMismatchEx ception in this code to loop?

    import java.io.*;
    import java.util.*;
    public class clubreg
    {
    public static void main(String args[])throws IOException
    {
    System.out.prin t("\t\t\t\t\t\t \tClub Registration Project by Andrea Sanchez,Aaron Go,Lycon Soliba and Patrick Balisong!\n");
    BufferedReader x=new BufferedReader( new InputStreamRead er(System.in));
    Scanner y=new Scanner(System. in);
    String name, curr, section, exco, sure ,rusure, tryagain;
    int yrlvl=0, index=0,control =0, length,same=1,n osame=0;
    char csure='y',ctrya gain='y',ccurr, csection,cexco, crusure='n';
    boolean a=true;
    while(ctryagain =='y')
    {
    System.out.prin tln("What is your name?");
    name=x.readLine ();

    System.out.prin tln("What is your year level?\n\t1 for 1st year\n\t2 for 2nd year\n\t3 for 3rd year\n\t4 for 4th year");
    while(a==true)
    {
    try
    {
    yrlvl=y.nextInt ();
    a=false;

    }

    catch(InputMism atchException e)
    {
    System.err.prin tln("Error! Input numbers only!Enter another number");
    }

    }
    a=true;

    while(yrlvl>4|| yrlvl<1)
    {
    System.out.prin tln("Error! Choose from 1-4 only!");
    yrlvl=y.nextInt ();
    }

    ...


    I'm losing my mind... Please Help me.
  • rotaryfreak
    New Member
    • Oct 2008
    • 74

    #2
    one quick thing you can do is:

    Code:
    catch(InputMismatchException e)
    {
    e.printStackTrace();
    System.out.println(e.getMessage());
    }
    printStackTrace () will give you the line which gave you the error
    e.getMessage() will help you in determining why you got that message

    i have not run the code, but i would have to say that your while-loop is probably not working as you expect. It probably should be something like:
    Code:
    while(ctryagain.equals('y')){
    }

    Comment

    Working...