why won't this work? i have a txt file that i need to read into a matrix. i want to read into a char array so i can analyze each char separate.

String fname = "c:\\game.txt";
char[][] matrix = new char[4][4];

private void readFile() 
    {
        try
        {
            Scanner s = new Scanner(this.fname);
            char next ;


            for(int i = 0; i < matrix.length; i++)
            {
              for(int j =0; j < matrix.length; j++)
              {        
                    while(s.hasNext() == true)
                    {

                            this.matrix[i][j] = next;

                    }
              }
            } 
        }
        catch(Exception e)
        {
            String s = e.toString();
            System.out.println(s);
        }


    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I think you would find it easier to read the input into a String and use the charAt() method to read each char in the String.

changed matrix and next to string. now it compiles but when i try running it goes into an endless loop?

Close on the code tags:

[code] // not [/code] here // paste code here

[/code]

Can you post the updated code and an input file?

String data;
String[][] matrix = new String[4][4];


private void readFile() 
    {
        try
        {
            Scanner s = new Scanner(this.fname);
            String next =null;


            for(int i = 0; i < matrix.length; i++)
            {
              for(int j =0; j < matrix.length; j++)
              {        
                    while(s.hasNext() == true)
                    {

                            this.matrix[i][j] = next;

                    }
              }
            } 
        }
        catch(Exception e)
        {
            String s = e.toString();
            System.out.println(s);
        }


    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Please format and use code tags.

[code]

// paste code here

[/code]

The first tag is [code]

The next is [/code]

There's a slash on the second, but not the first.
I don't know what fname is, but if it's a String and it's a filename and you are reading from a file, you probably want this:

Scanner s = new Scanner(new File (fname));

Next, look at this code:

while(s.hasNext() == true)
{
     this.matrix[i][j] = next;
}

i and j never change, so you are simply overwriting matrix[i][j] continuously. Note that next never changes from null anywhere and you never read the line. You just keep testing s over and over again, but never do anything with it. You probably want something like this:

for (int i = 0; i < matrix.length; i++) 
            {
                for (int j = 0; j < matrix.length; j++) 
                {
                    if (s.hasNext()) 
                    {
                        next = s.nextLine();
                        this.matrix[i][j] = next;
                    }
                    else
                        matrix[i][j] = "no more data";
                }
            }

Note the if instead of the while so you don't keep over-writing matrix[i][j] , and notice that I actually read from the file with s.nextLine () instead of just continually testing it.

class Hb
{


 static char[][] matrix = new char[4][4];
public static void main(String args[])
{
    System.out.println("i am in main");
    System.out.println(matrix.length);
    new Hb().readFile();
    System.out.println(new Hb().matrix[0][0]);
}

private void readFile()
{
try
{

    FileInputStream fi=new FileInputStream("c:/game.txt");
Scanner s = new Scanner(fi);
char next ;


 while(s.hasNext() == true)
 {
     String fname=s.next();
     System.out.println(fname);
for(int i=0,k=0; i < matrix.length && k<fname.length(); i++)
{
for(int j=0; j < matrix.length; j++)
{

if(k<fname.length())
matrix[i][j] = fname.charAt(k++);
System.out.println(matrix[i][j]);
//System.out.println(s.next());
}
System.out.println("hi \n");
}
}

}
catch(Exception e)
{
String s = e.toString();
System.out.println(s);
}

}
}

you can try the above code this will help you on your issue. " Scanner s = new Scanner(this.fname)" this line will read the string which you are passing here fname. to read the file date we need use IO package(i.e.File related classes).

Edited 3 Years Ago by Reverend Jim: Fixed formatting

if you find any more please inform

String data;
String[][] matrix = new String[4][4];


private void readFile() 
    {
        try
        {
            Scanner s = new Scanner(this.fname);
            String next =null;


            for(int i = 0; i < matrix.length; i++)
            {
              for(int j =0; j < matrix.length; j++)
              {        
                    while(s.hasNext() == true)
                    {

                            this.matrix[i][j] = next;

                    }
              }
            } 
        }
        catch(Exception e)
        {
            String s = e.toString();
            System.out.println(s);
        }


    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

When you create a Scanner object, you have to do it with an input stream. The name of the file is not an input stream. Do this.

Scanner input = new Scanner(new FileInputStream(fname));

This article has been dead for over six months. Start a new discussion instead.