473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

help with Scanner

4
Hey guys, im trying to finish an assignment for a java class, and i have finished the program except for one part. The program requires that i take 3 values inputted from the user, the first 2 must be digits between 0 and 9, and the third an operator.
the operator decides if the two numbers are added subtracted...etc. anyways the things i cannot get, is i need to check if they enter valid digits, and not letters, however if i do put a letter in, scanner gives me an exception. it happens at line 14 when you enter a letter.... how can i test if it is a letter... any suggestions?


here is my code

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.text.DecimalFormat;
  3.  
  4. public class problem1 {
  5. public static void main(String[]args){
  6.  
  7.     double num1,  num2, answ;
  8.  
  9.     String tString,num1Word = " ",num2Word= " ";
  10.     Scanner scan = new Scanner(System.in);
  11.     DecimalFormat formatter = new DecimalFormat("0");
  12.  
  13.     System.out.println("Please enter two numbers between 0 and 9 ");
  14.     num1= scan.nextDouble();
  15.     num2= scan.nextDouble();
  16.  
  17.     if(Character.isDigit((Double.toString(num1)).charAt(0))){
  18.  
  19.     }
  20.     else{System.out.println("Please Enter Digits only!");
  21.     System.exit(0);
  22.     }
  23.  
  24.     switch((int)num1){
  25.     case 0: num1Word= "Zero";
  26.     break;
  27.     case 1: num1Word= "One";
  28.     break;
  29.     case 2: num1Word= "Two";
  30.     break;
  31.     case 3: num1Word= "Three";
  32.     break;
  33.     case 4: num1Word= "Four";
  34.     break;
  35.     case 5: num1Word= "Five";
  36.     break;
  37.     case 6: num1Word= "Six";
  38.     break;
  39.     case 7: num1Word= "Seven";
  40.     break;
  41.     case 8: num1Word= "Eight";
  42.     break;
  43.     case 9: num1Word= "Nine";
  44.     break;
  45.     default: 
  46.         System.out.println("Invalid Number");
  47.     System.exit(0);
  48.     break;
  49.     }
  50.     switch((int)num2){
  51.     case 0: num2Word= "Zero";
  52.     break;
  53.     case 1: num2Word= "One";
  54.     break;
  55.     case 2: num2Word= "Two";
  56.     break;
  57.     case 3: num2Word= "Three";
  58.     break;
  59.     case 4: num2Word= "Four";
  60.     break;
  61.     case 5: num2Word= "Five";
  62.     break;
  63.     case 6: num2Word= "Six";
  64.     break;
  65.     case 7: num2Word= "Seven";
  66.     break;
  67.     case 8: num2Word= "Eight";
  68.     break;
  69.     case 9: num2Word= "Nine";
  70.     break;
  71.     default: 
  72.         System.out.println("Invalid Number");
  73.     System.exit(0);
  74.     break;
  75.     }
  76.     System.out.println("Please enter the mathematical symbol for what you would like to do with these numbers. ");
  77.     tString = scan.next();
  78.     char operator = tString.charAt(0);
  79.  
  80.  
  81.  
  82.  
  83.     switch(operator){
  84.     case '*': answ= num1*num2;
  85.         System.out.println(num1Word+ " Multiplied by "+num2Word+" is "+formatter.format(answ));
  86.         break;
  87.     case '/': if(num2 == 0){
  88.         System.out.println("You cannot divide by zero, session terminated.");
  89.  
  90.     }
  91.     else{
  92.         answ= num1/num2;
  93.         System.out.println(num1Word+ " Divided by "+num2Word+" is "+formatter.format(answ));
  94.     }
  95.     break;
  96.     case '+': answ= num1 + num2;
  97.     System.out.println(num1Word+ " Added to "+num2Word+" is "+formatter.format(answ));
  98.     break;
  99.     case '-': answ= num1 - num2;
  100.     System.out.println(num1Word+ " Minus "+num2Word+" is "+formatter.format(answ));
  101.     break;
  102.     case '^': 
  103.         answ= Math.pow(num1,num2);
  104.     System.out.println(num1Word+ " raised to the power of " +num2Word+ " is "+ formatter.format(answ));
  105.     break;
  106.     default: System.out.println("Invalid Operator");
  107.     System.exit(0);
  108.     break;
  109.     }
  110.  
  111. }
  112. }
  113.  
Feb 19 '09 #1
2 2086
horace1
1,510 Expert 1GB
you can use method scanner.hasNextDouble() to check if the next token can be read as a double and if not ignore it, e.g.
Expand|Select|Wrap|Line Numbers
  1. System.out.println("Please enter two numbers between 0 and 9 ");
  2. while(!scan.hasNextDouble())
  3.    { 
  4.     System.out.printf("error, enter a double !"); 
  5.     scan.nextLine();          // ignore rest of line
  6.    }
  7. num1= scan.nextDouble();
  8.  
Feb 19 '09 #2
bvav22
4
i really cannot thank you enough, that was such a big help.... :)
Feb 19 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Andrew Bell | last post by:
I'm doing a uni course in the UK and one of my semesters is about programming. This is just going to be compilied and executed with no menu just using command promt (javac classfile.class) I am...
2
by: porky008 | last post by:
I can not figure out how to use an array to store the different things and the total is only showing 0.0 instead of the total of the inventory. Can I please get some help on this? // This calls...
1
by: klubbhead | last post by:
This part of my program needs to read from 4 parameter files and then displays them on the screen by clicking a different radio button. The program itself works, but my professor says that I have to...
4
nomad
by: nomad | last post by:
I'm very new to java and programing. I need some help with this. Check each user entry to ensure validity. Color is either Black, White, or Red. if the user enter the wrong color the following with...
5
by: sandyw | last post by:
I need help in two areas: Both are at the bottom of my program where a client has two choice to make, 1. When the client enter "Y" it takes him to the begin of the program. I would like to know...
5
by: koonda | last post by:
Hi all, I am a student and I have a project due 20th of this month, I mean May 20, 2007 after 8 days. The project is about creating a Connect Four Game. I have found some code examples on the...
15
by: djtosh | last post by:
hi there hoping someone can help me out with this. im designing a code that is a basic database holding peoples details and i need to implement a method that searches by txt for a record by their...
3
by: thename1000 | last post by:
Hi, I'm trying to create this output: Input team 1's name: Team 1 Input team 1's ranking: 90.4 etc.
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
1
by: ImortalSorrow | last post by:
Please I need urgent help with this program! Everything is working well except a part where i want to repeat the riddle if the answer isnt correct. here's the code..hope someone can help me! ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.