2d array problem

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

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

2d array problem

 
0
  #1
Nov 13th, 2008
What is wrong with this code. when it do system.out.println(matrix[0][0] i should get 1 char i get the entire line.

  1. int r =Integer.parseInt(line.substring(0));
  2. int c = 10;
  3. String[][] matrix = new String[r][c];
  4.  
  5. while (line !=null)
  6. {
  7. for(int i =0; i< r; i++)
  8. {
  9.  
  10. for (int j =0; j< c; j++)
  11. {
  12. line = br.readLine();
  13. matrix[i][j] = line;
  14. }
  15. }
  16. }
  17.  
  18. System.out.print(matrix[0][1]);
  19. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,492
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 519
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: 2d array problem

 
0
  #2
Nov 13th, 2008
Well, go figure:
  1. line = br.readLine();
  2. matrix[i][j] = line;
If you put the whole line in there, that's what you're going to get.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 156
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

Re: 2d array problem

 
0
  #3
Nov 13th, 2008
so what function can i use to read in a single char at a time?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: 2d array problem

 
0
  #4
Nov 14th, 2008
read the line, and within the loop, you just extract a single char from that line.
that's the one you put in your array, next loop, you read the next char and so on, till you've reached the end of the line
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC