Results 1 to 19 of 19
  1. #1
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question 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
    Java Code:
    try {
    				File readin = null;                                                                          
    				boolean gettingFilename = true;
    				while(gettingFilename) {
    					System.out.println("Enter fully qualified CCLN Dataset name: ");
    					inFile = scan.next();
    Now I want the java code to consider the Input File Name , with spaces in it .
    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 .

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    have you tried scan.nextLine() instead?
    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:

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by renu View Post
    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
    Then you've got a bug in code that you're not showing us. Let's see how you're using this method and some more code too.

  5. #5
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question

    Quote Originally Posted by Fubarable View Post
    Then you've got a bug in code that you're not showing us. Let's see how you're using this method and some more code too.
    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 08:27 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  8. #8
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    Hi Sir,

    I did edit my previous post, please look at it and help me with the solution .

    Thank You .

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by renu View Post
    Hi Sir,

    I did edit my previous post, please look at it and help me with the solution .

    Thank You .
    already done.

  10. #10
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question

    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 08:45 PM.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    Java Code:
    System.out.println("Enter the Rate Reference Number: ");
    RefNum = scan.nextInt();
    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();
    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:
    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]);
    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!
    Last edited by Fubarable; 01-14-2011 at 08:50 PM.

  12. #12
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Thank You Sir ..for your reply .

    Thanks for the wonderful explanation . I will do it .

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by renu View Post
    Thank You Sir ..for your reply .

    Thanks for the wonderful explanation . I will do it .
    You're welcome. Please let us know if it fixes your problem or not.

  14. #14
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question

    Sir

    I tried using the scanner object in the method itself

    Java 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;
    	}
    and it gives me an error
    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

  15. #15
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    here's a cleaner way to do it
    Java 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
                }
            }
    as for input file names with spaces, you can use nextLine() instead of next() for your Scanner object

  16. #16
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    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?

    Java Code:
    String user_id = scan.next();  //Scan the input 
    // AND
    user_id = scan.nextLine();  //Scan the input
    Any specific reason?

    Change your hard coded message to something else,

    Java Code:
    System.out.println("The User Id is < 5 characters.");
    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... :)

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  17. #17
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Question

    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)

  18. #18
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You're welcome. Please let us know if it fixes your problem or not.
    Sir

    I have posted the changed code and the error i get doing it .

    Please help me in fixing the error , so that it proceeds further to ask for the file name with whole path .

  19. #19
    renu is offline Senior Member
    Join Date
    May 2010
    Posts
    117
    Rep Power
    0

    Default

    Sir

    Your code for the method help me to understand ..how to write simplier code.
    Thank You

    I am changing all my methods touse while(true) { ....} statments

    I might need help ..after i complete changing all the methods .

    Thank You again

Similar Threads

  1. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 04:09 AM
  2. Replies: 13
    Last Post: 04-16-2010, 03:51 AM
  3. Replies: 6
    Last Post: 03-01-2010, 08:00 AM
  4. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 02:59 AM
  5. Reading file data that contains no spaces
    By jdepue in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 05:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •