Results 1 to 4 of 4
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default Help with figuring out the output

    Hey, the following string method does exactly what I been asked to do, but the problem is that I need to adapt it exactly as written on a tester class, that suppose to check my outputs against the outputs on the tester.
    Although the task does the same if it is not equal it won't pass.

    Should be equal to this:
    String = "222\t147\t183\n251\t249\t195\n2\t253\t254\n";

    This is the code I wrote:

    public String toString (){
    String result ="";
    for (int row=0; row<arrayS.length; row++) {
    for (int column=0; column<arrayS[row].length; column++){
    result = result + arrayS [row] [column] +"\t";
    }
    result = result + "\n";
    }
    return result;
    }


    // Thank you very much for helping.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Of course, if you generate that String, it will look something like this on output to the console:
    Java Code:
    222     147     183
    251     249     195
    2       253     254
    -Gary-

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    result = result + arrayS [row] [column] +"\t";
    well ,i think it's because you are always placing the "\t" on, even on the end of the last element on the row, so the result would look like

    "222\t147\t183\t\n251\t249\t195\t\n2\t253\t254\t\n";
    which is going to not match the test string right.

    so instead,
    Java Code:
    public String toString (){
    String result ="";
    for (int row=0; row<arrayS.length; row++) {
      for (int column=0; column<arrayS[row].length; column++){
        result = result + arrayS [row] [column];
        [B]if ((column + 1) < arrayS[row].length) {
          // only append \t if not the last column
          result += "\t";
        }[/B]
      }
    result = result + "\n";
    }
    return result;
    }
    Last edited by travishein; 12-13-2010 at 01:19 AM.

  4. #4
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default Perfect!!!

    Thank you very much for your efficient and quick response, it's working! :)

Similar Threads

  1. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  2. Replies: 3
    Last Post: 06-10-2010, 03:42 PM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  4. different output
    By pro85 in forum Java Applets
    Replies: 6
    Last Post: 08-05-2008, 01:42 PM
  5. figuring out the current colour of my appelet
    By vortexprogramming in forum New To Java
    Replies: 2
    Last Post: 07-15-2008, 04:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •