[数字格式,尝试获取数据时异常

问题描述 投票:0回答:1

[当我尝试运行以下代码时,它甚至不让我输入输入并抛出NumberFormatException。任何人都可以帮助我,我复习了类似的问题,但没有得到正确的答案。

public static String takeFieldName() {
            String fullfield = "";
            //Scanner sc = new Scanner(System.in);
            System.out.println("Enter number of fields: ");
            //int nofields = sc.nextInt();
            int nofields = Integer.parseInt(sc.nextLine());

            String[] fname = new String[nofields];
            //sc.skip("/n");
            System.out.println("Specify fields with names");

            for(int i=0;i<fname.length;i++) {
                System.out.println("Enter field :"+(i+1));
                fname[i] = sc.nextLine();
            }
            System.out.println("Fields are:");
            for(String s:fname) {
                System.out.println(s);
            }
            return UserDAO.toCSV(fname);
    }

例外是:

Enter tablename: 
manikanta
table already exists
Enter tablename: 
salamankhan
Enter number of fields: 
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at connections.UserDAO.takeFieldName(UserDAO.java:53)
    at connections.Application.main(Application.java:11)
java database exception jdbc input
1个回答
0
投票

修改

int nofields = Integer.parseInt(sc.nextLine());

进入

int nofields = sc.nextInt());
© www.soinside.com 2019 - 2024. All rights reserved.