Results 1 to 5 of 5
- 02-28-2010, 06:30 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
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:
Moderator Edit: code tags added to improve code readabilityJava 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); } }Last edited by Fubarable; 02-28-2010 at 07:14 AM. Reason: code tags added
-
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?
- 03-01-2010, 01:12 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
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)
-
- 03-15-2010, 06:44 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
if statement with multiple inputs?
By soc86 in forum New To JavaReplies: 3Last Post: 01-20-2009, 04:44 AM -
temperature program with inputs
By JingGong in forum New To JavaReplies: 3Last Post: 10-17-2008, 06:34 PM -
Delay on inputs during calculation
By matt_well in forum New To JavaReplies: 14Last Post: 07-26-2008, 04:17 PM -
How to create this if many inputs?
By sarahannel123 in forum New To JavaReplies: 3Last Post: 05-18-2008, 04:22 PM -
Date Inputs
By hiranya in forum AWT / SwingReplies: 3Last Post: 11-06-2007, 05:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks