943,644 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 478
  • Java RSS
Nov 13th, 2008
0

2d array problem

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007
Nov 13th, 2008
0

Re: 2d array problem

Well, go figure:
Java Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Nov 13th, 2008
0

Re: 2d array problem

so what function can i use to read in a single char at a time?
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007
Nov 14th, 2008
0

Re: 2d array problem

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
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is online now Online
2,482 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Can someone help me?
Next Thread in Java Forum Timeline: I need help with a program that prints random lines from a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC