把 read = new byte[2]改为 byte[3],不过会出现另一个问题,试一下就知道了。

解决方案 »

  1.   

    System.out.println(group); 看看现实什么!
      

  2.   

    大家都看看啊,如果byte是2的话,输入<=0的数就是这样的错误:
    Please input how many groups number you want to purchase(1-99):
    Exception in thread "main" java.lang.NumberFormatException:
            at java.lang.Integer.parseInt(Integer.java:426)
            at java.lang.Integer.parseInt(Integer.java:454)
            at nanyue.main(nanyue.java:27)如果改成3确实没有错误了,可是输入一个负数居然变成正的了?怪怪的!我到底应该怎么读屏幕上的输入啊?
      

  3.   

    问题是:
    在第二次循环时他不会像第一次一样等你输入,而是直接去读以前的内容。比如第一次输入
    -4并回车,则它第一次会读到-4,接着转入第二次循环,这次它接着去读in里的内容:回车或空,因此就出现了输入数格式不对的错误。
    你可以输入-4567来验证以下。
    应该是用reset()来解决,需要再查一下。
      

  4.   

    我也是这样想过,所以曾经尝试把变量全部置空,可是没有用。
    我现在试试,这个reset是哪个对象里的?
      

  5.   

    奇怪,这个reset()明明是返回 void 为什么又有这样的错误?java.io.IOException: Resetting to invalid 
            at java.io.BufferedInputStream.reset(BufferedInputStream.java:373)
      

  6.   

    我先下去研究一下jdk文档再上来
      

  7.   

    这样可以,关键是用readLine()。
    不过就不能做到只读两位数了,你可以加上是否大与99的判断语句。import java.io.*;
    public class Test { 
    public static void main(String args[]) { 
    char[] lread=new char[2];
    String mgroup = new String();
    int g;
    boolean  flag = false;
    BufferedReader lin =
            new BufferedReader(
              new InputStreamReader(System.in));      while (!flag) {
          System.out.println("Please input how many groups number you want to purchase(1-99):");
    // System.in.reset();
          try {
            
            mgroup=(lin.readLine()).trim();
          }
          catch (Exception e) {
            System.out.println(e.toString());
          }
                g=Integer.parseInt(mgroup); //这里会执行错误?
          System.out.println(g);
          if (g<=0){
            flag=false;
          }
          else
            flag=true;} 
    }
      

  8.   

    我试过了,没事,这是我的代码,在JB下运行的
        byte rea[] = new byte[2];
        String group;
        int g;
        boolean flag=false;
        while (!flag) {
          System.out.print("Please input how many groups number you want to purchase(1-99):");
          try {
           System.in.read(rea);
          }
          catch (Exception e) {
            System.out.println(e.toString());
          }
          try {
             group=new String(rea).trim();
             g=Integer.parseInt(group);
            if (g<=0){
              flag=false;
            }
            else
              flag=true;
           System.out.println("g=    "+g);      }catch(Exception e){
          }
      

  9.   

    哦,你是在最外面又包了一个try?
    我试试吧
      

  10.   

    这是corejava的,使用:
    int num = Console.readInt("Please input how many groups number you want to purchase(1-99):");
    /**
       An easy interface to read numbers and strings from 
       standard input   @version 1.10 10 Mar 1997
       @author Cay Horstmann
    */public class Console
    {  /**
          print a prompt on the console but don't print a newline
          
          @param prompt the prompt string to display
        */   public static void printPrompt(String prompt)
       {  System.out.print(prompt + " ");
          System.out.flush();
       }
       
       /**
          read a string from the console. The string is 
          terminated by a newline      @return the input string (without the newline)
        */
        
       public static String readLine()
       {  int ch;
          String r = "";
          boolean done = false;
          while (!done)
          {  try
             {  ch = System.in.read();
                if (ch < 0 || (char)ch == '\n')
                   done = true;
                else if ((char)ch != '\r') // weird--it used to do \r\n translation
                   r = r + (char) ch;
             }
             catch(java.io.IOException e)
             {  done = true;
             }
          }
          return r;
       }   /**
          read a string from the console. The string is 
          terminated by a newline      @param prompt the prompt string to display
          @return the input string (without the newline)
        */
        
       public static String readLine(String prompt)
       {  printPrompt(prompt);
          return readLine();
       }   /**
          read an integer from the console. The input is 
          terminated by a newline      @param prompt the prompt string to display
          @return the input value as an int
          @exception NumberFormatException if bad input
        */
        
       public static int readInt(String prompt)
       {  while(true)
          {  printPrompt(prompt);
             try
             {  return Integer.valueOf
                   (readLine().trim()).intValue();
             } catch(NumberFormatException e)
             {  System.out.println
                   ("Not an integer. Please try again!");
             }
          }
       }   /**
          read a floating point number from the console. 
          The input is terminated by a newline      @param prompt the prompt string to display
          @return the input value as a double
          @exception NumberFormatException if bad input
        */
        
       public static double readDouble(String prompt)
       {  while(true)
          {  printPrompt(prompt);
             try
             {  return Double.parseDouble(readLine().trim());
             } catch(NumberFormatException e)
             {  System.out.println
             ("Not a floating point number. Please try again!");
             }
          }
       }
    }