如何重置do while循环?我一直收到“java.lang.IllegalArgumentException”错误

时间:2015-04-09 03:06:32

标签: java do-while

我正在尝试制作一个显示从Infix转换为Postfix的列表的程序,反之亦然。但是,当循环重新启动时,它会不断询问我是否要继续,并且一旦我点击" y"它就会抛出非法参数异常。

我真的很感激帮助。

import java.util.*;

public class ConversionTest1
{
  public enum Choices
  {one, two, three, four, y; }
  public static void main(String [ ] args)
  {
    try
    {
      LinkedStackClass lstack = new LinkedStackClass();
      InfixToPostfix obj = new InfixToPostfix();
      PostFixtoInFix object = new PostFixtoInFix();
      Scanner scan = new Scanner(System.in);
      char ch;
      int[] opt = {1,2,3,4};
      int size;
      do
      {
        System.out.println("Please select what type of conversion you would like to do: ");
        System.out.println(" 1) Infix to postfix ");
        System.out.println(" 2) Postfix to infix ");
        System.out.println(" 3) Print Equations ");
        System.out.println(" 4) Exit ");
        String choice = scan.nextLine();
          switch (Choices.valueOf(choice))
          {
            case one : 
              System.out.println("Infix : \t");
            //  lstack.obj(console.nextInt());
              String infix = scan.nextLine();
              System.out.println("Postfix : \t"+obj.convert(infix));
            //  System.out.println("Postfix : \t"+obj.convert(obj));
              //lstack.insertAtStart( scan.nextInt() );                     
              break; 

            case two : 
              System.out.println(" 2) postfix to infix ");
              System.out.println("Postfix : ");
            String postfix = scan.nextLine();
            System.out.println("Infix : "+object.convert(postfix));
              //list.insertAtStart( scan.nextInt() );                     
              break; 

            case three : 
              System.out.println(" 3) Print Equations ");
              System.out.println("Print Equations!");
             // list.insertAtStart( scan.nextInt() );                     
              break; 

            case four : 
              System.out.println(" 4) Exit ");
              //list.insertAtStart( scan.nextInt() );   
              System.exit(0);
              break; 
        //    case y:
          //    System.out.println("Working!");
            //  break;
              default : 
              System.out.println("Wrong Entry \n ");
              break; 
          }

        System.out.println("\nDo you want to continue (Type y or n) \n");
        ch = scan.next().charAt(0); 
      } while(ch == 'Y'|| ch == 'y');
    }
    catch(EmptyStackException e)
    {
      System.out.println("I caught it!");
    }

  }
}

1 个答案:

答案 0 :(得分:1)

你应该使用scan.nextLine();只是为了清理你的缓冲区,因为这是一个空白区域" "这个值并不存在于你的枚举中。

在读完字符后尝试这个,这应该清理你的缓冲区! 祝你好运。

     case four : 
              System.out.println(" 4) Exit ");
              //list.insertAtStart( scan.nextInt() );   
              System.exit(0);
              break; 
        //    case y:
          //    System.out.println("Working!");
            //  break;
              default : 
              System.out.println("Wrong Entry \n ");
              break; 
          }

        System.out.println("\nDo you want to continue (Type y or n) \n");
        ch = scan.next().charAt(0); 
        scan.nextLine();
      } while(ch == 'Y'|| ch == 'y');
    }
    catch(EmptyStackException e)
    {
      System.out.println("I caught it!");
    }