关于Scanner比较奇怪的问题
import java.util.Scanner;public class Testerone
{
public static void main(String[] args)
{
Scanner in =new Scanner(System.in);
System.out.println("Input AccountID: ");
int anAccountID = Integer.parseInt(in.next());
System.out.print( "Input Address: ");
String street = in.nextLine();
System.out.println( "Input Job: ");
String Job = in.nextLine();
System.out.println(anAccountID);
System.out.println(street);
System.out.println(Job);
}
}
ID是int, Address是中间有空格的字符串,Job是无空格字符串。
实在是想不出来这代码错在哪里?
请大家帮忙看下!
Input AccountID:
321421
Input Address:
Input Job:
321
321421
321 int anAccountID = Integer.parseInt(in.nextLine());
这个问题我以前也遇到过,也一直没想明白
如果把第二个输入改成这样就可以输入,但只能输入不带空格的住址了....System.out.print( "Input Address: ");
String street = in.next(); 改成 int anAccountID = Integer.parseInt(in.nextLine());问题解决
谢谢 一行一行读数据..同时注意异常...当数据转化为int时有异常的
[[it] 本帖最后由 sunkaidong 于 2008-4-25 14:41 编辑 [/it]]
页:
[1]
