Results 1 to 7 of 7
Thread: help with these output..
- 12-27-2010, 05:50 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
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:
andJava 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 i want to have an output like this: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); } }
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:
BUILD SUCCESSFUL (total time: 0 seconds)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]
help...
- 12-27-2010, 06:14 PM #2
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.).
- 12-27-2010, 07:37 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
- 12-27-2010, 07:45 PM #4
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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--
- 12-28-2010, 01:55 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
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..
what does toString method to?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.
can i use the each element in my arraylist later?
here the main class:Please provide an example main class you are using to test this.
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"); } }
- 12-28-2010, 02:18 AM #6
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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--
- 12-28-2010, 10:57 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 8
- Rep Power
- 0
i think i will use this code:
with the state class: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); } }
but i still have one more problem,.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; } }
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:
the "new" line will be parsing to: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
Java Code:ArrayList<String> finale = new ArrayList<String>();
Similar Threads
-
how to get the resultset output into an output file
By renu in forum New To JavaReplies: 0Last Post: 09-30-2010, 08:16 PM -
output
By yrollgayanth in forum New To JavaReplies: 5Last Post: 12-30-2009, 05:05 PM -
Need help: output is not what i want
By Joshsmith in forum New To JavaReplies: 8Last Post: 09-28-2009, 10:09 AM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
what the outPut
By alksam in forum Advanced JavaReplies: 5Last Post: 12-25-2008, 01:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks