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