Need help writing a toString

I have no idea how to go about writing the toString method for this code. I have to print the array that provides the longest sequence. I would appreciate your help.

Here is the code:import java.io.FileReader;

import java.io.IOException;

import java.util.List;

import java.util.ArrayList;

import java.util.Scanner;

import java.util.StringTokenizer;

class Grid

{

privateint [ ][ ] mainGrid;

public Grid( String file )throws IOException

{

FileReader fr =new FileReader( file );

Scanner scan =new Scanner( fr );

ArrayList<ArrayList><Integer>> numR =new ArrayList<ArrayList><Integer>>( );

while( scan.hasNextLine( ) )

{

String line = scan.nextLine( );

StringTokenizer st =new StringTokenizer ( line );

String number;

ArrayList<Integer> numC =new ArrayList<Integer>( );

while( st.hasMoreTokens( ) )

{

number = st.nextToken( );

int num = Integer.parseInt(number);

numC.add(num);

}

numR.add( numC );

}

int height = numR.size();

int width = numR.get( 0 ).size();

mainGrid =newint [height][width];

for(int i = 0; i < height; i++)

{

for(int j = 0; j < width; i++)

{

mainGrid [ i ][ j ] = numR.get( i ).get( j );

}

}

}

privateclass Position

{

privateint row;

privateint col;

public Position(int r,int c )

{

r = row;

c = col;

}

public List<Position> getAdjacents( )

{

int lowRow = row != 0 ? row - 1 : 0;

int lowCol = col != 0 ? col - 1 : 0;

int highRow = row != mainGrid.length - 1 ? row + 1 : mainGrid.length - 1;

int highCol = col != mainGrid[ 0 ].length - 1 ? col + 1 : mainGrid[ 0 ].length - 1;

List<Position> result =new ArrayList<Position>( );

for(int r = lowRow; r <= highRow; r++ )

for(int c = lowCol; c <= highCol; c++ )

if( r != row || c != col )

result.add(new Position( r, c ) );

return result;

}

publicboolean equals( Object other )

{

if( ! ( otherinstanceof Position ) )

returnfalse;

Position rhs = (Position) other;

return row == rhs.row && col == rhs.col;

}

publicint getValue( )

{

return mainGrid [ row ][ col ];

}

public String toString( )

{

return"(" + row +"," + col +")";

}

}

public Position newPosition(int r ,int c )

{

returnnew Position( r, c );

}

// Public driver

public List<Position> getSequence( )

{

List<Position> sequence =new ArrayList<Position>( );

for(int r = 0; r < mainGrid.length - 1; r++)

{

for(int c = 0; c < mainGrid[ 0 ].length - 1; c++)

{

sequence = getSequence(new Position( r, c ));

}

}

return sequence;

}

// Recursive routine

private List<Position> getSequence( Position pos )

{

List<Position> adj = pos.getAdjacents( );

if( adj.size( ) == 0)

{

List<Position> seq =new ArrayList<Position>( );

seq.add( pos );

return seq;

}

List<Position> maxSeq =null;

for( Position p: adj)

{

List<Position> currentSeq = getSequence( p );

if( currentSeq.size( ) < maxSeq.size( ))

maxSeq = currentSeq;

}

maxSeq.add( pos );

return maxSeq;

}

public String toString( )

{

StringBuffer sb =new StringBuffer( );

returnnew String( sb );

}

}

class MaxSubsequence

{

publicstaticvoid main( String [ ] args )

{

try

{

System.out.println(new Grid("numbergrid1.txt" ));

}

catch( IOException e)

{

System.err.println("Error opening file");

}

}

}

[8554 byte] By [applesorangesa] at [2007-11-26 19:53:48]
# 1
The toString method is used to provide a textural representation of the object. If you are trying to find the longest sequence then I would say this should be done in a method called findLongestSequence().
floundera at 2007-7-9 22:45:50 > top of Java-index,Java Essentials,New To Java...
# 2

> The toString method is used to provide a textural

> representation of the object. If you are trying to

> find the longest sequence then I would say this

> should be done in a method called

> findLongestSequence().

I already found the longest sequence with my recursive routine, getSequence. I just can't figure out for the life of me, how to print out the list of the longest Sequence in my toString.

applesorangesa at 2007-7-9 22:45:50 > top of Java-index,Java Essentials,New To Java...
# 3
So your getSequence method returns a list, so you will need to iterate over that list and add each element to a StringBuffer or StringBuilder. Once finished return the String of that StringBuffer/Builder.
floundera at 2007-7-9 22:45:50 > top of Java-index,Java Essentials,New To Java...
# 4

> So your getSequence method returns a list, so you

> will need to iterate over that list and add each

> element to a StringBuffer or StringBuilder. Once

> finished return the String of that

> StringBuffer/Builder.

This is the part I am having trouble with. I was told I could use a loop, but I don't know what I am calling from my getSequence to iterate over. I don't know how to print the array.

applesorangesa at 2007-7-9 22:45:50 > top of Java-index,Java Essentials,New To Java...
# 5

Call your getSequence method from the toString method. This gives you a List. Take a look at the methods of List to see how you can iterate over the list. Then look at the StringBuffer/Builder class and see how you can add those elements (plus any other formatting and words) to the final string.

floundera at 2007-7-9 22:45:50 > top of Java-index,Java Essentials,New To Java...
# 6

print array

String[] s = {"1", "2", "3"}

System.out.println(" array : " + s.toString());

Message was edited by:

p_epi

p_epia at 2007-7-9 22:45:50 > top of Java-index,Java Essentials,New To Java...
# 7

> Call your getSequence method from the toString

> method. This gives you a List. Take a look at the

> methods of List to see how you can iterate

> over the list. Then look at the StringBuffer/Builder

> class and see how you can add those elements (plus

> any other formatting and words) to the final string.

ok, i think i may be getting it a bit more. I don't need to use a loop at all?

I can just call the getSequence method, use a toArray to return the sequence and append the sequence to the new string?

applesorangesa at 2007-7-9 22:45:51 > top of Java-index,Java Essentials,New To Java...
# 8
You could try p_epi's suggestion but you won't get the result you expect.Yes, you will need a loop.
floundera at 2007-7-9 22:45:51 > top of Java-index,Java Essentials,New To Java...
# 9
I would use his example, but I am not inputting my own string and I don't need a println in the toString. =/
applesorangesa at 2007-7-9 22:45:51 > top of Java-index,Java Essentials,New To Java...
# 10
i really appreciate you guys helping me out. I am a bit slow on picking up on the concepts, so I hope you guys can bear with me.
applesorangesa at 2007-7-9 22:45:51 > top of Java-index,Java Essentials,New To Java...
# 11
I repeat, you won't get the result your expect. Try putting his code in a class and running it (after correcting the syntax error).
floundera at 2007-7-9 22:45:51 > top of Java-index,Java Essentials,New To Java...