Results 1 to 5 of 5
Thread: Help with Java problem
- 11-09-2011, 02:33 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Help with Java problem
Alright this one problem is giving me the hardest time of not really knowing where to start first. It looks so simple but then again I'm new to java. Any help would be appreciated.
Write a complete Java program to take input from a file ("input.txt" using Scanner) that is in the following format:
88 71 93
82 92 81
56 14 62
94 95 96
To an output file ("output.txt" using Formatter) and to the screen (System.out), output in the following format (use '\t' for a tab and AV means average, calculated by your program):
P1 P2 P3 AV
S1 88 71 93 84
S2 82 92 81 85
S3 56 14 62 44
S4 94 95 96 95
AV 80 68 83
- 11-09-2011, 02:39 AM #2
Re: Help with Java problem
Yes you do. You know how to write a class. You know how to write a method. Well thats a start. Next step is to read a single line from the file and display it to the screen. Once you have that working, read ALL lines from the file and print to screen. Then keep going taking it one step at a time.
- 11-09-2011, 03:00 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 2
- Rep Power
- 0
Re: Help with Java problem
import java.io.File;
import java.util.Scanner;
public class project{
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner( new File( "input.txt"));
int grades;
System.out.printf("\t%-10s%-12s%-12s%10s\n", "P1", "P2","P3","AV");
while ( input.hasNext()){
grades = input.nextInt();
}
}
}
This is what I have got now. But since the grades are broken down to each student and then each test I'm not sure how to get the student part to show and along with that I'm not entirely sure how to do the average from the data given by a file.
- 11-09-2011, 03:28 AM #4
Re: Help with Java problem
Your data looks perfect for a 2D array. What I would do is read each line, then split it, then convert each String "00" into an int 00 and insert into the 2D array. You cannot calculate the averages until you have read and stored all the data. So why worry about it yet? This is what I meant by doing things one step at a time.
- 11-09-2011, 04:15 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Dhaka,Bangladesh
- Posts
- 178
- Rep Power
- 0
Re: Help with Java problem
here is an algorithm.just convert it in approprieate code
Java Code:for i=0 to i<4 for j=0 to j<3 read a[i][j] //a[][] is int //input section ends here print (P1 P2 P3 AV) for i=0 to i<3{ print(s,i+1) for j=0 to j<3 print(a[i][j]) print(value of last colum like 84) } print(av) for j=0 to j<3 print (a[3][j]) print(value of last colum like 84)Don't Forget to try yourself before asking others help.....
Press REP, if you find their advices/solutions effective.
Similar Threads
-
Problem with Java Web Applications and Java in Control Panel
By Abysinian in forum Advanced JavaReplies: 4Last Post: 03-16-2012, 11:29 AM -
Small problem with problem with Java, C++ parse program.
By dragstang86 in forum New To JavaReplies: 4Last Post: 10-30-2011, 03:43 AM -
Problem Display Jmenubar Java Se6 u23 versus Java SE6 u22
By Ravanelly in forum Advanced JavaReplies: 0Last Post: 01-07-2011, 09:36 AM -
problem with java --> sql
By bikkerss in forum New To JavaReplies: 8Last Post: 04-14-2010, 01:03 PM -
Problem with Java...
By mc6415 in forum New To JavaReplies: 10Last Post: 02-22-2010, 12:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks