Results 1 to 2 of 2
  1. #1
    Sigmahokies is offline Member
    Join Date
    Apr 2017
    Location
    Richmond, VA
    Posts
    3
    Rep Power
    0

    Default Student Report - Help

    Hi everyone, I am learning how to do Java this year, I have been struggling with StringTokenizer to remove the middle name with period. Also, I am wondering if Java can tell what is different between string and number? because I like to take number into array as store, then use array to count, then sum all number, then divide with number of length (averageArray.length), then print output to create text in file. the problem is on 30 in list of Java.

    Here my code, but it is not success. Can you help me?

    PHP Code:
    package studentreport;
    
    import java.io.*;
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    public class StudentReport {
    
        public static void main(String[] args) throws FileNotFoundException {
            
            File file = new File("RawData.txt");
            File out = new File("StudentReport.txt");
       
            try {
                
                
                try (Scanner in = new Scanner(file)) {
                    
                    int count;
                    String temp;
                    String line;
                    
                    PrintWriter print = new PrintWriter(out);
                    print.println("Student Name \t \t Course Credit \t \t Grade");
                    print.println("----------------------------------------------------------");
                    line = in.nextLine();
                    
                    StringTokenizer tokens = new StringTokenizer(line);
                    
                     temp = tokens.nextToken(line);
                   
                    while(tokens.hasMoreTokens()) {
                          
                         if(temp.contains(".")) {
                             
                            temp = tokens.nextToken(temp);
                              
                         }
                        
                       print.println(temp);
                       
                    }print.close();
                }
                    
            } catch (FileNotFoundException e) {
                System.out.println(e.getMessage());
            
           } 
        }
    }
    I am trying to create text throughly programming in Java from RawData.txt to Student Report.txt.

    here my RawData.txt:
    Gary D. Taylor 87 90 92 97
    Taylor Adams 92 95 97
    Selman Jawad 88 87 92 91
    Joseph Davis 88 88 92 97
    Charity F. Morris 89 87 85 95
    Pranalika P. Patel 91 89 85
    Tammy Lester 89 90 95
    James Nickerson 95 92 91 94
    David Keller 95 88 85
    Tony Dickey 78 91 85
    Joe Cook 78 89 88 85
    Devin Hamilton 88 89 92 95
    Ruby Meadows 78 77 82 92
    Wendy Cowan 89 95 97 93
    Joseph Gardner 92 95 93
    Scott Deuel 88 85 87
    Mike Deuel 85 83 81
    Hedy D. Taylor 89 92 91 95
    Lily R. Taylor 89 88 83 94
    Billy C. Taylor Jr 80 82 85

    I will like to have programming in Java to create text file look like this:
    PHP Code:
    Student Name                 Number of credits             Student Average
    -------------------             -----------------------        ---------------------
    John Smith                                  9                                79.33
    Jennifer Harris                            12                               72.25
    what i need is removing the middle name with period and print output to text file.

    Thanks,
    Gary Taylor
    Last edited by Sigmahokies; 10-27-2017 at 12:33 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Student Report - Help

    You're using the tokenizer incorrectly (or at least, inefficiently). It implements the Enumerator interface (similar to Iterator) so
    the nextToken advances automatically:

    Java Code:
    String s = "This is some data 1 2 3 4";
    StringTokenizer t = new StringTokenizer(s);
    while (t.hasMoreTokens()) {
        String str = t.nextToken();
        System.out.println(str);
    }
    To solve you're int vs non-int problem I would just write a method to use the Integer.parseInt method and catch the exception. You can return some value to signal whether it was an integer or not.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. jasper report ,jasper report jar files and maven dependencies.
    By rajuchacha007 in forum Advanced Java
    Replies: 1
    Last Post: 04-09-2010, 08:15 AM
  2. I want to generate PDF report for student details?
    By eswaraaa in forum Java Servlet
    Replies: 1
    Last Post: 12-18-2009, 01:47 AM
  3. Student Help
    By mattwaab in forum Java Applets
    Replies: 0
    Last Post: 02-08-2008, 05:31 PM
  4. please help a student
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 02-01-2008, 01:11 AM

Tags for this Thread

Posting Permissions

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