Results 1 to 20 of 21
- 10-17-2008, 03:01 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 15
- Rep Power
- 0
help...! about reading a text file and finding their average
i am new to java.. can please help me find the solution of this problem.. i am studying for our removal this oct.21 can you please help me find this out so that.. i have enough(i think) knowledge if i encounter this problem again...
so sir this is the input of my textfile..
Jin Kazama 74 75 77 78 79
Kazuya Mishima 70 72 73 74 75
Lei Wulong 90 89 73 67 89
and this is my java code...
import java.util.Scanner;
import java.io.*;
public class ScoreAverager
{
//default constructor
public ScoreAverager implements ()
{
}
//method to read the file, average the grades, and print the new file
//an IO exception is thrown if the file can't be read or a new file cannot be written to
public void gradeAvg() throws IOException
{
Scanner input = new Scanner(System.in);
double sum = 0;
int count = 0;
double classAverage = 0;
String currentClass = null;
File inFile = new File("C:\\Users\\Nemesis\\Desktop\\grade.txt");
PrintWriter output = new PrintWriter(new File("C:\\Users\\Nemesis\\Desktop\\grades1.txt"));
try
{
input = new Scanner(inFile);
while(input.hasNext())
{
currentClass = input.next();
int var1 = input.nextInt();
while(var1 != -9999)
{
sum += var1;
count++;
var1 = input.nextInt();
}
classAverage = sum/count;
System.out.println(currentClass);
System.out.println(classAverage);
output.println("Class : " + currentClass);
output.println("Class Average: " + classAverage);
output.println();
}
}
catch (IOException e)
{
System.out.println(e);
System.exit(0);
}
finally
{
output.close();
// input.close();
}
}
;
}
error:
java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.
:confused::confused::confused::confused:
-
so where is the main method?
- 10-17-2008, 06:04 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Main method is the entry point of the Java application like this. So you have to define it and start execution from there.
Even though without the main method can you compile this code. Before run you have to compile it. I'm wonder how did you get this error. There are lots of errors I can see on your code.Last edited by Eranga; 10-17-2008 at 06:13 AM.
- 10-17-2008, 08:22 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 15
- Rep Power
- 0
i got it.. i have my new code.. but i still have a new problem.. is how to sort their grades.. btw.. this is my new code
/**
* @(#)textFileRead.java
*
*
* @author
* @version 1.00 2008/10/17
*/
import java.io.*;
import java.util.*;
public class textFileRead {
static Scanner console = new Scanner(System.in);
public static void main(String[]args) throws FileNotFoundException
{
Scanner inFile = new Scanner(new FileReader("C:\\Users\\Nemesis\\Desktop\\grade.txt "));
//PrintWriter outFile = new PrintWriter("C:\\Documents and Settings\\elson.KINGJIM\\Desktop\\grade.out");
int num,count=0;
double tot=0;
num = inFile.nextInt();
double sum = 0;
double sort[]=new double[num];
//String First[]=new String[num*2];
//String Last[]=new String[num*2];
for(int i=0;i<num;i++)
/*while(inFile.hasNext())*/
{
String frstName =inFile.next();
String lstName = inFile.next();
double g1 = inFile.nextInt();
double g2 = inFile.nextInt();
double g3 = inFile.nextInt();
double g4 = inFile.nextInt();
double g5 = inFile.nextInt();
tot=g1+g2+g3+g4+g5;
sum=tot/5;
if(sum>=75)
{
System.out.printf("\n %s %s %.2f %.2f %.2f %.2f %.2f and the average is : %.2f Passed",frstName,lstName,g1,g2,g3,g4,g5,sum);
sort[count++] = sum;
//First[count++] = frstName;
//Last[count++] = lstName;
}
else
{
System.out.printf("\n %s %s %.2f %.2f %.2f %.2f %.2f and the average is : %.2f Failed",frstName,lstName,g1,g2,g3,g4,g5,sum);
sort[count++] = sum;
//First[count++] = frstName;
//Last[count++] = lstName;
}
}
System.out.println("\n\nSorted Grades");
System.out.print("-----------------------------\n");
Arrays.sort(sort);
for(int i=0;i<sort.length;i++)
{
System.out.println(" "+sort[i]);
}
System.out.println();
// outFile.close();
inFile.close();
}
}
- 10-17-2008, 08:33 AM #5
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
use code tags. [code][/ code] (without spaces)
I die a little on the inside...
Every time I get shot.
- 10-17-2008, 02:28 PM #6
I see the sort method being called in your code. Is the results of that correct?
If not, can you explain.Last edited by Norm; 10-17-2008 at 07:06 PM. Reason: correct typo
- 10-17-2008, 06:27 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You mean sorting the array? Can you explain it more. To sort an array, there are lots of approaches.
- 10-18-2008, 02:28 AM #8
Member
- Join Date
- Oct 2008
- Posts
- 15
- Rep Power
- 0
there is no error here.. but sir.. you can see that the code sort their average.. hmm.. how can i sort by their grades? hmm
- 10-18-2008, 02:49 AM #9
What would be the results of that sort?how can i sort by their grades?
The student(s) with the highest grade first. It would be possible for all of the students to have one of their grades be the highest grade and also the lowest grade. Say they all got 100 on one test and they all got 0 on another. How would they sort?
- 10-18-2008, 02:50 AM #10
Member
- Join Date
- Oct 2008
- Posts
- 15
- Rep Power
- 0
ascend sir.. thanks for the time.. ^^
- 10-18-2008, 06:58 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are you stuck on how to sort with the average grade and find what's name belongs to, is it?
- 10-18-2008, 09:50 AM #12
Member
- Join Date
- Oct 2008
- Posts
- 15
- Rep Power
- 0
yes, i dont know how to sort the grades of each names.. hmm help please
- 10-18-2008, 10:37 AM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Before that you have do a lot in your code. Start from the beginning. The way you are work out to read the file is incorrect. Basically input is mismatch there. Better to fix it first.
- 10-18-2008, 04:34 PM #14
Are you asking how to sort the grades for a student?
Where do you store the grades when you read them into the program? If they are in some kind of collection like an array, then you need to have a sort routine to sort the contents of that collection.
- 10-18-2008, 08:06 PM #15
Member
- Join Date
- Oct 2008
- Posts
- 15
- Rep Power
- 0
in the console.. do you have any codes?
- 10-18-2008, 08:10 PM #16
What does that mean?in the console..
- 10-18-2008, 10:11 PM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 10-19-2008, 06:59 AM #18
Member
- Join Date
- Oct 2008
- Posts
- 15
- Rep Power
- 0
here sir.. i want to sort their different grades.. then compile it in the java console.. and run.. jejE sorry just a beginner in java.. jejE
- 10-19-2008, 02:52 PM #19
What does that mean?then compile it in the java console
You compile a java source program with the java command.
The java console is where the browser displays STD output from the jre.
Are you asking how to output the results of the sort to the console?
Use the System.out.println() method to put output on the console.
- 10-19-2008, 05:38 PM #20
Member
- Join Date
- Oct 2008
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Reading Integers from a text file
By tress in forum New To JavaReplies: 6Last Post: 02-26-2011, 05:45 PM -
[SOLVED] Reading a text file into an Array
By DonCash in forum New To JavaReplies: 13Last Post: 01-25-2011, 12:51 AM -
Reading a zip/text file present in a MAC Bundle
By Prasannaa in forum Advanced JavaReplies: 1Last Post: 08-07-2008, 03:05 PM -
Reading two text file and sum them up
By matt_well in forum New To JavaReplies: 36Last Post: 07-22-2008, 02:55 AM -
Reading text file
By Lennon-Guru in forum New To JavaReplies: 1Last Post: 12-15-2007, 11:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks