Welcome to Dream.In.Code
Become a Java Expert!

Join 149,477 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,790 people online right now. Registration is fast and FREE... Join Now!




loan calculator - option x (cancel)

 
Reply to this topicStart new topic

loan calculator - option x (cancel), how to choose option x

javanewbie07
8 May, 2007 - 11:25 PM
Post #1

New D.I.C Head
*

Joined: 8 May, 2007
Posts: 2


My Contributions
this is the menu for a loan calculator:
Please select one of the following options:
1. Calculate monthly payment
2. Calculate loan duration
x. Cancel
Enter your option (1, 2 or x) : x

i have used a switch statement for the variable option to choose the option desired.
CODE

switch (option)
      {
         // option 1
         case 1:


this is the same for option 2.

but as the variable option is declared as int, option x fails to work. how do i get the user to choose option x without any errors????

this is what's displayed when i choose option x:
CODE

Enter your option (1, 2 or x) : x
Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:819)
        at java.util.Scanner.next(Scanner.java:1431)
        at java.util.Scanner.nextInt(Scanner.java:2040)
        at java.util.Scanner.nextInt(Scanner.java:2000)
        at LoanCalculator.main(LoanCalculator.java:41)



User is offlineProfile CardPM
+Quote Post

Ellie
RE: Loan Calculator - Option X (cancel)
9 May, 2007 - 11:47 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions

What Scanner method are you using to get the input? If you use Scanner.nextInt() it will throw an error if you enter text.

Scanner.next(); will return you a String of the user input. You could compare that input with 'x' and it it matches that you could assign yout int to 3 to use in your switch statement. If not, then try to parse your input to an integer (Integer.parseInt(your input text)wink2.gif. This way, you can also catch unexpected inputs and deal with them too.

Hope that makes sense.
User is offlineProfile CardPM
+Quote Post

javanewbie07
RE: Loan Calculator - Option X (cancel)
9 May, 2007 - 07:40 PM
Post #3

New D.I.C Head
*

Joined: 8 May, 2007
Posts: 2


My Contributions
where would i put

Integer.parseInt(your input text)

in my code??
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Loan Calculator - Option X (cancel)
10 May, 2007 - 12:45 AM
Post #4

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
Here's a quick demo, it'll only take input once, but it should show you where to put everything

Also, I don't have the Scanner class, so I used a bufferedreader. It makes no difference though

CODE
import java.io.*;
public class DICSwitch {
    
    public static void main (String args[])
    {
        String in = "";
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try {
        
        in = reader.readLine();}
        catch (IOException e) {
            System.out.println(e);
        }
        int choice = 0;
        if (in.equals("x")) {
            choice = 3;
            
        }
        else
        try {
            choice = Integer.parseInt(in);
        } catch (NumberFormatException e) {
            System.out.println(e);
        }
        switch (choice)
        {
            case 1: System.out.println("Choice = 1");
            break;
            case 2: System.out.println("Choice = 2");
            break;
            case 3: System.out.println("Choice = 3");
            break;
            default: System.out.println("Choice invalid");
        }
        System.exit(0);
    }
    
    
}


This post has been edited by Ellie: 10 May, 2007 - 12:46 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:05PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month