Results 1 to 14 of 14
  1. #1
    rat
    rat is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default 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.

    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
    Everything else appears to be fine, it parses the file correctly too but can't compile.

    The only real difference, also where it won't compile.


    Java Code:
    records.add(new Sales(s, q1, q2, q3, q4));
    vs
    Java Code:
    records.add(new State(fn, c));
    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    can't compile.
    Please copy and paste here the FULL text of the error message

  3. #3
    rat
    rat is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    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>

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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?

  5. #5
    rat
    rat is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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?

    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; }
    } // State
    Last edited by rat; 05-18-2011 at 05:36 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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.

  7. #7
    rat
    rat is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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.
    :( can't change the class.

    do you mean to change

    Java Code:
    records.add(new State(fn, c));
    Java Code:
    records.add(new State(fn, sn, p, c));
    cause I tried that and it gave me this error


    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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    variable sn might not have been initialized
    That says that what the problem is.
    Has the program assigned anything to sn?

    The same with the next one. Where is p given a value?

  9. #9
    rat
    rat is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    changed program to this

    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, 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
    and the error message is now

    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

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    How is this different from the error messages you got in post #7
    See my post #8

  11. #11
    rat
    rat is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    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?

  12. #12
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Where is anything assigned to sn before the call to the constructor?

    If I'm reading from two files that each have a different constructor
    Files don't have constructors. Classes have constructors.

    setup an object array and read the files into references to be associated with the object array
    Not sure what that means.
    Yes you can create an array and read the records from the files into the array.
    It depends on what your program requires.

  14. #14
    rat
    rat is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by lance9200@yahoo.com View Post
    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.
    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.


    Quote Originally Posted by Norm View Post
    Where is anything assigned to sn before the call to the constructor?


    Files don't have constructors. Classes have constructors.


    Not sure what that means.
    Yes you can create an array and read the records from the files into the array.
    It depends on what your program requires.
    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 06:35 PM.

Similar Threads

  1. Having trouble with 2d Array
    By geekchick in forum New To Java
    Replies: 2
    Last Post: 08-02-2010, 05:54 AM
  2. trouble with array method
    By kpro862 in forum New To Java
    Replies: 6
    Last Post: 05-12-2010, 01:08 AM
  3. Array trouble
    By hobo in forum New To Java
    Replies: 8
    Last Post: 11-17-2009, 01:04 PM
  4. [SOLVED] Array trouble....
    By AngrYkIdzrUlE in forum New To Java
    Replies: 9
    Last Post: 04-18-2009, 11:18 PM
  5. Having trouble with array
    By ice22 in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 04:06 AM

Posting Permissions

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