• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tokenizing after reading from a file

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first part of my assignment was to write a file (which I have done) with a matrix of information (First name, last name, gender, age, dependents and SIN/SSN).


Check to see if the file exist and if it has parent directory names as follow:


The last part of my assignment is:

Write a program to:

a. Read in the file one line at a time into a string using readLine and display them in DOS window.

b. Using StringTokenizer break up the string into separate fields in arrays as follows: first, last, gender, age, dependents and sin.

c. Print a report in a DOS window that lists all First Names, Last Names and SIN numbers by last name.

I think I got a. right but b. and c. are giving me trouble. Here's what I have so far:


Any advice as to what I am doing wrong?

Thanks
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karine,

Just looking at the section of code you've commented out...

First off, you made a little typo in your for statement. (Just take a look at that line, it should jump right out at you.) That should at least get your code running for you. Secondly, think about your StringTokenizer and where you should be initializing it.
 
Karine Roy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I see what is wrong with the For statement. I rewrote the code as follow but get these 8 error messages (7 being about a non-static method cannot be referenced from a static context, and the other is with my compareTo, having to be a boolean and not a int)

Here is the code:


And here are the error messages.

"8 errors found:

File: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java [line: 25]
Error: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java:25: non-static method tokenFile(int,java.lang.String[],java.lang.String[],java.lang.String[],int[],int[],int[],java.lang.String[]) cannot be referenced from a static context

File: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java [line: 64]
Error: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java:64: non-static method nextToken() cannot be referenced from a static context

File: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java [line: 66]
Error: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java:66: non-static method nextToken() cannot be referenced from a static context

File: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java [line: 68]
Error: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java:68: non-static method nextToken() cannot be referenced from a static context

File: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java [line: 70]
Error: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java:70: non-static method nextToken() cannot be referenced from a static context

File: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java [line: 73]
Error: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java:73: non-static method nextToken() cannot be referenced from a static context

File: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java [line: 76]
Error: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java:76: non-static method nextToken() cannot be referenced from a static context

File: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java [line: 92]
Error: C:\Users\User\Documents\Karine\School\Java 1\Assignments\Assignment 8\ReadFile.java:92: incompatible types
found : int
required: boolean"


 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding the first (repeated) error, do you see anything different about how you've declared the tokenFile method compared with the other methods you have?

For the other error, unlike in languages like C++, Java requires that boolean expressions or variables be used in contexts where a logical value is expected. So in your if statement,

you have to make the stuff between the parentheses into a boolean expression--easy enough to do with a =, < or > 0 as needed.
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, you may want to break those calls to the tokenFile and reportSorted methods into multiple lines. Having it stretch across the screen like that makes it hard to read. (And because of the way that text flows in the forums, it makes everything else hard to read as well.)
 
Karine Roy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've fixed the second problem. As for the first (the repeating error), i added the word "static" in my method but it still gives me the same error. There must be something I am not understanding. Sorry, I am very new at this....

 
Karine Roy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, fixed the spacing:

 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karine,

The spacing does make it easier to read, but what I was referring to earlier was the method declaration that stretches across the page. If it looks okay in your editor then that's fine for your own use, but when you post your code here in the forum it makes it hard to read because you have to pan back and forth (unless perhaps you've got a very big monitor).

For your remaining error messages, look at this snippet:

When you call nextToken(), what exactly do you want to call it on?
 
Karine Roy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I got frustrated and rewrote the code. It makes much more sense now but the tokenizer is not working.


The file contents are:
John,Doe,M,24,2,343345678,
Peter,Miles,M,34,3,333434344,
Mary,Macintosh,F,45,2,323098543,
Laurie,Dent,F,22,1,3245676543,
Kim,Peter,F,32,4,4098734543,
Karyn,Marts,F,34,0,4567890,

The error I get it:

Welcome to DrJava. Working directory is C:\Users\User\Documents
\Karine\School\Java 1\Assignments\Assignment 8
> java ReadFile
John,Doe,M,24,2,343345678,
John
java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at ReadFile.main(ReadFile.java:43)

Not too sure where I am going wrong this time. It should work. What I am thinking is that there is something wrong with the file.

 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, now that you've got it compiling, it's time for the joy of logic (runtime) errors!

While I think that there is some benefit to decomposing your program into a number of distinct methods, your current version is a lot simpler than what you had before. One stylistic change I would suggest is to use a while loop instead of a do-while. (I think that should work without needing any other changes.)

While you're at it, take a close look at what's in the body of the loop. You've repeated a line, and that's probably what's causing your current problem.
 
Karine Roy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow it's been a long day. I read that part of the code at least 40 times and didn't even see that!

Thanks so much! I also used your suggestion and it looks better.

Thanks again for all your help!
 
reply
    Bookmark Topic Watch Topic
  • New Topic