Results 1 to 7 of 7
  1. #1
    Spooge is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Question text file into arrays using StreamTokenizer

    If someone can tell me what I'm doing wrong, I will be eternally grateful! I've been fiddling with this for weeks, and cannot get it to work. Taking a text file and Tokenizing it; putting it into 4 arrays, and using TreeMap and HashMap (I have to use those them, it's the rules), then writing a new file.
    Text file:
    123456 Computers 16000.00 4932
    234567 Cameras 4000.00 1133
    345678 Furniture 1200.00 3441
    456789 Computers 18000.00 4827
    567890 Furniture 2500.00 3442
    678910 Appliances 5500.00 6018





    package testSale

    import java.io.*;
    import java.util.*;
    import java.io.StreamTokenizer;
    import java.io.FileReader;
    import java.io.FileWriter;




    public class Main {

    public static void main(String[] args)
    throws FileNotFoundException, IOException
    {

    Scanner console = new Scanner(System.in);
    System.out.println("Enter the name of your sales file:");
    String salesFile = console.next();
    StreamTokenizer separated = null;

    /**Creates simple one-dimensional arrays to store the information extracted
    * from the sales file.
    */
    int skuNumber[] = new int[10];
    String department[] = new String[10];
    double salesDollars[] = new double[10];
    int employeeID[] = new int[10];

    // Creates the FileReader to read sales.txt
    FileReader reader = new FileReader(salesFile);

    /** I'm not permitted to buffer the file until it has already been read; so I am
    * doing so here.
    */
    StringBuffer myBuffered = new StringBuffer(reader);

    // Separates each line into component parts using white space as dividers.
    StreamTokenizer separated = new StreamTokenizer(new String(myBuffered));
    separated.eolIsSignificant(true);



    /** Puts each token into its appropriate Array. Since there are consistently
    * four repeating items that must be put sequentially into arrays, I will put
    * each item into its corresponding array, and every fourth item will advance
    * the arrays' number.
    */
    int arrayLine =0;
    while (separated.nextToken() != StreamTokenizer.TT_EOF){
    skuNumber[arrayLine] = separated.nextToken();
    department[arrayLine] = separated.nextToken();
    salesDollars[arrayLine]= separated.nextToken();
    employeeID[arrayLine] = separated.nextToken();
    arrayLine++;
    }

    /**Creates a set to store the different property types, and iterates
    * through the department Array to add each entry to the set.
    */
    Set<String> departmentTypes = new TreeSet<String>();{
    for (int i=0; i<10; i++){
    boolean add = departmentTypes.add(departmentType[i] + "/n");
    }
    }


    /**Creates a map to store the employeeID and amount of sales under each
    * employee.
    * The key is the employeeID, the value is the salesDollars.
    */
    TreeMap<Integer,Double> employeeTotals = new TreeMap<Integer,Double>();{
    for (int j=0; j<10; j++){
    Double put = agentTotals.put(agentID[j], propertyAmount[j]);
    j++;
    }
    }

    FileWriter employeeReport = new FileWriter("employeereport.txt");
    agentReport.write(departmentTypes);
    agentReport.append(employeeTotals);

    // Closes the scanner and writer.
    console.close();
    System.out.close();
    employeeReport.close();
    }

    }

    All kinds of "Cannot find symbol" errors, and cannot find the sales.txt file no matter where I put it in the Netbeans/windows file directory structure. Please assist!!!!!

    Thank you to those of you who can easily correct my stupid errors, since I'm sure that is what they are.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The compiler messages tell you what is wrong. Work through them one at a time. If you cannot understand what a message means post the complete message exactly as it appears along with some indication of which line of your code the message is referring to.

    "Cannot find symbol" is a message you get when you use a variable that you have not declared, or a method that is not described in the published API of a class. The message will tell you the symbol that is bogus (doesn't exist) or is a typo.

    For instance:

    Java Code:
    StringBuffer myBuffered = new StringBuffer(reader);

    Is there a StringBuffer constructor that takes a FileReader instance as an argument? If not, then you will get a "cannot find symbol" message.

    It's best, once you have a definite plan in mind, to work one small step at a time resolving such errors as they occur (ie removing typos, declarig everything that needs to be declared and only using methods you have checked in the API documentation)

  3. #3
    Spooge is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    C:\...\Main.java:41: cannot find symbol
    symbol : constructor StringBuffer(java.io.FileReader)
    location: class java.lang.StringBuffer
    StringBuffer myBuffered = new StringBuffer(reader);

    C:\...\Main.java:42: cannot find symbol
    symbol : constructor StreamTokenizer(java.lang.String)
    location: class java.io.StreamTokenizer
    StreamTokenizer separated = new StreamTokenizer(new String(myBuffered));

    C:\...\Main.java:47: incompatible types
    found : int
    required: java.lang.String
    firstName[arrayLine] = separated.nextToken();

    C:\...\Main.java:48: incompatible types
    found : int
    required: java.lang.String
    lastName[arrayLine] = separated.nextToken();

    C:\...\Main.java:55: package system does not exist
    system.out.println(firstName[i] + " " + lastName[i] + " " + age[i] + " \n");

    C:\...\Main.java:60: package system does not exist
    system.out.close();

    C:\...\nbproject\build-impl.xml:553: The following error occurred while executing this line:
    C:\...\nbproject\build-impl.xml:286: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 0 seconds)

    That's the list of errors.

    My questions:
    1)How can certain packages "not exist" if they've been imported???
    2)Why is the program saying "cannot find symbol" when they've been imported?
    3)Why is there an "incompatible type" when the corresponding array is declared as a String array and that token from the text file should be a String? How did it find an "int"?
    4)Why doesn't it find the "sales.txt" file when I attempt to run the program?
    Exception in thread "main" java.io.FileNotFoundException: sales.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)

    Thank you very much for your help, and for responding so quickly!!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Spooge View Post
    My questions:
    1)How can certain packages "not exist" if they've been imported???
    You can import all you want, but even if a class such as StringBuffer exists, if you try to call a constructor for it that doesn't exist (I believe you've been advised above to check the API), those imports won't do squat.

    C:\...\Main.java:55: package system does not exist
    system.out.println(firstName[i] + " " + lastName[i] + " " + age[i] + " \n");
    "system" doesn't in fact exist. Check spelling and capitalization. By the way, you're showing us an error for code that you're not showing us (where do you use firstName or lastName anything above), which makes helping you very difficult. Also, you'll want to edit your first post so that it uses code tags -- see the link in my signature -- so we can read your code. Most of your errors are through carelessness. If I were you, I'd go through each error message one at a time and check carefully that you're spelling things right, using correct method and constructor calls -- ones that exist, etc...

    Luck!
    Last edited by Fubarable; 10-18-2010 at 11:06 PM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    1)How can certain packages "not exist" if they've been imported???
    The messages assert (twice) that the package system does not exist. You don't actually import that package (or class) at the start of your code. You could try that, but I wouldn't bother ... because it doesn't exist. Like a "cannot find symbol" error you might want to consider the possibility of a typo. The smallest spelling error will take you from something that does exist (and has been imported etc) to something that will make the compiler complain.

  6. #6
    Spooge is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    OK, I see what my first dumba-- mistake is. "System" should be capitalized. Stupid stupid stupid.
    And I added the error codes from a VERY similar exercise. Same issues though. My bad.

    Other errors I still don't know:

    FileReader reader = new FileReader(salesFile); //OK.

    StringBuffer myBuffered = new StringBuffer(reader) //Correct per API, but shows
    C:\...\Main.java:48: cannot find symbol
    symbol : constructor StringBuffer(java.io.FileReader)
    location: class java.lang.StringBuffer
    StringBuffer myBuffered = new StringBuffer(reader);

    StreamTokenizer separated = new StreamTokenizer(new String(myBuffered));
    //Good per API, but shows:
    C:\...\Main.java:51: cannot find symbol
    symbol : constructor StreamTokenizer(java.lang.StringBuffer)
    location: class java.io.StreamTokenizer
    StreamTokenizer separated = new StreamTokenizer(myBuffered)

    department[arrayLine] = separated.nextToken();
    //Token should be a string, array is declared as a string. But error message:
    C:\...\Main.java:64: incompatible types
    found : int
    required: java.lang.String
    propertyType[arrayLine] = separated.nextToken();

    FileWriter employeeReport = new FileWriter("employeereport.txt");
    //Pretty straightforward...
    agentReport.write(departmentTypes);
    agentReport.write(employeeTotals);
    //Tried both write and append methods, realized append is not valid for this. Write IS!
    C:\..\Main.java:123: cannot find symbol
    symbol : method write(java.util.Set<java.lang.String>)
    location: class java.io.FileWriter
    agentReport.write(propertyTypes)

    What am I not doing correctly? Why the errors?

    A big thank you to Fubarable and pbrockway2!!!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Spooge View Post
    OK, I see what my first dumba-- mistake is. "System" should be capitalized. Stupid stupid stupid.
    And I added the error codes from a VERY similar exercise. Same issues though. My bad.
    No problem. You're learning the hard way how unforgiving compilers are -- and that's a good thing since you're far better off finding errors at compile time than at run time.

    Other errors I still don't know:

    FileReader reader = new FileReader(salesFile); //OK.

    StringBuffer myBuffered = new StringBuffer(reader) //Correct per API, but shows
    C:\...\Main.java:48: cannot find symbol
    symbol : constructor StringBuffer(java.io.FileReader)
    location: class java.lang.StringBuffer
    StringBuffer myBuffered = new StringBuffer(reader);
    When I inspected the API, I couldn't find a StringBuffer constructor entry that took a FileReader. String, charSequence, int and default, yes, but FileReader, no.

    StreamTokenizer separated = new StreamTokenizer(new String(myBuffered));
    //Good per API, but shows:
    C:\...\Main.java:51: cannot find symbol
    symbol : constructor StreamTokenizer(java.lang.StringBuffer)
    location: class java.io.StreamTokenizer
    StreamTokenizer separated = new StreamTokenizer(myBuffered)
    Again, I can't find an entry for a String constructor that takes a StringBuffer as a parameter. The correct way to get a String from a StringBuffer is in the StringBuffer API (actually it's a method from StringBuffer's ultimate parent, Object -- toString()).

    etc...

    I think through careful reading you'll find the cause for your other errors.

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2010, 10:18 AM
  2. Replies: 3
    Last Post: 06-08-2010, 08:10 PM
  3. Replies: 8
    Last Post: 11-06-2009, 06:52 AM
  4. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  5. Using StreamTokenizer to take input
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-04-2008, 09:38 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
  •