0

When i try to run this below code it is not even letting me to enter the input and throwing NumberFormatException. Can anyone please help me i reviewed similar kind of questions but didn't got the proper answer.

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);
    }

Exception is:

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)

When i use nextInt() to take the size of an array the output i'm getting is:

Enter tablename: 
salmankhan
Enter number of fields: 
2
Specify fields with names
Enter field :1
Enter field :2

Where i need to take field 1 and field 2 separately.

the takeTableName() is another function in that method i'm calling takeFieldName() if the table doesn't exist in the database it will call takeFieldName() method to take field names. in that i used next() method to take the table name

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
Mars_rider
  • 19
  • 4
  • 3
    you are trying to parse an empty String into a numeric value, but an empty String is not a numeric value. – Stultuske Dec 19 '19 at 09:03
  • then how do i pass the number to the array? when i use nextInt() it is not working properly. Please give me a solution for this. – Mars_rider Dec 19 '19 at 09:07
  • you should enter the number , provide the numeric value, instead of pressing enter key. – Thanigai Arasu Dec 19 '19 at 09:08
  • It is not even letting me to enter anything. after taking the table name i want to take the size of the array so i need to pass integer to that. after entering the table name itself it is throwing the exception – Mars_rider Dec 19 '19 at 09:11
  • @Mars_rider you have no number. Why not use a JOptionPane dialog? It 'll make it less likely to have invalid input. https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html – Stultuske Dec 19 '19 at 09:11
  • About your current code: where do you enter the table name? why don't you use sc.nextInt() to read your number? – Stultuske Dec 19 '19 at 09:13
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/5221149) – Andreas Dec 19 '19 at 09:15
  • the takeTableName() is another function in that method i'm calling takeFieldName() if the table doesn't exist in the database it will call takeFieldName() method to take field names. in that i used next() method to take the table name. – Mars_rider Dec 19 '19 at 09:20
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Gk Mohammad Emon Dec 19 '19 at 09:21
  • wait i'll edit the question please check it after a min – Mars_rider Dec 19 '19 at 09:24

1 Answers1

0

Modify

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

into

int nofields = sc.nextInt());
naoko
  • 123
  • 6