Results 1 to 7 of 7
  1. #1
    e1007 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default help with these output..

    i have a file:
    0,1,A
    0,5,B
    1,1,A
    1,3,B
    2,6,A
    2,4,B
    3,6,A
    3,4,B
    4,6,A
    4,4,B
    5,1,A
    5,5,B
    6,6,A
    6,2,B

    and class:
    Java Code:
    public class State {
    
        
        ArrayList<String> StateIn, StateOut, Symbol;
    
        public ArrayList<String> getStateIn() {
            return StateIn;
        }
    
        public ArrayList<String> getStateOut() {
            return StateOut;
        }
    
        public ArrayList<String> getSymbol() {
            return Symbol;
        }
    
        public void setStateIn(ArrayList<String> StateIn) {
            this.StateIn = StateIn;
        }
    
        public void setStateOut(ArrayList<String> StateOut) {
            this.StateOut = StateOut;
        }
    
        public void setSymbol(ArrayList<String> Symbol) {
            this.Symbol = Symbol;
        }
    
       
    
        public State(ArrayList<String> StateIn, ArrayList<String> StateOut, ArrayList<String> Symbol) {
            this.StateIn = StateIn;
            this.StateOut = StateOut;
            this.Symbol = Symbol;
        }  
        }
    and

    Java Code:
    public class FastReader {
      
        Scanner scan = null;
        
        public void parseFile(String filename) throws FileNotFoundException{
    
            String line = null;
            ArrayList<State> al = new ArrayList<State>();
            ArrayList<String> start = new ArrayList<String>();
            ArrayList<String>  finale = new ArrayList<String>();
            ArrayList<String> stateIn = new ArrayList<String>();
            ArrayList<String> stateOut = new ArrayList<String>();
            ArrayList<String> simbol = new ArrayList<String>();
    
            scan = new Scanner(new BufferedReader(new FileReader(filename)));
            
           while(scan.hasNext()) {
                StringTokenizer tok = new StringTokenizer(scan.next(), ",");
                stateIn.add(tok.nextToken());
                stateOut.add(tok.nextToken());
                simbol.add(tok.nextToken());
                al.add(new State(stateIn, stateOut, simbol));
           }
             
             System.out.println(al);
    
             }
        }
    and i want to have an output like this:
    0 1 A
    0 5 B
    1 1 A
    1 3 B
    2 6 A
    2 4 B
    3 6 A
    3 4 B
    4 6 A
    4 4 B
    5 1 A
    5 5 B
    6 6 A
    6 2 B

    but i get:

    Java Code:
    [faster.State@14318bb, faster.State@ca0b6, faster.State@10b30a7, faster.State@1a758cb, faster.State@1b67f74, faster.State@69b332, faster.State@173a10f, faster.State@530daa, faster.State@a62fc3, faster.State@89ae9e, faster.State@1270b73, faster.State@60aeb0, faster.State@16caf43, faster.State@66848c]
    BUILD SUCCESSFUL (total time: 0 seconds)

    help...

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You cannot print an ArrayList object, as you are trying to do here:
    System.out.println(al);

    Instead, you will want to print each item in the ArrayList separately (element 0, 1, 2, etc.).

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by e1007 View Post
    i have a file:
    0,1,A
    0,5,B
    1,1,A
    1,3,B
    2,6,A
    2,4,B
    3,6,A
    3,4,B
    4,6,A
    4,4,B
    5,1,A
    5,5,B
    6,6,A
    6,2,B

    [ ... ]

    and i want to have an output like this:
    0 1 A
    0 5 B
    1 1 A
    1 3 B
    2 6 A
    2 4 B
    3 6 A
    3 4 B
    4 6 A
    4 4 B
    5 1 A
    5 5 B
    6 6 A
    6 2 B
    So you want every comma replaced by a single space character on each line? Have a look at the String.replaceAll( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    When you use System.out.println(al), Java will use the default toString method which simply prints the name of the object (State), an @ sign, followed by the State object's location in memory.

    If you want it to print meaningful output, you will have to provide a toString() method in your State class. Please provide an example main class you are using to test this.
    --user0--

  5. #5
    e1007 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    So you want every comma replaced by a single space character on each line? Have a look at the String.replaceAll( ... ) method.

    kind regards,

    Jos
    hhmm...
    no.
    i don't want to replace every comma by a single space character.
    i want to store it in an arrayList and want to process it later..

    When you use System.out.println(al), Java will use the default toString method which simply prints the name of the object (State), an @ sign, followed by the State object's location in memory.
    what does toString method to?
    can i use the each element in my arraylist later?

    Please provide an example main class you are using to test this.
    here the main class:
    Java Code:
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws Exception {
            // TODO code application logic here
    
         FastReader fr = new FastReader();
         fr.parseFile("c:\\dfa.txt");
    
    
        }
    
    }

  6. #6
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Hi - the toString() method will overwrite the default toString() method being used in your example. You can add the following method to your State class and run your program and check if that's the kind of ourput you are looking for.

    Java Code:
        public String toString()	{
    		String s = "";
        	for(int i = 0; i < StateIn.size(); i++)	{
    			s += StateIn.get(i) + " ";
    			s += StateOut.get(i) + " ";
    			s += Symbol.get(i) + " \n";
        	}
        	return s;
        	
        }
    --user0--

  7. #7
    e1007 is offline Member
    Join Date
    Dec 2010
    Posts
    8
    Rep Power
    0

    Default

    i think i will use this code:

    Java Code:
    public class FastReader {
      
        Scanner scan = null;
        
        public void parseFile(String filename) throws FileNotFoundException{
    
            String line = null;
            ArrayList<State> al = new ArrayList<State>();
      
            scan = new Scanner(new BufferedReader(new FileReader(filename)));
            
           while(scan.hasNext()) {
                StringTokenizer tok = new StringTokenizer(scan.next(), ",");
                String str1 = tok.nextToken();
                String str2 = tok.nextToken();
                String str3 = tok.nextToken();
                al.add(new State(str1, str2, str3));
           }
             
             System.out.println(al);
    
             }
        }
    with the state class:
    Java Code:
    public class State {
    
        String stateIn, stateOut. symbol;
        
        public String getStateIn() {
            return stateIn;
        }
    
        public String getStateOut() {
            return stateOut;
        }
    
        public String getSymbol() {
            return symbol;
        }
    
        public void setStateIn(String stateIn) {
            this.stateIn = stateIn;
        }
        .....
    
    
        public State(String stateIn, String stateOut, String symbol) {
            this.stateIn = stateIn;
            this.stateOut = stateOut;
            this.symbol = symbol;
        }  
        public String toString() {
           return stateIn + " " + stateOut + " " + symbol;
        }
        }
    but i still have one more problem,.
    when i try to add 1 line to read,.
    which is have to parse it to different arraylist, i still get an error output.

    for example:


    i want to add this line to my input:

    Java Code:
    2 4 5 6 <-- this is the "new" line, but the length is still unknown
    0 1 A
    0 5 B
    1 1 A
    1 3 B
    2 6 A
    2 4 B
    3 6 A
    3 4 B
    4 6 A
    4 4 B
    5 1 A
    5 5 B
    6 6 A
    6 2 B
    the "new" line will be parsing to:
    Java Code:
    ArrayList<String> finale = new ArrayList<String>();

Similar Threads

  1. Replies: 0
    Last Post: 09-30-2010, 09:16 PM
  2. output
    By yrollgayanth in forum New To Java
    Replies: 5
    Last Post: 12-30-2009, 06:05 PM
  3. Need help: output is not what i want
    By Joshsmith in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 11:09 AM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  5. what the outPut
    By alksam in forum Advanced Java
    Replies: 5
    Last Post: 12-25-2008, 02:44 PM

Posting Permissions

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