2d arrayist

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

2d arrayist

 
-1
  #1
Nov 12th, 2008
what is wrong with this code. it does not read the input file correctly.

[/code]
private void readFile()
{
String fName = "c:\\game.txt";
ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
{
try
{
File file = new File(fName);
Scanner in = new Scanner(file);

// for ( int i = 0; i < matrix.size(); i++ )
// matrix.add(new ArrayList<String>());

while (in.hasNextLine())
{
String line = in.nextLine();

for (int i =0; i<matrix.size(); i++)
matrix.get(i).add(line);

}
in.close();
}
catch(Exception e)
{
String s = e.toString();
}
}
}

[/code]
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: 2d arrayist

 
0
  #2
Nov 12th, 2008
Doesn't this answer your question?
http://www.daniweb.com/forums/thread156720.html
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: 2d arrayist

 
0
  #3
Nov 12th, 2008
Your question has already been answered on your other thread, and I've explained twice the mistake you are making on the code tags. Again, it's this:


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

not this:

[/code]
// paste code here
[/code]
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: 2d arrayist

 
0
  #4
Nov 12th, 2008
i tried the scanner thing and nothing.

  1. String fName = "c:\\game.txt";
  2. ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
  3.  
  4.  
  5.  
  6.  
  7. private void readFile()
  8. {
  9.  
  10. try
  11. {
  12. File file = new File(fName);
  13. Scanner in = new Scanner(new FileInputStream(fName));
  14.  
  15.  
  16. while (in.hasNextLine())
  17. {
  18. String line = in.nextLine();
  19.  
  20. for (int i =0; i<matrix.size(); i++)
  21. matrix.get(i).add(line);
  22. }
  23. in.close();
  24. }
  25. catch(Exception e)
  26. {
  27. String s = e.toString();
  28. }
  29.  
  30. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: 2d arrayist

 
0
  #5
Nov 12th, 2008
this is what im getting when i compile:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at Move.printFile(Move.java:87)
at Move.<init>(Move.java:40)
at Main.main(Main.java:19)
Java Result: 1
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: 2d arrayist

 
0
  #6
Nov 12th, 2008
line is getting a value, but for some reason matrix is not being populated. "i" always stays at 0. and there is info on the file it is reading from
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: 2d arrayist

 
1
  #7
Nov 12th, 2008
Mark your other thread solved please.

ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();

  1. matrix.get(i).add(line);

I don't see anywhere where you ever create an ArrayList of String and add it to matrix, so there's nothing to "get". You should create an ArrayList of String, add the Strings to it, then add that ArrayList to matrix. You are currently "getting" something that has never been added.

  1. ArrayList <String> arraylist = new ArrayList<String> ();
  2. String line;
  3. // read a line from file, store in line
  4. arraylist.add (line);
  5. matrix.add (arraylist);
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 159
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: 2d arrayist

 
0
  #8
Nov 12th, 2008
got it working now, but it is printing out the wrong thing.

  1. private void printFile()
  2. {
  3.  
  4. for(int i=0; i<matrix.size(); i++)
  5. {
  6. System.out.println(matrix.get(i));
  7. }
  8. }

it is supposed to print

X XXXXX X
XXXXXXXXX
XXX X X X
X X X X X
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX
XXXXXXXXX

it prints
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
[XXXXXXXXX, XXX X X X , XXX X X X , X X X X X, X X X X X, X X X X X, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX, XXXXXXXXX]
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: 2d arrayist

 
0
  #9
Nov 12th, 2008
Because that is the toString() representation of an ArrayList, which is what you are printing.

If you want to show it differently then iterate each list and print it as you see fit.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 439 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC