Results 1 to 19 of 19
- 01-14-2011, 05:47 PM #1
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
How to consider the Input File Name containing spaces in it , when given by user.
I wrote a java code which picks up the File Name from the console
Now I want the java code to consider the Input File Name , with spaces in it .Java Code:try { File readin = null; boolean gettingFilename = true; while(gettingFilename) { System.out.println("Enter fully qualified CCLN Dataset name: "); inFile = scan.next();
Which the code is not doing .
Can anyone help me with the statements to consider the Input File , with spaces in its name .
Thank You In Advance .
-
have you tried scan.nextLine() instead?
- 01-14-2011, 07:06 PM #3
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Hi
If i use scan.nextLine() , it is not stopping after asking for the Input File ,Iskips the first time question and reports File not found eror and comesback and asks again ..and when file name is given with spaces in it , it is considered and file is processed .
How can i get it to work , when asked for the first time only ...Thank You
Enter fully qualified CCLN Dataset name:
The Input File Name is :
Please wait...for the processing to be completed
Input File Not Found
Enter fully qualified CCLN Dataset name:
-
- 01-14-2011, 07:20 PM #5
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Hi
This is the piece of code which takes the input file and reads each line of input file.
Java Code:// Here we enter the input filename with fully qualified path try { File readin = null; boolean gettingFilename = true; while(gettingFilename) { System.out.println("Enter fully qualified CCLN Dataset name: "); inFile = scan.next(); ------------------------------------>//Is changed to scan.nextLine(); System.out.println("The Input File Name is :"+inFile); System.out.println("Please wait...for the processing to be completed"); System.out.println(" "); readin = new File(inFile); if(readin.exists()){ System.out.println(" "); Filename myHomePage = new Filename(inFile, '\\', '.'); Filename = myHomePage.filename(); gettingFilename = false; }else{ System.out.println("Input File Not Found"); } } // FIRST STEP : Here I open the input file and read in record by record Scanner in = new Scanner(readin); while (in.hasNextLine()){ String input = in.nextLine(); input = input.trim(); String[] column = input.split(","); int columnCount = column.length; if(columnCount == 4){ }else{ } } }catch(FileNotFoundException e){ System.out.println("Error Message :I/P File Not Found - "+e.getMessage()); }Last edited by renu; 01-14-2011 at 07:27 PM.
-
It is very hard to read your posted code with quote tags. Please edit your post above and change the quote tags to code tags. Thanks.
-
Again, I suspect the problem is in code not shown, that the error is in code above what you have posted. I'm guessing that you're getting tokens from the Scanner object, scan, prior to the code above, but have left an unprocessed end of line token somewhere, and that this gets processed the first time your while loop iterates. You probably need to process that end of line token, perhaps with a call to scan.nextLine() before the while loop.
- 01-14-2011, 07:27 PM #8
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
-
- 01-14-2011, 07:38 PM #10
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
QUOTE=Fubarable;170044]Again, I suspect the problem is in code not shown, that the error is in code above what you have posted. I'm guessing that you're getting tokens from the Scanner object, scan, prior to the code above, but have left an unprocessed end of line token somewhere, and that this gets processed the first time your while loop iterates. You probably need to process that end of line token, perhaps with a call to scan.nextLine() before the while loop.[/QUOTE]
Sir,
This is the full code .... ...Please let me know where is the error .
Java Code:Scanner scan = new Scanner(System.in); pathOfFile = System.getenv("TEMP"); File location =createDirectoryIfNeeded(pathOfFile ,"ABCDEF"); enteredVal = f_enteredVal(scan); if (enteredVal==1) { String process = "EXTRACTION PROCESSING BEGAN"; System.out.println("process :"+process); // Calling the procedure to enter all the input values required. user_id = f_userid(scan); password = f_password(scan); loopUntilGoodUser = dbConnect(user_id, password); // Loops until user name and password are correct to connect to DB. while(loopUntilGoodUser == false){ user_id = f_userid(scan); password = f_password(scan); loopUntilGoodUser = dbConnect(user_id, password); } year = f_year(scan); Source = f_Source(scan); roleID = f_userRole(user_id); if(roleID == 1){ extractionType = f_extractionType(scan); }else{ extractionType = 1; } if(extractionType == 1){ dataType = f_dataType(scan); dataSelection = 2; RefNum = 0; }else if(extractionType == 2){ dataType = 3; dataSelection = f_dataSelection(scan); System.out.println("Enter the Rate Reference Number: "); RefNum = scan.nextInt(); } // Here we enter the input filename with fully qualified path try { File readin = null; boolean gettingFilename = true; while(gettingFilename) { System.out.println("Enter fully qualified CCLN Dataset name: "); inFile = scan.next(); ------------------------------------>//Is changed to scan.nextLine(); System.out.println("The Input File Name is :"+inFile); System.out.println("Please wait...for the processing to be completed"); System.out.println(" "); readin = new File(inFile); if(readin.exists()){ System.out.println(" "); Filename myHomePage = new Filename(inFile, '\\', '.'); Filename = myHomePage.filename(); gettingFilename = false; }else{ System.out.println("Input File Not Found"); } } // FIRST STEP : Here I open the input file and read in record by record Scanner in = new Scanner(readin); while (in.hasNextLine()){ String input = in.nextLine(); input = input.trim(); String[] column = input.split(","); int columnCount = column.length; if(columnCount == 4){ }else{ } } }catch(FileNotFoundException e){ System.out.println("Error Message :I/P File Not Found - "+e.getMessage()); }
The screen shot looks like this :-
Enter fully qualified CCLN Dataset name:
C:\abc\JAVAPROJECT\renu Spaces.csv
The Input File Name is :C:\abc\JAVAPROJECT\renu
Please wait...for the processing to be completed
Input File Not Found
Enter fully qualified CCLN Dataset name:
The Input File Name is :Spaces.csv
Please wait...for the processing to be completed
Input File Not Found
Enter fully qualified CCLN Dataset name:Last edited by renu; 01-14-2011 at 07:45 PM.
-
Your error could be anywhere you use the scan variable, get a token but ignore an end of line token. For instance, the error could be here:
since you ignore the end of line token. If this were the spot, you'd fix it by changing this to:Java Code:System.out.println("Enter the Rate Reference Number: "); RefNum = scan.nextInt();
Java Code:System.out.println("Enter the Rate Reference Number: "); RefNum = scan.nextInt(); scan.nextLine(); // need to handle the end of line token.
Of course the error could be generated from any place where you use the Scanner prior your current use of it, including all these method calls:
You may be better off creating your Scanner object anew in each method -- just be sure to close it at the end of the method that creates a local object so as not to run out of system resources!Java Code:user_id = f_userid([b][color="red"]scan[/color][/b]); password = f_password([b][color="red"]scan[/color][/b]); //...... while(loopUntilGoodUser == false){ user_id = f_userid([b][color="red"]scan[/color][/b]); password = f_password([b][color="red"]scan[/color][/b]); //... } year = f_year([b][color="red"]scan[/color][/b]); Source = f_Source([b][color="red"]scan[/color][/b]); //... if(roleID == 1){ extractionType = f_extractionType([b][color="red"]scan[/color][/b]); } //.... if(extractionType == 1){ dataType = f_dataType([b][color="red"]scan[/color][/b]); //.... }else if(extractionType == 2){ //... dataSelection = f_dataSelection([b][color="red"]scan[/color][/b]);Last edited by Fubarable; 01-14-2011 at 07:50 PM.
- 01-14-2011, 07:56 PM #12
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Thank You Sir ..for your reply .
Thanks for the wonderful explanation . I will do it .
-
- 01-18-2011, 05:22 PM #14
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Sir
I tried using the scanner object in the method itself
and it gives me an errorJava Code:public static String f_userid() { Scanner scan = new Scanner(System.in); System.out.println("Enter User Id: "); String user_id = scan.next(); //Scan the input int userlen = user_id.length(); while(userlen != 5){ System.out.println("The User Id is < 5 characters."); System.out.println("Enter User Id: "); user_id = scan.nextLine(); //Scan the input userlen = user_id.length(); } scan.close(); return user_id; }
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at hfrps.MainClass.f_userid(MainClass.java:936)
at hfrps.MainClass.main(MainClass.java:131)
And even the problem of taking the spaces within the File name and the file path is not solved .need help to fix this problem
- 01-19-2011, 01:48 AM #15
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
here's a cleaner way to do it
as for input file names with spaces, you can use nextLine() instead of next() for your Scanner objectJava Code:......... while(true){ System.out.println("Enter User Id: "); String user_id = scan.next(); //Scan the input int userlen = user_id.length(); if (userlen != 5){ System.out.println("The User Id is < 5 characters."); }else{ break; //or do your return here } }
- 01-19-2011, 05:23 AM #16
I just ran your f_userid() method independently and it worked fine, no errors and no exceptions.
Plus, why are you using two different methods to scan your input?
Any specific reason?Java Code:String user_id = scan.next(); //Scan the input // AND user_id = scan.nextLine(); //Scan the input
Change your hard coded message to something else,
Because even when the input characters are more than 5, it displays the same message. You can say, "The User Id should match exactly 5 characters" or something... :)Java Code:System.out.println("The User Id is < 5 characters.");
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-19-2011, 05:24 PM #17
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Sir,
This is a console program , where it repeats the questions , till it gets the right answer to proceed , for example in case of User id ..(It needs the right user id to proceed further)
I am attaching the code from the begining ...pls help me in correcting this code as well as to make it work .
Java Code:public static void main(String[] args) throws ClassNotFoundException, InterruptedException, IOException { : : enteredVal = f_enteredVal(); if (enteredVal==1) { String process = "DATA EXTRACTION PROCESSING BEGAN"; System.out.println("process :"+process); // Calling the procedure to enter all the input values required. user_id = f_userid(); }//end of main private static int f_enteredVal(){ Scanner scan = new Scanner(System.in); System.out.println("Please enter desired task number:"); System.out.println("1 =School Data Extraction"); System.out.println("2 =Hospital Data Extraction"); System.out.println("3 =Census Data Extraction"); enteredVal = scan.nextInt(); while(enteredVal != 1 && enteredVal != 2 && enteredVal != 3){ System.out.println("Invalid task number"); System.out.println("Enter desired task number:"); enteredVal = scan.nextInt(); // scan.nextLine(); } scan.close(); return enteredVal; } public static String f_userid() { Scanner scan = new Scanner(System.in); // System.out.println("Enter User Id: "); // String user_id = scan.next(); //Scan the input // int userlen = user_id.length(); // while(userlen != 5){ // System.out.println("The User Id is < 5 characters."); // System.out.println("Enter User Id: "); // user_id = scan.next(); //Scan the input // userlen = user_id.length(); // } // scan.close(); // return user_id; while(true){ System.out.println("Enter User Id: "); String user_id = scan.next(); //Scan the input int userlen = user_id.length(); if(userlen != 5){ System.out.println("The User Id < 5 characters."); }else{ return user_id; } } }
Thanks in advance.
The error i get when i run it :-
Exception in thread "main" Enter User Id:
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at hfrps.MainClass.f_userid(MainClass.java:970)
at hfrps.MainClass.main(MainClass.java:149)
- 01-19-2011, 05:33 PM #18
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
- 01-19-2011, 07:29 PM #19
Senior Member
- Join Date
- May 2010
- Posts
- 113
- Rep Power
- 0
Similar Threads
-
Need help getting input(first/last name) from user
By nightrise420 in forum New To JavaReplies: 11Last Post: 09-11-2010, 03:09 AM -
How do I write to an existing file in Java with a program that asks for user input?
By gmoney8316 in forum New To JavaReplies: 13Last Post: 04-16-2010, 02:51 AM -
Write to a txt. file, via user input? (java with netbeans)
By Glypsen in forum NetBeansReplies: 6Last Post: 03-01-2010, 07:00 AM -
User Input
By brmcdani in forum New To JavaReplies: 2Last Post: 02-05-2010, 01:59 AM -
Reading file data that contains no spaces
By jdepue in forum Advanced JavaReplies: 1Last Post: 08-01-2007, 04:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks