Karine Roy

Greenhorn
+ Follow
since Jul 11, 2011
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Karine Roy

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!
10 years ago
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.

10 years ago
sorry, fixed the spacing:

10 years ago
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....

10 years ago
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"


10 years ago
Thanks guys, this helped a lot!
10 years ago
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
10 years ago
I am trying to catch an exception when the part number entered is not an integer. For some reason my "try - catch" is not working. Any advice?

10 years ago
Thanks for your help! I figured it out. The .equal() only works on strings, I had integers!
10 years ago
I'm having a problem with finding a match in my 2d array with an answer provided with the user.

I know my code is not complete but I am trying to get menu option #1 to work. Can anyone help me?

Also, I have no idea how to set my code with the numbers on the side, can you let me know how to do this?

10 years ago
That was simple. Thank you so much! I will use the code tag next time.
10 years ago
I am new at Java and I am not sure if I am posting at the right place. Please let me know if I am not. I am trying to transfer a 2 dimension array to a text file but get the following runtime error:

java.lang.ArrayIndexOutOfBoundsException: 2
at Parts.writeArray(Parts.java:25)
at Parts.main(Parts.java:13)

Here is the code I have so far:

import java.util.Scanner;
import java.io.*;

public class Parts
{

static final String [][] parts = {{"1", "17.56"}, {"2", "18.56"}, {"3", "3.45"}, {"4", "0.67"}};

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

PrintWriter outputFile = new PrintWriter("Parts.txt");
writeArray(parts, "Parts.txt");}

public static void writeArray(String[][] parts, String outputFile)
{
PrintStream array;
try
{
array = new PrintStream(new FileOutputStream("Parts.txt"));
for (int row = 0; row < parts.length; row++)
{
for (int col = 0; col < parts.length; col++)
{
String s = parts[row][col];
array.println(s);
}
}
array.close();
}catch (FileNotFoundException e)
{
System.out.println("not found");}
}

}

Can someone please help me?

Thanks!
10 years ago
I am new at Java and I am not sure if I am posting at the right place. Please let me know if I am not. I am trying to transfer a 2 dimension array to a text file but get the following runtime error:

java.lang.ArrayIndexOutOfBoundsException: 2
at Parts.writeArray(Parts.java:25)
at Parts.main(Parts.java:13)

Here is the code I have so far:



Can someone please help me?

Thanks!
10 years ago