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

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

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

#46 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2560
  • View blog
  • Posts: 10,693
  • Joined: 20-September 08

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

Posted 11 October 2011 - 08:10 AM

Quote

But, when I use other txt file (consists of letters),


Please attach THAT file
Was This Post Helpful? 0
  • +
  • -

#47 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 - 08:27 AM

here is a print screen. That's the only output.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#48 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

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

Posted 11 October 2011 - 08:32 AM

Alright.. this is not going anywhere fast. We need you to repost your code, and post a small example text from the file with letters.

I think that it is printing the reference and not the actual object but will not be able to tell unless you post your code and small example text from the letter file.
Was This Post Helpful? 0
  • +
  • -

#49 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 - 08:36 AM

View Postg00se, on 11 October 2011 - 08:10 AM, said:

Quote

But, when I use other txt file (consists of letters),


Please attach THAT file


here is my code:


import java.util.*;
import java.io.*;
 
public class Exercise{
	  public static void main(String args[]){
		Exercise x = new Exercise();
			
		
		System.out.println(x.ForestItem());
		
   }

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

           file = new Scanner(new File("account.txt"));
            while (file.hasNext()){
                line = file.nextLine();
                for (int j=0; j<line.length(); j++){
                    char c = line.charAt(j);
                    
                        gridCharArray[i][j] = c;
                  
                }
                i++;
				
            }
        }catch (Exception e){
            
			e.printStackTrace();
        }
        return gridCharArray;
    }
}


Attached File(s)

  • Attached File  gesh.txt (48bytes)
    Number of downloads: 42

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

Was This Post Helpful? 0
  • +
  • -

#50 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2560
  • View blog
  • Posts: 10,693
  • Joined: 20-September 08

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

Posted 11 October 2011 - 08:41 AM

Quote

That's the only output.


That's the result of printing char[]. If you want to see the characters for char[] 'c', then do

System.out.println(new String(c));

Was This Post Helpful? 1
  • +
  • -

#51 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

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

Posted 11 October 2011 - 08:43 AM

System.out.println(x.ForestItem());

I was right. That method, returns a a 2D array. return gridCharArray;

You print it, but when you just throw it in a System.out.println(Array[][]); it will print the reference that it is at, not its contents. If you are wanting to print its contents You need loop through it and print them out individually.

This post has been edited by Fuzzyness: 11 October 2011 - 08:43 AM

Was This Post Helpful? 1
  • +
  • -

#52 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7305
  • View blog
  • Posts: 12,161
  • Joined: 19-March 11

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

Posted 11 October 2011 - 08:47 AM

That is in fact an object address. The most likely case in this scenario is that you're trying to print a 2D array in a println() statement.

A char array will print as something like [s,t,r,i,n,g], but a 2D array will print as an object reference. I suspect you're calling goose's method and trying to print the result.

EDIT: not quite right. Here's a bit of test code

public static void main(String[] args) {
		char[] charArray= "String".toCharArray();
		char[][] charArrays= {charArray, charArray};
		System.out.println(charArray);
		System.out.println(charArray.toString());
		System.out.println(charArrays);
	}
which results in:

String
[C@addbf1
[[C@42e816

This post has been edited by jon.kiparsky: 11 October 2011 - 09:04 AM

Was This Post Helpful? 1
  • +
  • -

#53 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 - 08:49 AM

just to clear everything..

What I want to do is to read a file and then store its content to a 2d array, then return that array. then, i will create a method that will print the array (I havent done that part yet). :)
Was This Post Helpful? 0
  • +
  • -

#54 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

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

Posted 11 October 2011 - 08:51 AM

Then what you need to do is assign the array to another array of same type and just make a for loop.
char[][] gridCharsArray = x.ForestItem();
/* for loop to Print it out */

Was This Post Helpful? 1
  • +
  • -

#55 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7305
  • View blog
  • Posts: 12,161
  • Joined: 19-March 11

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

Posted 11 October 2011 - 08:54 AM

Or better still, print it in an enhanced for loop:

for (char[] array : x.ForestItem())
{
  System.out.println(array);
}


(BTW: hey, Goose, what's with the naming fail? Why the initial cap on ForestItem()?)
Was This Post Helpful? 1
  • +
  • -

#56 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2560
  • View blog
  • Posts: 10,693
  • Joined: 20-September 08

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

Posted 11 October 2011 - 09:02 AM

Quote

That's the result of printing char[].


Sorry -that should have said char[][]. If you want a quick and dirty way to visualize char[][] 'c', then

System.out.println(java.util.Arrays.deepToString(c));

Was This Post Helpful? 0
  • +
  • -

#57 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7305
  • View blog
  • Posts: 12,161
  • Joined: 19-March 11

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

Posted 11 October 2011 - 09:12 AM

Okay, so here's a case of "syntactic sugar causes cancer of the semicolon". I've run into this before, and each time if gets me, because you never run into this in real code, only in student code where people are making arrays and printing them.

But if you see this sort of problem, here's the rule:

If you have a char array, giving it as the argument to a print method will convert it to a String, but calling its toString() will call Object.toString().

This is undocumented, or poorly documented, and contrary to the expected behavior. Expected behavior is that if an object is passed as an argument to a println, you get that object's toString, whatever it is. Here, you get something different, and it doesn't make a lot of sense, and nobody tells you about it, so it's worth keeping in mind if you're reading a lot of student code.
Was This Post Helpful? 1
  • +
  • -

#58 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 12 October 2011 - 02:57 AM

I got it now. Thanks everyone for the help!
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4