tlx119 发表于 2008-4-25 10:28

关于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是无空格字符串。
实在是想不出来这代码错在哪里?
请大家帮忙看下!

tlx119 发表于 2008-4-25 10:51

测试结果:
Input AccountID:
321421
Input Address:
Input Job:
321

321421

321

sunkaidong 发表于 2008-4-25 11:47

int anAccountID = Integer.parseInt(in.nextLine());

okchenpeng 发表于 2008-4-25 13:34

这个问题我以前也遇到过,也一直没想明白

如果把第二个输入改成这样就可以输入,但只能输入不带空格的住址了....
System.out.print( "Input Address: ");
        String street = in.next();

tlx119 发表于 2008-4-25 14:36

改成 int anAccountID = Integer.parseInt(in.nextLine());问题解决
谢谢

sunkaidong 发表于 2008-4-25 14:38

一行一行读数据..同时注意异常...当数据转化为int时有异常的

[[it] 本帖最后由 sunkaidong 于 2008-4-25 14:41 编辑 [/it]]

页: [1]

编程论坛