How to store characters from a .txt file into a 2D array

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4

57 Replies - 6842 Views - Last Post: 12 October 2011 - 02:57 AM Rate Topic: -----

#16 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,150
  • Joined: 19-March 11

Re: How to store characters from a .txt file into a 2D array

Posted 10 October 2011 - 07:30 PM

What is it that you don't get? If you ask a question, it's easier to answer it.
Was This Post Helpful? 0
  • +
  • -

#17 pilaso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 10-October 11

Re: How to store characters from a .txt file into a 2D array

Posted 10 October 2011 - 08:05 PM

I have this:

    while ((strLine = br.readLine()) != null)   {
 
		a=strLine.length();

		char line[]=new char[a];
		for(int b=0; a<=strLine.length(); a++)
		System.out.println(line[b]);
	 
	 
    }



I still cant store it. (And that's just 1D array)
Was This Post Helpful? 0
  • +
  • -

#18 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,150
  • Joined: 19-March 11

Re: How to store characters from a .txt file into a 2D array

Posted 10 October 2011 - 08:10 PM

First of all, if you have a series of objects, you can put them into an array. If you give me a series of integers, I can put them in an array - I just increment an index, and pop them into an int[] array. These char arrays are objects, and you're getting them in a series, so you can pop them into an array if you really want to.

But again - why do you need an array of arrays?
Was This Post Helpful? 0
  • +
  • -

#19 pilaso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 10-October 11

Re: How to store characters from a .txt file into a 2D array

Posted 10 October 2011 - 08:24 PM

Well, i just have to have it on an array of arrays.

In my code, why wont the chars be not printed when I print line[b]? how can i store the chars in one line into line[]?
Was This Post Helpful? 0
  • +
  • -

#20 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,150
  • Joined: 19-March 11

Re: How to store characters from a .txt file into a 2D array

Posted 10 October 2011 - 08:30 PM

Quote

Well, i just have to have it on an array of arrays.


Is this a requirement of the assignment? If so, then just say so: the teacher is the customer, and the customer is always right.

Quote

In my code, why wont the chars be not printed when I print line[b]? how can i store the chars in one line into line[]?


Kiparsky's seventh law of debugging: Look at what your code does, not at what it doesn't do.

while ((strLine = br.readLine()) != null)   {  // while there are lines
                                // for each line:
  a=strLine.length();             // get the length of the line and call it a

  char line[]=new char[a];        // declare an array of chars as long as the line
  for(int b=0; a<=strLine.length(); a++)  
  System.out.println(line[b]);     // and print each character that's in the new array
}


So you create an array of char and then you print each element of it. But you haven't put anything into it to print.

This post has been edited by jon.kiparsky: 10 October 2011 - 08:31 PM

Was This Post Helpful? 0
  • +
  • -

#21 pilaso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 10-October 11

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 03:23 AM

I have this now:

        try{
		
           file = new Scanner(new File("accounts.txt"));
            while (file.hasNext()){
                line = file.nextLine();
                for (int j=0; j<10; j++){
                    char c = line.charAt(j);
                    
                        gridCharArray[i][j] = c;
                   
                }
                i++;
            }




The good thing is that it compiles already. But then, when I run it, it says that the file cant be found and then prints some nonsense characters. Cant seem to point out where I went wrong. Help please? :)

This post has been edited by pilaso: 11 October 2011 - 03:24 AM

Was This Post Helpful? 0
  • +
  • -

#22 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1909
  • View blog
  • Posts: 3,954
  • Joined: 11-December 07

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 03:38 AM

Well, it's telling you what's wrong. It can't find the file. I'm assuming the file does exist so that means it is looking in the wrong place, or the file is in the wrong place.

Java will look in the current working directory for the file. If it is somewhere else, you will need to either move it there, use the full path to the file or use the relative path to the file.
Was This Post Helpful? 0
  • +
  • -

#23 pilaso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 10-October 11

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 03:43 AM

Yes, my file is in the current directory... :( And yes, it is existing.

What else could be wrong?
Was This Post Helpful? 0
  • +
  • -

#24 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1909
  • View blog
  • Posts: 3,954
  • Joined: 11-December 07

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 03:56 AM

Then you have either misspelled it or the current directory is not where you think it is. Try using the full path.
Was This Post Helpful? 0
  • +
  • -

#25 pilaso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 10-October 11

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 04:24 AM

It really wont..

Is this right though?

file = new Scanner(new File("accounts.txt"));



the output is always like this:

File does not exist.
[[$@@$%7]8@@


:(
Was This Post Helpful? 0
  • +
  • -

#26 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1909
  • View blog
  • Posts: 3,954
  • Joined: 11-December 07

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 04:32 AM

Well, something is obviously wrong. Is that your own error message? Where does it come from?
Was This Post Helpful? 0
  • +
  • -

#27 pilaso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 10-October 11

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 04:35 AM

The "File does not exist" is my error message but the weird characters arent mine :(

here's my code:

public static char[][] ForestItem(){
     
        Scanner file;
        String line;
        int i = 0;
        char[][] gridCharArray = new char[10][10];
        try{

           file = new Scanner(new File("accounts.txt"));
            while (file.hasNext()){
                line = file.nextLine();
                for (int j=0; j<10; j++){
                    char c = line.charAt(j);
                    
                        gridCharArray[i][j] = c;
                   
                }
                i++;
            }
        }catch (Exception e){
            System.out.println("File does not exist.");
        }
        return gridCharArray;
    }

This post has been edited by pilaso: 11 October 2011 - 04:37 AM

Was This Post Helpful? 0
  • +
  • -

#28 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1909
  • View blog
  • Posts: 3,954
  • Joined: 11-December 07

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 04:43 AM

catch (Exception e){
  System.out.println("File does not exist.");
}



From what you are saying about the file definitely being in the right place, it could be your error message that's wrong. You can use the exception to print out a more meaningful error message:

catch (Exception e){
  //System.out.println("File does not exist.");
  e.printStackTrace();
}


Was This Post Helpful? 0
  • +
  • -

#29 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,150
  • Joined: 19-March 11

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 04:54 AM

If you want to test where your "current directory" actually is, do this:

File testFile = new File(".");    // create a file on the current directory
System.out.println("Current directory is "+testFile.getAbsolutePath); 
                                        //print the location of that file
System.out.println("Contents of current directory:");
for (String fileName: testFile.list())  // iterate over the directory contents
  System.out.println(fileName);         // and print them



The current directory isn't always where you think it is.
Was This Post Helpful? 0
  • +
  • -

#30 pilaso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 10-October 11

Re: How to store characters from a .txt file into a 2D array

Posted 11 October 2011 - 05:09 AM

Already did that and the file is definitely in the directory..


I followed what cfoley said and now it says:

java.lang.StringIndexOutofBoundsException: String index out of range blah blah

and then below that there is still the nonsense characters.

What does that mean? I think I got it wrong with the:

char[][] gridCharArray = new char[10][10];



because the file isnt really exactly 10x10. But since that file doesnt have the same numbers of columns per line, I just typed 10 (for maximum). So, some elements in the array wont contain anything.. maybe that's why it's in error..?

:|
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • 4