Results 1 to 5 of 5
  1. #1
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default Someone plz help... how to block inputs

    (beginner question) I have a problem on my java assignment ~ I need to block an input if the user types letters or numbers... someone plz help TT^TT

    heres my code so far:

    Java Code:
    import java.io.*;
    
    
    public class GradingSystem
    {
        public void Input()throws IOException{
        BufferedReader userInput=new BufferedReader(new InputStreamReader(System.in));
       
        String subject=" ", familyname=" ", firstname=" ", idnumber=" ", subject1=" ";
        double prelim=0.0, midterm=0.0, finals=0.0;
       
       
        try{
        System.out.println("\n\nenter subject:");
        subject1 = userInput.readLine();
        }catch(IOException ioe){}
       
        try{
        System.out.println("enter ID number:");
        idnumber = userInput.readLine();
            if(idnumber==" ")
       
        }catch(NumberFormatException nfe){System.out.println(nfe);}
       
        
        System.out.println("enter first name:");
        firstname = userInput.readLine(); [B]//i need to block or make it an error if the user types numbers[/B]
       
        System.out.println("enter family name:");
        familyname = userInput.readLine(); [B]// need to block or make it an error if the user types numbers[/B]
       
       
        
        System.out.println("enter prelim grade:");
        prelim=Integer.parseInt(userInput.readLine()); [B]//need to block or make it an error if the user types letters[/B]
       
        System.out.println("enter midterm grade:");
        midterm=Integer.parseInt(userInput.readLine());
       
        System.out.println("enter finals grade:");
        finals=Integer.parseInt(userInput.readLine());
        }catch(NumberFormatException nfe){System.out.println(nfe);}
       
        prelim*=(.3);
        midterm*=(.3);
        finals*=(.4);
        finals+= prelim+midterm;
       
        System.out.println("\n"+"\n"+subject1+"\n"+idnumber+"\n"+"\n"+firstname+", "+familyname+"\n"+"\n"+"Grade"+"\n"+"Prelim Grade: "+prelim+"\n"+"Midterm  Grade: "+midterm+"\n"+"Finals Grade: "+finals);
       
       
        System.out.println("Subject Grade: "+finals);
       
        }
       
    }
    Moderator Edit: code tags added to improve code readability
    Last edited by Fubarable; 02-28-2010 at 08:14 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Consider placing the parseInt line in a try/catch NumberFormatException block. As for the name input, I'm not sure you want to trap numbers. What if someone is named James Spencer, 3rd?

  3. #3
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    thnx for the reply, it gave me an idea... xD btw can you give me an idea on what other things you can put on the "try/catch" to block other inputs.... (coz i dont have an idea on what its called or what to search on the internet)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

Similar Threads

  1. if statement with multiple inputs?
    By soc86 in forum New To Java
    Replies: 3
    Last Post: 01-20-2009, 05:44 AM
  2. temperature program with inputs
    By JingGong in forum New To Java
    Replies: 3
    Last Post: 10-17-2008, 07:34 PM
  3. Delay on inputs during calculation
    By matt_well in forum New To Java
    Replies: 14
    Last Post: 07-26-2008, 05:17 PM
  4. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 05:22 PM
  5. Date Inputs
    By hiranya in forum AWT / Swing
    Replies: 3
    Last Post: 11-06-2007, 06:11 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
  •