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.
57 Replies - 4304 Views - Last Post: 12 October 2011 - 02:57 AM
#31
Re: How to store characters from a .txt file into a 2D array
Posted 11 October 2011 - 05:14 AM
#32
Re: How to store characters from a .txt file into a 2D array
Posted 11 October 2011 - 05:29 AM
Ooooh. 
So, I changed the:
to:
and now it still compiles but the output are just the nonsense characters. :|
Still not getting my desired output
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
#33
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;
}
#34
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.. :|
I was thinking, those elements which wont contain any characters would just contain the space characters... But i dont know how to code it.. :|
#35
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?
#36
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
I cant seem to find a link for attaching file.
#37
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
#38
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)
-
account.txt (2.12K)
Number of downloads: 16
#39
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
~@@_
~@
#40
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
#41
Re: How to store characters from a .txt file into a 2D array
Posted 11 October 2011 - 07:07 AM
jon.kiparsky, on 11 October 2011 - 06:56 AM, said:
Cute. Are these the nonsense characters?
Nope.. Haha.
Haha. My prof really knows these stuffs. Making me crazy. LOL
g00se, 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..
#42
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)
#43
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
#44
Re: How to store characters from a .txt file into a 2D array
Posted 11 October 2011 - 07:57 AM
jon.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
#45
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.
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.
|
|

New Topic/Question
Reply




MultiQuote



|