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

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

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

#31 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,887
  • Joined: 19-March 11

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

Posted 11 October 2011 - 05:14 AM

The blah blah part is useful, especially the part where it tells you which line of the code the Exception came from.

In this case there's a reasonable guess: if your file has more than ten lines, this will go out of bounds.
Was This Post Helpful? 0
  • +
  • -

#32 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:29 AM

Ooooh. :D

So, I changed the:
 for (int j=0; j<10; j++){


to:

 for (int j=0; j<line.length(); j++){


and now it still compiles but the output are just the nonsense characters. :|

Still not getting my desired output :(
Was This Post Helpful? 0
  • +
  • -

#33 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

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

Posted 11 October 2011 - 05:44 AM

Try something like the following


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

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

	    while (i < gridCharArray.length && file.hasNextLine()) {
		line = file.nextLine();
		gridCharArray[i++] = line.toCharArray();
	    }
	} catch (Exception e) {
	    System.out.println("File does not exist.");
	}
	finally {
	    try { file.close() ; } catch(Exception e) { /* ignore */ }	
	}

	return gridCharArray;
    }


Was This Post Helpful? 0
  • +
  • -

#34 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 - 06:02 AM

Hello g0ose! I just tried the code you gave but the output are still nonsense characters :|

I was thinking, those elements which wont contain any characters would just contain the space characters... But i dont know how to code it.. :|
Was This Post Helpful? 0
  • +
  • -

#35 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

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

Posted 11 October 2011 - 06:18 AM

Well that's probably something to do with the contents of the file. Can you attach it to this question please?
Was This Post Helpful? 0
  • +
  • -

#36 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 - 06:31 AM

How can I attach a file? Sorry for asking this stupid question :D I cant seem to find a link for attaching file.
Was This Post Helpful? 0
  • +
  • -

#37 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

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

Posted 11 October 2011 - 06:35 AM

You need to select the button marked 'Use Full Editor' to do that
Was This Post Helpful? 0
  • +
  • -

#38 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 - 06:44 AM

I am required to read and store 5 files to 2d array. This is just one of them:

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#39 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,887
  • Joined: 19-March 11

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

Posted 11 October 2011 - 06:56 AM

Cute. Are these the nonsense characters?

           ]z
           `@@_
            @@@L
      .d@L,]@@@@L,
-z__   ]@@@a@@@@@@_
 `@@@@zza@@@@@@@@@@L
  `]@@@@@@@@@@@@@@@@@_
    `@@@@@@@@@@@@@@@@@L
     `-@@@@@@@@@@@@@@@@'
       `@@@@@@@@@@@@@@[
        `@@@@@@@@@@@@@[
          ]@@@@@@@@@@@[
           "~~~~-@@@@@@,
                  "~-@@@_
                     ~@@@L
                      `@@@L_
                       `~@@@L
                         `@@@z,
                          `]@@@_
                            `@@@z
                             `]@@L_
                               ~@@@z
                                `@@@z,
                                 `]@@@L
                                   `@@@z
                                     ]@@L,
                                      ~@@@z
                                       "@@@z
                                        `-@@@_
                                          ~@@@_
                                           `@@@z
                                            `-@@@_
                                              ]@@@_
                                               "@@@z
                                                `]@@L,
                                                  `@@@L
                                                   `@@@z,
                                                    `-@@@_
                                                      `@@@L
                                                       `@@@L    ]e
                                                         ~@@b_  a@b
                                                          `@@@e]@@L
                                                    -zzzz___@@@U@@@,
                                                      "~-@@@@@@@@@@@
                                                         `~-@@@@@@@@L
                                                            "~-@@@@@@,
                                                               "~@@@@L
                                                                 `~@@@e
                                                                    ~@@_
                                                                      ~@

Was This Post Helpful? 0
  • +
  • -

#40 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

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

Posted 11 October 2011 - 06:58 AM

Looks like an ascii art file. There are 50 lines in that file of up to 70-odd characters (symbols). You're going to have to size the first dimension of your array accordingly
Was This Post Helpful? 0
  • +
  • -

#41 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 - 07:07 AM

View Postjon.kiparsky, on 11 October 2011 - 06:56 AM, said:

Cute. Are these the nonsense characters?



Nope.. Haha. :D The nonsense characters are something like this: @@][76@@[#


:D

Haha. My prof really knows these stuffs. Making me crazy. LOL

View Postg00se, on 11 October 2011 - 06:58 AM, said:

Looks like an ascii art file. There are 50 lines in that file of up to 70-odd characters (symbols). You're going to have to size the first dimension of your array accordingly


Hmmm.. Is there no way wherein I dont have to specify a dimension of the array? Because I need to run 5 different txt files using just this one method. And those files have different dimensions..
Was This Post Helpful? 0
  • +
  • -

#42 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,343
  • Joined: 20-September 08

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

Posted 11 October 2011 - 07:12 AM

You can use List<Character> and turn it into Character[] later (or char[] if you really have to)
Was This Post Helpful? 0
  • +
  • -

#43 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,887
  • Joined: 19-March 11

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

Posted 11 October 2011 - 07:44 AM

Quote

Nope.. Haha. The nonsense characters are something like this: @@][76@@[#


That does look a little like the content of your file, honestly.
(which is what I posted)

This post has been edited by jon.kiparsky: 11 October 2011 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#44 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 - 07:57 AM

View Postjon.kiparsky, on 11 October 2011 - 07:44 AM, said:

Quote

Nope.. Haha. The nonsense characters are something like this: @@][76@@[#


That does look a little like the content of your file, honestly.
(which is what I posted)


Oooh. I didnt notice that. But, when I use other txt file (consists of letters), it still shows those kind of nonsense characters :)
Was This Post Helpful? 0
  • +
  • -

#45 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,887
  • Joined: 19-March 11

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

Posted 11 October 2011 - 08:04 AM

Okay, enough playing around.

Please run the code on the file you've just posted (account.txt) and post the complete output so we can see exactly what you're looking at. "Something like ..." isn't going to cut it.
Was This Post Helpful? 0
  • +
  • -

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