Win a copy of Rust Web Development this week in the Other Languages forum!

Stephen Barrow

Greenhorn
+ Follow
since Mar 03, 2004
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 Stephen Barrow

OK I'm trying to make a program using i/o that will read 5 records from a text file C:\\hw4\\rawData.txt. They are in the format of name, age, sex, and gpa ex=John Doe 25 M 3.2 not separators just a string.
I have to write a method that will display all five on the screen. That I have done the first part of the code will display the five record lines of the text file. Then I have to use indexOf and get the gpa's out of each line and then insert them into another file I create C:\\test\\write.txt. That is what I am having extreme trouble on. I have asked for help before in a previous post and have tried to use some of the things that I learned there but I am still stuck My code is below. Give me some pointers on how to do this. I'm not asking anyone to do my work. I have some code I just need to know how to make it work and more importantly WHY it is not working. Thanks you all for you knowledge.
Ok I have my new code below. I put everything under the main method check the code to see if it is working. I will seperate the code into the appropriate methods later. The problem now is that I get the following errors:
A:\HW_4_Code2.java:36: incompatible types
found : int
required: java.lang.String
s = st.lastIndexOf(" ");
^
A:\HW_4_Code2.java:37: cannot resolve symbol
symbol : method substring (java.lang.String)
location: class java.lang.String
st.substring(s);
^
2 errors
Process completed.
When I change s to an int type the code works but when the new file is created it inputs numbers that are not the gpa. Example would be that the gpa is 3.0 but the file lists 17? Why is it doing this?
import javax.swing.*;
import java.io.*;
public class HW_4_Code2 {
public static void main(String[] args) throws IOException {
//setup file and stream
File inFile = new File("C:\\hw4\\rawData.txt");
FileInputStream inStream = new FileInputStream(inFile);
FileReader fileReader = new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(fileReader);
//String str, s;

//set up an array to read data in
int fileSize = (int)inFile.length();
byte[] byteArray = new byte[fileSize];
//read data in and display them
inStream.read(byteArray);
for (int i = 0; i < fileSize; i++) {
System.out.print((char)byteArray[i]);
inStream.close();
}
//Suppose to take all five gpa's from file and then input them
//into another file.
//public void processFile(String inFile) throws IOException{
File outFile = new File("C:\\test\\write");
BufferedReader in = new BufferedReader(new FileReader(inFile));
FileOutputStream outFileStream = new FileOutputStream(outFile);
PrintWriter outStream = new PrintWriter(outFileStream);
String st, s;
while ((st = in.readLine()) != null){
st = bufReader.readLine();
s = st.lastIndexOf(" ");
st.substring(s);
outStream.println(s);
}
in.close();
outStream.close();
}
}

--------------------------------------------------------------------------------
17 years ago
OK I'm trying to make a program using i/o that will read 5 records from a text file C:\\hw4\\rawData.txt. They are in the format of name, age, sex, and gpa ex=John Doe 25 M 3.2 not separators just a string.
I have to write a method that will display all five on the screen. That I have done the first part of the code will display the five record lines of the text file. Then I have to use indexOf and get the gpa's out of each line and then insert them into another file I create C:\\test\\write.txt. That is what I am having extreme trouble on. I have asked for help before in a previous post and have tried to use some of the things that I learned there but I am still stuck My code is below. Give me some pointers on how to do this. I'm not asking anyone to do my work. I have some code I just need to know how to make it work and more importantly WHY it is not working. Thanks you all for you knowledge.
Ok I have my new code below. I put everything under the main method check the code to see if it is working. I will seperate the code into the appropriate methods later. The problem now is that I get the following errors:
A:\HW_4_Code2.java:36: incompatible types
found : int
required: java.lang.String
s = st.lastIndexOf(" ");
^
A:\HW_4_Code2.java:37: cannot resolve symbol
symbol : method substring (java.lang.String)
location: class java.lang.String
st.substring(s);
^
2 errors
Process completed.
When I change s to an int type the code works but when the new file is created it inputs numbers that are not the gpa. Example would be that the gpa is 3.0 but the file lists 17? Why is it doing this?
import javax.swing.*;
import java.io.*;
public class HW_4_Code2 {
public static void main(String[] args) throws IOException {
//setup file and stream
File inFile = new File("C:\\hw4\\rawData.txt");
FileInputStream inStream = new FileInputStream(inFile);
FileReader fileReader = new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(fileReader);
//String str, s;


//set up an array to read data in
int fileSize = (int)inFile.length();
byte[] byteArray = new byte[fileSize];
//read data in and display them
inStream.read(byteArray);
for (int i = 0; i < fileSize; i++) {
System.out.print((char)byteArray[i]);
inStream.close();
}
//Suppose to take all five gpa's from file and then input them
//into another file.
//public void processFile(String inFile) throws IOException{
File outFile = new File("C:\\test\\write");
BufferedReader in = new BufferedReader(new FileReader(inFile));
FileOutputStream outFileStream = new FileOutputStream(outFile);
PrintWriter outStream = new PrintWriter(outFileStream);
String st, s;

while ((st = in.readLine()) != null){
st = bufReader.readLine();
s = st.lastIndexOf(" ");
st.substring(s);
outStream.println(s);
}
in.close();
outStream.close();

}
}
17 years ago
Is this suppose to replace my existing code?
17 years ago
Ok,
So we need separators. No problem, but can you point me in the right direction on how to do the rest?
17 years ago
Im trying to make a program using io that will read 5 records from a text file. They are in the format of name, age, sex, and gpa
ex=John Doe 25 M 3.2
I have to write a method that will display all five on the screen. I can do that the first part of my code works. Then I have to use indexOf and get the gpa's out and then insert them into another file I create. That is what I am having trouble on. My code is below.
import javax.swing.*;
import java.io.*;
class HW_4_Code2 {
public static void main (String[] args) throws IOException {
//setup file and stream
File inFile = new File("C:\\hw4\\rawData.txt");
FileInputStream inStream = new FileInputStream(inFile);
//set up an array to read data in
int fileSize = (int)inFile.length();
byte[] byteArray = new byte[fileSize];
//read data in and display them
inStream.read(byteArray);
for (int i = 0; i < fileSize; i++) {
System.out.print((char)byteArray[i]);
}
inStream.close();

String last = inFile.substring(inFile.indexOf(" ")+1,
inFile.length());
System.out.println(+last);
}
}
17 years ago
How would I declare an array of double of size 100 to store point values of 100 students and assigns each student element a value same as its index (or subscript) number.
Would this code work?
student = new Student("John", 10, ,'M');
stu.add(student);
17 years ago
I tried to add your suggested code and heres what I got
import java.util.*;
import java.text.*;
import javax.swing.*;
public class Deviator
{
public static void main(String [] args)
{


Deviator deviator = new Deviator();
double [] values = {10,20,30,4,2}; // This is were I define my array
List values = new ArrayList();
values.add(10);
values.add(20);
double mean = deviator.calculateMean(values);
double stddev = deviator.calculateStandardDeviation(values);
System.out.println("values: " + values);
System.out.println("mean : " + mean);
System.out.println("stddev: " + stddev);
}



public double calculateMean(final List values)
{
double mean = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
mean += ((Double)values.get(i)).doubleValue();
return mean/numValues;
}
public double calculateStandardDeviation(final List values)
{
double mean = this.calculateMean(values);
double sum = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
{
double value = ((Double)values.get(i)).doubleValue();
double diff = value - mean;
sum += diff*diff;
}
return Math.sqrt(sum/(numValues - 1));
}
}
Errors 5:
java:12: values is already defined in main(java.lang.String[])
List values = new ArrayList();
java:13: cannot resolve symbol
symbol : method add (int)
location: class double[]
values.add(10);
^
java:14: cannot resolve symbol
symbol : method add (int)
location: class double[]
values.add(20);
^
java:15: calculateMean(java.util.List) in Deviator cannot be applied to (double[])
double mean = deviator.calculateMean(values);
java:16: calculateStandardDeviation(java.util.List) in Deviator cannot be applied to (double[])
double stddev = deviator.calculateStandardDeviation(values);
17 years ago
Error messages that result when the above code is compliled
java:12: calculateMean(java.util.List) in Deviator cannot be applied to (double[])
double mean = deviator.calculateMean(values);

java:13: calculateStandardDeviation(java.util.List) in Deviator cannot be applied to (double[])
double stddev = deviator.calculateStandardDeviation(values);
^
17 years ago
import java.util.*;
public class Deviator
{
public static void main(String [] args)
{
Deviator deviator = new Deviator();
double [] values = {10,20,30,4,2}; //This is were I define my array

double mean = deviator.calculateMean(values);
double stddev = deviator.calculateStandardDeviation(values); System.out.println("values: " + values);
System.out.println("mean : " + mean);
System.out.println("stddev: " + stddev);
}



public double calculateMean(final List values)
{
double mean = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
mean += ((Double)values.get(i)).doubleValue();
return mean/numValues;
}
public double calculateStandardDeviation(final List values)
{
double mean = this.calculateMean(values);
double sum = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
{
double value = ((Double)values.get(i)).doubleValue();
double diff = value - mean;
sum += diff*diff;
}
return Math.sqrt(sum/(numValues - 1));
}
}
17 years ago
Ive tried taking it out but I can't get the code to run right. I keep getting cannot resolve errors etc..
17 years ago
anybody there?
17 years ago
Can somebody tell me how I can change this code to allow me to hard code the values into it? such as double [] values = {10,20,30,4,2};
import java.util.*;
public class Deviator
{
public static void main(String [] args)
{

Deviator deviator = new Deviator();
List values = deviator.getValues(args);
double mean = deviator.calculateMean(values);
double stddev = deviator.calculateStandardDeviation(values);
System.out.println("values: " + values);
System.out.println("mean : " + mean);
System.out.println("stddev: " + stddev);

System.out.println("No arguments");

}
public List getValues(final String [] args)
{
List values = new ArrayList();
for (int i = 0; i < args.length; ++i)
{
try
{
Double value = Double.valueOf(args[i]);
values.add(value);
}
catch (NumberFormatException e)
{
System.err.println("Not a number: " + args[i]);
}
}
return values;
}

public double calculateMean(final List values)
{
double mean = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
mean += ((Double)values.get(i)).doubleValue();
return mean/numValues;
}
public double calculateStandardDeviation(final List values)
{
double mean = this.calculateMean(values);
double sum = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
{
double value = ((Double)values.get(i)).doubleValue();
double diff = value - mean;
sum += diff*diff;
}
return Math.sqrt(sum/(numValues - 1));
}
}
17 years ago
Can somebody tell me how I can change this code to allow me to hard code the values into it? such as double [] values = {10,20,30,4,2};
import java.util.*;
public class Deviator
{
public static void main(String [] args)
{

Deviator deviator = new Deviator();
List values = deviator.getValues(args);
double mean = deviator.calculateMean(values);
double stddev = deviator.calculateStandardDeviation(values);
System.out.println("values: " + values);
System.out.println("mean : " + mean);
System.out.println("stddev: " + stddev);

System.out.println("No arguments");

}
public List getValues(final String [] args)
{
List values = new ArrayList();
for (int i = 0; i < args.length; ++i)
{
try
{
Double value = Double.valueOf(args[i]);
values.add(value);
}
catch (NumberFormatException e)
{
System.err.println("Not a number: " + args[i]);
}
}
return values;
}

public double calculateMean(final List values)
{
double mean = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
mean += ((Double)values.get(i)).doubleValue();
return mean/numValues;
}
public double calculateStandardDeviation(final List values)
{
double mean = this.calculateMean(values);
double sum = 0.0;
int numValues = values.size();
for (int i = 0; i < numValues; ++i)
{
double value = ((Double)values.get(i)).doubleValue();
double diff = value - mean;
sum += diff*diff;
}
return Math.sqrt(sum/(numValues - 1));
}
}
17 years ago