把 read = new byte[2]改为 byte[3],不过会出现另一个问题,试一下就知道了。
解决方案 »
- [求助]确保字符串是1-100的数,怎么写正则?
- java学了一年了,但我一直梦想成为网络工程师,大家来救救迷茫的我吧
- 怎么jsp的index.jsp内容无法改变呢?苦恼中...
- 急急急!jfreechart如何生成动态曲线图
- 如何设置Jframe的背景图片?
- 在一个字符串中怎么判断是英文还是中文?
- processWindowEvent(WindowEvent e)方法有什么用?
- 请“john_sheep(彩虹勇士(楼主,没事就把贴子结了吧.)) ”进来接分100
- servlet:我的浏览器关不掉!!!
- 简单问题:String怎么转换为int
- 请问各位大哥:如何将FRAME上的咖啡标志改成自己设计的标记呢?一定加分!急!急!急!
- 小弟想跳到一家大公司搞JAVA开发,但是要先做技术支持一类的工作,经常出差,而且不知道何时才能真正搞开发?该去还是该留?
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确实没有错误了,可是输入一个负数居然变成正的了?怪怪的!我到底应该怎么读屏幕上的输入啊?
在第二次循环时他不会像第一次一样等你输入,而是直接去读以前的内容。比如第一次输入
-4并回车,则它第一次会读到-4,接着转入第二次循环,这次它接着去读in里的内容:回车或空,因此就出现了输入数格式不对的错误。
你可以输入-4567来验证以下。
应该是用reset()来解决,需要再查一下。
我现在试试,这个reset是哪个对象里的?
at java.io.BufferedInputStream.reset(BufferedInputStream.java:373)
不过就不能做到只读两位数了,你可以加上是否大与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;}
}
}
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){
}
我试试吧
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!");
}
}
}
}