Results 1 to 2 of 2
  1. #1
    BSOS is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Exclamation Help On Computer Science Final!!!! Java Program Help!!!!

    I need this program to compile with out any errors or warnings. Any help would be dearly appreciated.
    Java Code:
    import java.util.*;
    import java.io.*;
    /**
     * A class that represents a class score book.  This
     * book maps names to scores and grades.  This will
     * read the names, scores and grade information from a file.
     */
    public class CourseData2 {
     
      /////////////////// fields ///////////////////
     
      private String fileName;
      private Map phoneMap = new HashMap();
     
      /////////////////// constructors ///////////////////
      /**
       * Constructor that takes a file name and reads
       * in the names and phone numbers from a file.
       * @param file the name of the file to read
       */
      public CourseData2(String file) {
        this.fileName = file;
       
        // Read the map information in form the file
        readInfoFromFile();
      } // constructor
     
      /////////////////// methods ///////////////////
      /**
       * Get the phone number for hte passed nam
       * @param name the name to look up in hte map
       * @return the phone number if found, else null
       */
      public String getPhoneNumber(String name) {
        String phoneNumber = (String) phoneMap.get(name);
        return phoneNumber;
      } // method getPhoneNumber()
     
     
      /**
       * Method to read the phone information from a
       * file and use it to fill hte map
       */
      public void readInfoFromFile() {
        String line = null;
        String[] phoneArray = null;
       
        try {
         
          // create the reader
          BufferedReader reader =
            new BufferedReader(new FileReader(fileName));
         
          // loop reading from the file
          while ((line = reader.readLine()) != null) {
            while ((line = reader.readLine()) != null) {
            if (line.indexOf(":") >= 0) {
              phoneArray = line.split(":");
             
              int f = (String)phoneArray[2];
              if (0 <= f <= 100) {
                String a = null;
            if (0 <= f < 60) { a = "E";}
            else if (60 <= f < 70) { a = "D";}
            else if (70 <= f < 80) { a = "C";}
            else if (80 <= f < 90) { a = "B";}
            else if (90 <= f <= 100) { a = "A";}
              }
              else if ( ((int)f > 100) + ((int)f < 0) ) {
            SimpleOutput.showError("Score is not an integer");
            String g = new String();
            g = phoneArray[2];
              }//if < 0
            }//while 2
             
            phoneMap.put(phoneArray[0].trim(),
                        (phoneArray[1].trim() + phoneArray[2].trim() + a.trim()));
            } // if
            } // while
           
         
            /*if (f < 60) { (String)f = "E";}
            else if (60 <= f < 70) { (String)f = "D";}
            else if (70 <= f < 80) { (String)f = "C";}
            else if (70 <= f < 90) { (String)f = "B";}
            else if (90 <= f) { (String)a = "A";}*/
         
          // Close the reader
          reader.close();
         
        } // try
        catch (FileNotFoundException ex) { 
          SimpleOutput.showError("Could not find the file \"" + fileName + "\".");
        } // catch
        catch (Exception ex) {
          ex.printStackTrace();
        } // catch
      } // method readInfoFromFile()
     
     
      public void printBook() {
       
        // Sort hashtable.
        Vector phoneVector = new Vector(phoneMap.keySet());
        Collections.sort(phoneVector);
       
        // Display (sorted) hashtable.
        for (Enumeration phoneEnumeration = phoneVector.elements(); phoneEnumeration.hasMoreElements();) {
          String key = (String)phoneEnumeration.nextElement();
          System.out.println("Name: " + key + ", Year: " + phoneMap.get(key) + ", Score: " + phoneMap.get(key) + ", Grade: " + phoneMap.get(key));
        } // for
       
      } // method printBook()
     
      /* main method for testing */
      public static void main(String[] args) {
        CourseData2 phoneBook = new CourseData2("scores12.txt");
        System.out.println(phoneBook.getPhoneNumber("Lee"));
        System.out.println(phoneBook.getPhoneNumber("Smith"));
        phoneBook.printBook();
      } // main()
     
    } // class

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    		int f = (String) phoneArray[2];
    		if (0 <= f <= 100) {
    			String a = null;
    			if (0 <= f < 60) {
    				a = "E";
    				} else if (60 <= f < 70) {
    					a = "D";
    				} else if (70 <= f < 80) {
    					a = "C";
    				} else if (80 <= f < 90) {
    					a = "B";
    				} else if (90 <= f <= 100) {
    					a = "A";
    				}
    			} else if (((int) f > 100) + ((int) f < 0)) {
    		SimpleOutput.showError("Score is not an integer");
    		String g = new String();
    		g = phoneArray[2];
    Your problem is here: int f = (String) phoneArray[2];

    Try compiling it and seeing the error for yourself. I surely hope none actually takes this post seriously and does your homework for you.

Similar Threads

  1. How to scan your computer for ports in use
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 08:13 PM
  2. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  3. which are the final classes in the java APi
    By vagarwal_mca in forum New To Java
    Replies: 2
    Last Post: 01-10-2008, 02:47 AM
  4. Replies: 4
    Last Post: 11-27-2007, 11:59 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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