Results 1 to 7 of 7
  1. #1
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Help parsing file

    Hey im creating a sudoku solving application in Java.

    my problem is reading in a file, the file is in .sud format.

    the problem i have is that my Scanner is reading the file as 1 continuous line but it is fact lines long. any idea why?

    Also how would i get it to say "if there is a space, replace that space with the 0 char"

    This is the file i have to use, i didnt create the file my teacher did

    So far i have

    This is a test method before i start the work properly i want to be sure i can read and print the file

    Java Code:
    jfc = new JFileChooser();
    fileName = jfc.getSelectedFile();
    
    Scanner scan = new Scanner(fileName);
    for(int i = 0; i < 9; i++ {
    for(int j = 0; j < 9; j++ {
    String s = scan.nextLine();
    s.replaceAll("\\s", "0");
    }
    }
    System.out.print(s);
    Input File

    Java Code:
       5 4
     59   16
     8     3
    4   3   6
       1 9
    5   7   8
     7     9
     62   45
       8 7
    Which i want to read as:

    000500400
    005900160
    008000300
    400030060
    001090000
    500700800
    070009000
    062000500
    080070000


    imagine this in a sudoku grid, spaces should be empty squares in the grid

    output of my program:
    Java Code:
       5 4
     59   16
     8     3
    4   3   6
       1 9
    5   7   8
     7     9
     62   45
       8 7
    Then i get noSuchElementException after 1 iteration, it reads it all as one line
    Last edited by JosAH; 10-09-2013 at 07:55 PM. Reason: added [code] ... [/code] tags

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Help parsing file

    Strings are immutable. So this line:

    s.replaceAll("\\s", "0");

    Doesn't actually do anything. You have to save the String value returned from the function to keep track of it.

    Edit: Also, you're reading an entire line each time in that loop. How many times does that inner loop execute in total? How many lines does the file have? Step through this with a debugger, or at least add some print statements, to make it more obvious what's going on.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Help parsing file

    ah i see!

    i added:

    Java Code:
    String result = s.replaceAll("\\s", 0");
    which solved my problem,

    The inner loop is executing the correct number of time

    The outer loop (i) is only executing once, which is causing my error.

    The file im reading has 9 lines in it, is the replace all removing the new line character or something?

    maybe i could try

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Help parsing file

    I recommend posting an SSCCE of your updated code, that way we're all looking at the exact same thing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Help parsing file

    Java Code:
    rows = 9;
    		columns = 9;
    		int x;
    		String result;
    		try {
    
    			Scanner scan = new Scanner(fileName);
    			for(int i = 0; i < columns; i ++){
    				for(int j = 0; j < rows; j ++){
    					String s = scan.nextLine();
    					result = s.replaceAll("\\s", "0"); //used to track empty "grid squares" in the file
    					System.out.println(result);
    
    				}
    
    				System.out.println("no of i exec's " + i);
    
    
    			}
    
    		
    
    		}
    
    		catch ( IOException e) {
    
    			e.printStackTrace();
    			System.out.println(fileName);
    
    		}
    i only executes once, ive used the de bugger cant see why, file has 9 lines

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Help parsing file

    Why are you using nested loops that iterate row and column times? Wouldn't the number of times your code loops depend upon the number of lines in the input file? Wouldn't a while loop that keeps iterating as long as there is a line to read be better?

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Help parsing file

    Running this code, which is just a subset of yours:

    Java Code:
    public class Main {
     
        public static void main(String[] args) {
        	
        	int total = 0;
        	
        	for(int i = 0; i < 9; i ++){
                for(int j = 0; j < 9; j ++){
                	total++;
                }
            }
        	System.out.println(total);
        }  
    }
    It prints out 81. Is that how many lines your file has?

    Your print statement is inside the outer loop, so you aren't seeing it every time the inner loop runs.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: 09-28-2011, 09:33 AM
  2. Parsing XML file using Xpath in jdk1.4
    By ziggy in forum New To Java
    Replies: 7
    Last Post: 02-27-2011, 12:55 PM
  3. HTML FILE parsing.
    By makpandian in forum New To Java
    Replies: 8
    Last Post: 11-02-2010, 03:00 PM
  4. parsing xml file
    By mp.saaravanan in forum New To Java
    Replies: 1
    Last Post: 05-02-2010, 02:55 PM
  5. Parsing XML File
    By rstar7 in forum XML
    Replies: 1
    Last Post: 10-10-2008, 03:31 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
  •