Results 1 to 2 of 2
Thread: File IO & useDelimiter
- 03-28-2011, 06:04 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 19
- Rep Power
- 0
File IO & useDelimiter
Hi,
I've an input file contains:
I wrote a code to make some calculations to print them in an output file. The problem is that I want to use "useDelimiter" to track the space and split the data line e.g.:Smith 27 83.7
Jones 21 28.35
Walker 96 182.4
Doe 60 150
Wood 100 400
Street 33 57.4
Taylor 83 190
Davis 110 198
Smart 75 292.5
Bird 84 168
Summers 52 83.2
Name = Smith.
credit Hours = 27.
credit Points = 83.7
I used "StringTokenizer" and it worked perfectly. Now I want to use "useDelimiter", but I don't know why it's not working.
This is my code:
Can you help me, please?PHP Code:import java.util.*; import java.io.*; import java.text.*; public class Warning { // -------------------------------------------------------------------- // Reads student data (name, semester hours, credit points) from a // text file, computes the gpa, then writes data to another file // if the student is placed on academic warning. // -------------------------------------------------------------------- public static void main (String[] args) { int creditHrs; // number of semester hours earned double creditPts; // number of credit points earned double gpa; // grade point (credit point) average String line, name, inputFileName = "Assign8bStudents.dat"; String outputFileName = "Assign8cWarning.dat"; DecimalFormat numFormat = new DecimalFormat("#.##"); Scanner lineScan; try { //input file stream is inFile File inputStream = new File(inputFileName); Scanner inFile = new Scanner(inputStream); //output file stream is outFile PrintWriter outputStream = new PrintWriter(outputFileName); outputStream.println (); outputStream.println ("Students on Academic Warning"); outputStream.println (); outputStream.println ("Name" + "\t " + "Credit Hrs" + "\t" + "GPA"); // Process the input file, one token at a time while (inFile.hasNextLine()) { line = inFile.next(); lineScan = new Scanner(line); lineScan.useDelimiter(" "); name = lineScan.next(); try { while(lineScan.hasNext()) { creditHrs = Integer.parseInt(lineScan.next()); creditPts = Double.parseDouble(lineScan.next()); // Get the credit hours and credit points and gpa = creditPts/creditHrs; if(((gpa < 1.5) && (creditHrs < 30)) || ((gpa < 1.75) && (creditHrs < 60)) || ((gpa < 2.0) && (creditHrs >= 60))) { outputStream.println(name + "\t\t" + creditHrs + "\t" + numFormat.format(gpa)); //print name in output file }// end if } }// end try catch (NumberFormatException e1) { System.out.print("There is an erorr in this line: "); System.out.println(line); } line = inFile.nextLine(); }// end while inFile.close(); outputStream.close(); }// end try catch (FileNotFoundException e2) { System.out.println("\"Assign8bStudents.dat\" not found."); } catch (IOException e3) { System.out.println(e3); } }// end main() }// end class: WarningLast edited by alihht; 03-28-2011 at 06:07 AM.
- 03-28-2011, 08:59 AM #2
Member
- Join Date
- Nov 2009
- Posts
- 19
- Rep Power
- 0
Sorry, Just forget about it.
I just face some weird people in a different forum thinking that I'm trying to cheat, while I'm asking them to help me to learn.
I have another code that works fine and print the correct output.
All what I wanted is to learn another way. Thank you any ways.
System.out.println("Regards,");
System.exit(0);
Similar Threads
-
Write a program that sorts data from a text file and sort them in a file
By danmgz45 in forum New To JavaReplies: 6Last Post: 12-01-2010, 05:31 AM -
Sending a File from Server to Client and saving the file to Clients computer
By al_Marshy_1981 in forum NetworkingReplies: 8Last Post: 02-18-2010, 12:54 PM -
useDelimiter usage
By reis3k in forum New To JavaReplies: 4Last Post: 12-24-2009, 11:01 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How to parse the CSV(Comma separation values)file and validate the file using java
By padmajap13 in forum Advanced JavaReplies: 7Last Post: 05-23-2008, 03:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks