学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
发新话题
打印

关于Scanner比较奇怪的问题

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

TOP

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

321421

321

TOP

int anAccountID = Integer.parseInt(in.nextLine());
学习需要安静。。海盗要重新来过。。

TOP

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

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

TOP

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

TOP

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

[ 本帖最后由 sunkaidong 于 2008-4-25 14:41 编辑 ]
学习需要安静。。海盗要重新来过。。

TOP

发新话题