#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Problem with scanner reading past end of line


    Hello all.

    I have started learning java and i need some help on an issue i am having. I am trying to create a program that writes 4 variables to a file (separated by commas ",") and then prints out the file line by line in a formatted manner

    I actually have it workign using string.tokenizer, however i know this is depricated and am trying to get it working using string.split(",") or .useDelimiter(",")

    the problem i am having is that my code seems to go past the end of line character picking up the first variable of the next line and adding it to the last variable of the previous line.

    I have tried several variations and i have no idea as to why it is doing this. any help at all would be appreciated. Below is the code i am using
    Code:
    FileReader fr = new FileReader("products.csv"); //new filereader isntance to open file
    		Scanner in = new Scanner(fr);
    			
    		Product array[] = new Product[Product.countLines("products.csv")];
    	
    		try {
    		
    				//Read data from the file 
    				
    				int i = 0;
    		while(in.hasNextLine()){
    			    in.useDelimiter(",");
    				tempProdCode = in.next();
    				System.out.println("tempProdCode is:"+tempProdCode);
    				tempProdDescr = in.next();
    				System.out.println("tempProdDescr is:"+tempProdDescr);
    				String test = in.next();
    				System.out.println("test value for tempProdCost is : "+test);
    				tempProdCost = Double.valueOf(test);
    				
    				tempProdUnit = in.next();
    				
    				array[i] = new Product(tempProdCode, tempProdDescr, tempProdCost, tempProdUnit);
                    System.out.println("end of one line from input file");
                    System.out.println("value of i is "+i);
    					System.out.println(array[i]);
    			i++;
    			if(in.hasNextLine()){
    				in.useDelimiter("\n");//to see the end of THE CURRENT LINE
    				in.next();//to read past the end of line character
    			}
    				
    		}
    		in.close();
    		
    			}//end of try 
    				
    				catch (Exception e){//Catch exception
    					System.err.println("Error: " + e);
    				}//end of catch
    forgot to add : the file is a csv file and the contents are in the format :
    P3847,Condensed Powdered Water,2.5,per packet
    K3876,Distilled Moonbeams,3.0,a dozen
    Z9983,Anti-Gravity Pills,12.75,for 60
    Powdered Fairy Dust,Q9245,30.45,per packet

    the output i am getting is here : http://snag.gy/9mBqI.jpg

    thanks for any help!
    Last edited by vramch; February 19th, 2013 at 07:57 AM. Reason: add url to output screenshot
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Please post a complete program that will compile and execute for testing.
    Also post the program's output that shows the problem. Add some comments to explain where the problem is.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Please post a complete program that will compile and execute for testing.
    Also post the program's output that shows the problem. Add some comments to explain where the problem is.



    The program does not error out on any one line. It compiles and runs, allows me to add to the file it is writing to and read from the file as well.

    It is going past the end of line of the file storing the information and adding the first entry of the next line to the end of the previous line, causing errors in the program output.

    Output :


    Enter product description : #
    tempProdCode is:P3847
    tempProdDescr is:Condensed Powdered Water
    test value for tempProdCost is : 2.5
    end of one line from input file
    value of i is 0
    P3847, Condensed Powdered Water, $2.50 per packet
    K3876.
    tempProdCode is:
    Z9983
    tempProdDescr is:Anti-Gravity Pills
    test value for tempProdCost is : 12.75
    end of one line from input file
    value of i is 1

    Z9983, Anti-Gravity Pills, $12.75 for 60
    Q9887.
    tempProdCode is:

    Error: java.util.NoSuchElementException

    Process completed.

    The output should be :
    Enter product description : #
    P3847 Condensed Powdered Water $2.50 per packet
    K3876 Distilled Moonbeams $3.00 a dozen
    Z9983 Anti-Gravity Pills $12.75 for 60
    Q9887 Powdered Fairy Dust $20.50 per pack

    Process completed.

    this is using string.tokenizier code which is commented out in the code displayed above
    Last edited by vramch; February 19th, 2013 at 02:05 PM. Reason: removed code
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Here is the output fro when I execute the code. Can you tell me where the problem is?
    Running: java Product


    Enter product description : #
    tempProdCode is:P3847
    tempProdDescr is:Condensed Powdered Water
    test value for tempProdCost is : 2.5
    end of one line from input file
    value of i is 0
    P3847, Condensed Powdered Water, $2.50 per packet
    K3876.
    tempProdCode is:
    Z9983
    tempProdDescr is:Anti-Gravity Pills
    test value for tempProdCost is : 12.75
    end of one line from input file
    value of i is 1

    Z9983, Anti-Gravity Pills, $12.75 for 60
    Q9887.

    0 error(s)
    One problem I see in the code is that it does not call the pri tStackTrace() method in the catch block to give more info when there is an exception.
    Last edited by NormR; February 19th, 2013 at 01:08 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    the code should look like :
    Enter product description : #
    P3847 Condensed Powdered Water $2.50 per packet
    K3876 Distilled Moonbeams $3.00 a dozen
    Z9983 Anti-Gravity Pills $12.75 for 60
    Q9887 Powdered Fairy Dust $20.50 per pack

    Process completed.

    It errors out when i compile it. Regardless of the platform - widows, osx or dos

    the line : Z9983, Anti-Gravity Pills, $12.75 for 60
    Q9887. <- is an error. It is printing the "." after the code Q9887 which is the beginning of the next line, instead of after "for 60" which is the end of the previous line

    it also does it on the line : P3847, Condensed Powdered Water, $2.50 per packet
    K3876.

    and it's printing 3 lines when there are 4 lines of products in the csv file
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    It errors out when i compile it.
    Please copy the full text of the compiler's error messages and paste them here.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Please copy the full text of the compiler's error messages and paste them here.
    apologies - i meant to say when i run it. It does not give any errors to compile.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The statement:
    n.useDelimiter(",");
    tells Scanner to use , for delimiters. I guess it takes you at your word and ignores other delimiters like line end. I don't use regular expressions very often and don't know what the pattern should be for both , and \n
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    The statement:
    n.useDelimiter(",");
    tells Scanner to use , for delimiters. I guess it takes you at your word and ignores other delimiters like line end. I don't use regular expressions very often and don't know what the pattern should be for both , and \n
    If i replace the loop which reads and prints the contents of the file with :

    Code:
    try{
    						int i=0;
    						Product[] tempProd = new Product [Product.countLines("products.csv")];
    					while ((line = in.nextLine()) != null) {
    						
    							  StringTokenizer st = new StringTokenizer (line, ",");
    							  while (st.hasMoreTokens ()) {
    											
    									tempProdCode = st.nextToken ();
    									tempProdDescr = st.nextToken ();
    									tempProdCost = Double.parseDouble(st.nextToken ());
    									tempProdUnit = st.nextToken ();
    									
    							tempProd[i] = new Product (tempProdCode, tempProdDescr, tempProdCost, tempProdUnit);
    							
    						System.out.println(tempProd[i]);
    							i++;
    							}//end of inner while loop assiging tokens to variables
    					}//end of outer while loop for reading file line by line
    				}//end of try 
    				
    				catch (Exception e){//Catch exception
    				//	System.err.println("Error: " + e);
    				}//end of catch
    it works perfectly and produces the desired result :

    Enter product description : #
    P3847, Condensed Powdered Water, $2.50 per packet.
    K3876, Distilled Moonbeams, $3.00 a dozen.
    Z9983, Anti-Gravity Pills, $12.75 for 60.
    Q9887, Powdered Fairy Dust, $20.50 per pack.

    Process completed.

    I don't understand why the other code is ignoring the end of line character when the older depreciated code works fine!
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    why the other code is ignoring the end of line character
    Because you told it to:
    Code:
      in.useDelimiter(",");
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Because you told it to:
    Code:
      in.useDelimiter(",");
    but that is splitting a string that is being read here : line = in.nextLine()

    this is supposed to read the line, then it should be split by in.useDelimiter(","); - so this should split the line that is read - which should end at the end of line character not so?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I think that line told the Scanner to use a comma to separate tokens being read from the file. It must have replaced the Scanner's normal separator of lineend. So lineend was just some more bytes that were being read from the file and could be part of a token.

    There is probably a regular expression you could use that would tell Scanner to use BOTH the comma AND the lineend character to separate tokens.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    ok thanks - let me do some reading up on regex!

IMN logo majestic logo threadwatch logo seochat tools logo