Results 1 to 2 of 2
  1. #1
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default File IO & useDelimiter

    Hi,

    I've an input file contains:

    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
    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.:
    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:

    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: Warning
    Can you help me, please?
    Last edited by alihht; 03-28-2011 at 06:07 AM.

  2. #2
    alihht is offline Member
    Join Date
    Nov 2009
    Posts
    19
    Rep Power
    0

    Default

    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

  1. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  2. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  3. useDelimiter usage
    By reis3k in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 11:01 AM
  4. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  5. Replies: 7
    Last Post: 05-23-2008, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •