Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone, so I am currently learning Java, and I have been working in a RaggedArray.java class which I have completed most of it. The problem is that I do not what to add to the public String toString Method. The file rag.txt:

3
/**/
17 39 82 108
117 8 25
47 58 63 72 99


is read into the field int [ ] [ ] rag by the program RaggedArray.java. I must provide code to the String toString() method so that it will return a string representing rag, i.e.,

17 39 82 108
117 8 25
47 58 63 72 99


with each int being right-justified in a field of width four, and with no line-feed character after the last line.

The RaggedArray.java is listed here:

public class RaggedArray {
    /**/
    private int [ ][ ] rag;
    /**/
    public static void main ( String [] arg ) {
    System.out.println( new RaggedArray() );
    return;
  }
    /**/
    public RaggedArray() {
    initFromFile("rag.txt");
  }
    /**/
    public String toString () {
    /**/
    String lf;
    /*
     *  lf == line-feed character
     */
    lf = System.lineSeparator();
    /*
     *  I have to write code here to finish my program.
     */
  }
    /**/
    private void initFromFile ( String fileName ) {
    /**/
    FileInput fi;
    int numRows;
    /**/
    fi = new FileInput(fileName);
    numRows = fi.readLineInt();
    rag = new int [numRows][];
    fi.readLine();
    for ( int i = 0; i < numRows; ++i ) rag[i] = fi.readLineInts();
    fi.close();
    /**/
    return;
  }
}


What I have tried:

I have written the RaggedArray to fill in the requirements, but I got stuck in what to write for the string toString method.
Posted
Updated 24-Feb-22 10:43am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900