Results 1 to 8 of 8
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Only reads one char value from a .txt file

    There is a .txt file with the following data:
    14 14 // they specify the no. of rows m and columns n of the word search grid

    S I N S N O W S T O R M W I
    K L N T S N O W M A N E F E
    A R E C A L P E R I F R I E
    T C G I C A N D L E E R M K
    E F H B G T A O C E E Y I A
    S R R I L H T U Z P A C T L
    T A N O L I B E R I G H T F
    B C D H S L Z E T A N R E W
    O S D E E T T Z L D R I N O
    O E D V L N I S A L S S S N
    T B O L I S C Y Y R S T E S
    S H H W O L E L O W D M C A
    S N A D E C E M B E R A D L
    E L T I S E A S O N G S H T

    This is just half the data and my code, at the moment, is just to extract these values and create a 2D-array.
    But, I am getting a 14x14 grid output of just the char value E. I can't figure out what is wrong.

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    import java.lang.*;
    public class WordSearch
    {
      public static void main(String[] args) throws IOException
      {
        // Read data from .txt file
         Scanner sc = new Scanner( new File ("hidden.txt"));
       
       //Create output file
        
         File outFile = new File("found.txt");
         FileWriter fWriter = new FileWriter (outFile);
         PrintWriter pWriter = new PrintWriter (fWriter);
        
       //read size of grid
         int m = sc.nextInt();//row
         int n = sc.nextInt();//column     
    
       //the no. of words
       //  int k = sc.nextInt();
    
         char [][] grid = new char [m][n];//array to store characters 
    
       //variable to retrive each line
         String line = "";
    
      //fill the array "Grid" with char values
        while(sc.hasNextLine())
        {
          line = sc.nextLine();
          for(int r = 0; r<grid.length; r++){
           for(int c = 0; c<grid[0].length; c++){
            for(int i = 0; i< line.length(); i = i + 2){  
              grid [r][c] = line.charAt(i); 
           }
          }
         }
          
        }
    
      //code test by displaying
        for(int r = 0; r<grid.length; r++){
          for(int c = 0; c<grid[0].length; c++) {
             System.out.print(grid[r][c]+" "); 
         }
           System.out.println();
        }
     
       sc.close();
       pWriter.close();
      }
    }

  2. #2
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Only reads one char value from a .txt file

    Here is the output if you did not understand:

    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: Only reads one char value from a .txt file

    The problem lies within the loops starting at line 30. You are using a while loop to read in a line and then using nested loops to fill in the grid. You need to re-think the process:

    1. read in a line
    2. if done, continue to 5.
    3. populate current row
    4. go to 1.
    5. rest of program

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Only reads one char value from a .txt file

    Ok. But, how would I read one line after the other without using the while(..) statement?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Only reads one char value from a .txt file

    The while loops is fine.
    It's the arrangment inside that that's wrong.
    You are reading the line, then looping over the entire 2D array. For each entry n the array you are then looping over each character in the line and setting that entry to the character. Everytime.

    As jim says, you need to treat each line read in as a row, so have something to keep track of which row you are working on.
    Skip the loop over the rows and just have a single loop which goes up to the length of a row. Use the index to index into the character and the column.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Only reads one char value from a .txt file

    I kind of get what you guys are trying to say now.

    But what exactly do you mean by this "Skip the loop over the rows..."?

    Also, since the length of the string "line" would be much more than that of the no. of columns required I wouldn't be able to "Use the index to index into the character and the column," as you suggested.
    for e.g. in the case I have presented above the length of the string line for any row would be 27 while the no. of columns is just 14.

    Would you mind demonstrating the code? I think I would understand better that way.

    -Thank You

  7. #7
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Re: Only reads one char value from a .txt file

    I did try this modified version of code based on what I understood from what you guys suggested:
    Java Code:
    for(int r = 0; r < grid.length; r++){
          for(int c = 0; c < grid[0].length; c++){
            while(sc.hasNextLine()){  
               line = sc.nextLine();
               grid [r][c] = line.charAt(c);  
           }
          }
         }
    But I get this error:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:695)
    at WordSearch.main(WordSearch.java:33)

    33 => grid [r][c] = line.charAt(c);

    I believe this code will be more along the lines of what you guys are saying:

    Java Code:
    for(int r = 0; r < grid.length; r++){
            line = sc.nextLine();
          for(int c = 0; c < grid[0].length; c++){
            for(int i = 0; i < line.length(); i++)
               grid [r][c] = line.charAt(i);
             }
         }
    But still I am one row is missing and now it is reading in one char per line i.e. the output looks like this:

    c:\CS312>java WordSearch

    I I I I I I I I I I I I I I
    E E E E E E E E E E E E E E
    E E E E E E E E E E E E E E
    K K K K K K K K K K K K K K
    A A A A A A A A A A A A A A
    L L L L L L L L L L L L L L
    F F F F F F F F F F F F F F
    W W W W W W W W W W W W W W
    O O O O O O O O O O O O O O
    N N N N N N N N N N N N N N
    S S S S S S S S S S S S S S
    A A A A A A A A A A A A A A
    L L L L L L L L L L L L L L
    Last edited by abi; 04-12-2013 at 07:54 PM.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: Only reads one char value from a .txt file

    I would suggest you incorporate the hasNextLine method into your for loop, like this:
    Java Code:
    for (int r = 0; sc.hasNextLine(); r++) {
    
    }
    Get rid of the middle for loop and set c = 0 there instead. Then I would only increment c after you store the character. Just make certain you don't store spaces.

    Regards,
    Jim
    Last edited by jim829; 04-12-2013 at 08:41 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. FileReader reads all data as int
    By fred2028 in forum New To Java
    Replies: 25
    Last Post: 09-14-2011, 07:50 PM
  2. problem with reading a text file with special char
    By kishan.java in forum New To Java
    Replies: 1
    Last Post: 04-10-2011, 09:30 AM
  3. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  4. Replies: 1
    Last Post: 11-12-2008, 03:43 PM
  5. Replies: 9
    Last Post: 09-11-2008, 02:51 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
  •