Results 1 to 14 of 14
Thread: text to array trouble
- 05-18-2011, 03:05 PM #1
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
text to array trouble
I modified another program that does work to get to this point. The first program works totally fine. It reads from a txt file to an array based off a class. (Not sure if that sentence made sense)
Just for reference heres the original working
Java Code:import java.util.*; import java.io.*; public class ReadFile3 { public static void main(String []args) { LinkedList records = new LinkedList(); // an empty LinkedList object String s, s2; StringTokenizer st; double q1, q2, q3, q4; try { BufferedReader inFile = new BufferedReader(new FileReader("Sales.txt")); while ((s = inFile.readLine()) != null) { s2 = inFile.readLine(); st = new StringTokenizer(s2); q1 = Double.valueOf(st.nextToken()); q2 = Double.valueOf(st.nextToken()); q3 = Double.valueOf(st.nextToken()); q4 = Double.valueOf(st.nextToken()); records.add(new Sales(s, q1, q2, q3, q4)); } // while not EOF inFile.close(); } catch (Exception e) { System.err.println("????"); } for (int k=0; k < records.size(); ++k) { System.out.println(records.get(k)); } } // main }
Now heres this program, also supposed to be reading from a txt file into an array off class. This time it has to do 2 text files.
Everything else appears to be fine, it parses the file correctly too but can't compile.Java Code:import java.io.FileReader; import java.util.ArrayList; import java.util.*; import java.io.*; public class Final2 { public static void main(String []args) { ArrayList states = new ArrayList(); // To keep track of states String s, s2, fn, c; StringTokenizer st = new StringTokenizer(" - "); double q1, q2, q3, q4; try { BufferedReader inFile = new BufferedReader(new FileReader("Capitals.txt")); while ((s2 = inFile.readLine()) != null) { st = new StringTokenizer(s2); fn = String.valueOf(st.nextToken("-")); c= String.valueOf(st.nextToken()); states.add(new State(fn, c)); } inFile.close(); } catch (Exception e) { System.err.println("????"); } } // main } // ReadFile
The only real difference, also where it won't compile.
vsJava Code:records.add(new Sales(s, q1, q2, q3, q4));
Theres a sales class and a state class. The Sales class only has the s, q1, q2, q3, q4 values. Whereas the state class has fn, sn, c, p, and that line only adds fn and c.Java Code:records.add(new State(fn, c));
- 05-18-2011, 04:23 PM #2
Please copy and paste here the FULL text of the error messagecan't compile.
- 05-18-2011, 04:28 PM #3
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
sure, here you go
C:\b>javac Final2.java
Final2.java:22: cannot find symbol
symbol : constructor State(java.lang.String,java.lang.String)
location: class State
records.add(new State(fn, c));
^
1 error
C:\b>
- 05-18-2011, 04:30 PM #4
The error says that the State class does NOT have a constructor that takes 2 Strings as args.
Where is the State class defined? What args does its constructors take?
- 05-18-2011, 04:34 PM #5
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
I assume from looking at it, string, string, long, string
The state class is in its own java file in the same folder.
Heres its code
Java Code:import java.io.*; public class State implements Serializable { protected String fullName; protected String shortName; protected long population; protected String capital; public State(String fn, String sn, long p, String c) { fullName = fn; shortName = sn; population = p; capital = c; } public String getFullName() { return fullName; } public String getShortName() { return shortName; } public long getPopulation() { return population; } public String getCapital() { return capital; } public void setFullName(String s) { fullName = s; } public void setShortName(String s) { shortName = s; } public void setPopulation(long p) { population = p; } public void setCapital(String s) { capital = s; } } // StateLast edited by rat; 05-18-2011 at 04:36 PM.
- 05-18-2011, 04:38 PM #6
So either add a constructor to the State class that takes two strings
or change the call to the constructor to use all the args that it takes.
- 05-18-2011, 04:45 PM #7
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
:( can't change the class.
do you mean to change
Java Code:records.add(new State(fn, c));
cause I tried that and it gave me this errorJava Code:records.add(new State(fn, sn, p, c));
Final2.java:22: variable sn might not have been initialized
records.add(new State(fn, sn, p, c));
^
Final2.java:22: variable p might not have been initialized
records.add(new State(fn, sn, p, c));
^
Note: Final2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
- 05-18-2011, 05:02 PM #8
That says that what the problem is.variable sn might not have been initialized
Has the program assigned anything to sn?
The same with the next one. Where is p given a value?
- 05-18-2011, 05:20 PM #9
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
changed program to this
and the error message is nowJava Code:import java.io.FileReader; import java.util.ArrayList; import java.util.*; import java.io.*; public class Final2 { public static void main(String []args) { ArrayList states = new ArrayList(); // To keep track of states String s, s2, sn, fn, c; StringTokenizer st = new StringTokenizer(" - "); double q1, q2, q3, q4; long p; try { BufferedReader inFile = new BufferedReader(new FileReader("Capitals.txt")); while ((s2 = inFile.readLine()) != null) { st = new StringTokenizer(s2); fn = String.valueOf(st.nextToken("-")); c= String.valueOf(st.nextToken()); states.add(new State(fn, sn, p, c)); } inFile.close(); } catch (Exception e) { System.err.println("????"); } } // main } // ReadFile
Final2.java:22: variable sn might not have been initialized
states.add(new State(fn, sn, p, c));
^
Final2.java:22: variable p might not have been initialized
states.add(new State(fn, sn, p, c));
^
Note: Final2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
- 05-18-2011, 05:25 PM #10
How is this different from the error messages you got in post #7
See my post #8
- 05-18-2011, 05:28 PM #11
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
I don't think it is, I meant to show its making that error regardless of anything being assigned to sn.
If I'm reading from two files that each have a different constructor does that mean I need to setup an object array and read the files into references to be associated with the object array?
- 05-18-2011, 05:31 PM #12
Member
- Join Date
- May 2011
- Posts
- 6
- Rep Power
- 0
From the code above I see where you have declared sn and p but I don't see where you have intialized them (gave them values) so that the program can use those values.
- 05-18-2011, 05:32 PM #13
Where is anything assigned to sn before the call to the constructor?
Files don't have constructors. Classes have constructors.If I'm reading from two files that each have a different constructor
Not sure what that means.setup an object array and read the files into references to be associated with the object array
Yes you can create an array and read the records from the files into the array.
It depends on what your program requires.
- 05-18-2011, 05:32 PM #14
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Don't want the program to use those values, when it reads the file it only takes out fn and c, but I guess that doesn't work with the constructor for the class.
erm, attributes?
Basically I need to read data records from two txt files into an array of State objects. This file has fullname and capital, the other has population and abbreviation.Last edited by rat; 05-18-2011 at 05:35 PM.
Similar Threads
-
Having trouble with 2d Array
By geekchick in forum New To JavaReplies: 2Last Post: 08-02-2010, 04:54 AM -
trouble with array method
By kpro862 in forum New To JavaReplies: 6Last Post: 05-12-2010, 12:08 AM -
Array trouble
By hobo in forum New To JavaReplies: 8Last Post: 11-17-2009, 12:04 PM -
[SOLVED] Array trouble....
By AngrYkIdzrUlE in forum New To JavaReplies: 9Last Post: 04-18-2009, 10:18 PM -
Having trouble with array
By ice22 in forum New To JavaReplies: 3Last Post: 11-13-2007, 03:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks