Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Can't work out how to detect and incorrect input

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't work out how to detect and incorrect input

    I'm making a calculator that asks for inputs in the console and prints answers, nothing fancy. I've got it all working, even detecting if you have inputed an integer lower than 0 when square rooting.

    At the moment I have it so you type in a string which asks you to type in integers. The problem I'm having is detecting if the string is wrong rather than the string and the integer.

    Here's my code:

    import java.util.Scanner;
    import static java.lang.System.*;
     
    public class CalculatorConsole {
     
    		@SuppressWarnings({ "resource" })
    		public static void main(String args[]) {
     
    			out.println("Please select an operation from the list:");
    			out.println("- sqrt");
    			out.println("- add");
    			out.println("- subtract");
    			out.println("- times");
    			out.println("- divide");
    			out.println("- terminate");
     
    			String sqrt = "sqrt";
    			String add = "add";
    			String subtract = "subtract";
    			String times = "times";
    			String divide = "divide";
    			String terminate = "terminate";
     
    			int loop = 0;
     
    			while (loop != 1) {
     
    				Scanner input = new Scanner(in);
    				String output = input.next();
     
    				if (output.equals(sqrt)) {
    					out.print("Please enter a number to square root: ");
     
    						int sqrtInput = input.nextInt();
     
    						if (sqrtInput < 0) {
    							out.println("You are a silly person, you cannot use anything below zero");
     
    						} else {
     
    							out.printf("The square root of " + sqrtInput + " is %1.1f\n", Math.sqrt(sqrtInput));
     
    						}
    				}
     
    					if (output.equals(add)) {
    					out.println("Please enter two numbers to add: ");
     
    						int addInput1 = input.nextInt();
    						int addInput2 = input.nextInt();
     
    							out.println((addInput1) + " + " + (addInput2) + " = " + (addInput1 + addInput2));
     
    				}
     
    					if (output.equals(subtract)) {
    						out.println("Please enter two numbers to subtract: ");
     
    							int subtractInput1 = input.nextInt();
    							int subtractInput2 = input.nextInt();
     
    								out.println((subtractInput1) + " - " + (subtractInput2) + " = " + (subtractInput1 - subtractInput2));
     
    					}
     
    						if (output.equals(times)) {
    							out.println("Please enter two numbers to times: ");
     
    								int timesInput1 = input.nextInt();
    								int timesInput2 = input.nextInt();
     
    									out.println((timesInput1) + " * " + (timesInput2) + " = " + (timesInput1 * timesInput2));
     
    						}
     
    							if (output.equals(divide)) {
    								out.println("Please enter two numbers to divide: ");
     
    									int divideInput1 = input.nextInt();
    									int divideInput2 = input.nextInt();
     
    										if (divideInput2 == 0) {
    											out.println("You are a silly person, you cannot use 0");
    										} else {
     
    										out.println((divideInput1) + " / " + (divideInput2) + " = " + (divideInput1 / divideInput2));
    										}	
    							}
     
    								if (output.equals(terminate)) {
    									out.println();
    									out.println("##################");
    									out.println("PROGRAM TERMINATED");
    									out.println("##################");
     
    										loop++;
     
    								}
     
    								if (!output.equals (sqrt + times + add + divide + subtract)) {
    									out.println("Incorrect input");
     
    								}
    									if (loop != 1) {
    										out.println();
    										out.println("Please choose another operation");
     
    									}
     
    		}
    	}
    }

    Thanks in advance!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can't work out how to detect and incorrect input

    In order to check one thing regardless of the other, check one item first (string). If the item is wrong, ask for input until it is correct. Then move on and work on the next part (integers).

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't work out how to detect and incorrect input

    Thanks for the tip, but could you help me with my code please? I learn better that way, thanks.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can't work out how to detect and incorrect input

    Quote Originally Posted by DrPerry View Post
    Thanks for the tip, but could you help me with my code please? I learn better that way, thanks.
    Spoonfeed you working code? No. That would make me a bad person.

    You said you are having problems detecting if the string is wrong rather than the string and the integer.
    So take it one step at a time.

    First get a string from the user.
    Check the string to see if it is valid for your purpose.
    ---if not, repeat the 2 steps above.
    ---if so, move on to the next part.

    Get an integer from the user.
    Check the integer to see if it is valid for the purpose chosen by the user's string.
    ---if not, repeat the 2 steps above.
    ---if so, move on to the next part.

    Get the second integer from the user.
    Check the integer to see if it is valid for the purpose chosen by the user's string.
    ---if not, repeat the 2 steps above.
    ---if so, move on to the next part.

    Calculate the result based on the string the user selected to start.
    Output the result to the user.
    Ask the user if they want to keep using the calculator.
    Get the user's reply.
    Check the reply to see if it is valid for the purpose of using the calculator more or exiting the program.
    ---if not, repeat the 2 steps above.
    ---if so, start the calcuator over or exit the program.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't work out how to detect and incorrect input

    I don't feel like that helped me, also I only came here because I wanted someone to tell me how to fix my code, not tell me how to figure out how to fix my code. It's not exactly spoonfeeding if I only need help with one issue in my code and it's the first time I've needed it.

    I appreciate the help you've given me, but could you please just tell me how to fix it? If not, I'll just move onto another forum. Thanks.

  6. #6
    Junior Member
    Join Date
    Aug 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't work out how to detect and incorrect input

    Oh it doesn't matter anyway, I fixed it. I just didn't think of it properly, derp. :/

    I made an 'incorrect input tester', so if you do put a correct string it, it changes an integer which makes it so it doesn't show the message "incorrect input", otherwise it does.

    Here's the updated code if anyone's even the slightest bit interested:
    import java.util.Scanner;
    import static java.lang.System.*;
     
    public class CalculatorConsole {
     
    		@SuppressWarnings({ "resource" })
    		public static void main(String args[]) {
     
    			out.println("Please select an operation from the list:");
    			out.println("- sqrt");
    			out.println("- add");
    			out.println("- subtract");
    			out.println("- times");
    			out.println("- divide");
    			out.println("- terminate");
     
    			String sqrt = "sqrt";
    			String add = "add";
    			String subtract = "subtract";
    			String times = "times";
    			String divide = "divide";
    			String terminate = "terminate";
     
    			int loop = 0;
    			int incorrectInputTester = 0;
     
    			while (loop != 1) {
     
    				Scanner input = new Scanner(in);
    				String output = input.next();
     
    				if (output.equals(sqrt)) {
    					out.print("Please enter a number to square root: ");
    					incorrectInputTester = 1;
     
    						int sqrtInput = input.nextInt();
     
    						if (sqrtInput < 0) {
    							out.println("You are a silly person, you cannot use anything below zero");
     
    						} else {
     
    							out.printf("The square root of " + sqrtInput + " is %1.1f\n", Math.sqrt(sqrtInput));
     
    						}
    				}
     
    					if (output.equals(add)) {
    					out.println("Please enter two numbers to add: ");
    					incorrectInputTester = 1;
     
    						int addInput1 = input.nextInt();
    						int addInput2 = input.nextInt();
     
    							out.println((addInput1) + " + " + (addInput2) + " = " + (addInput1 + addInput2));
     
    				}
     
    					if (output.equals(subtract)) {
    						out.println("Please enter two numbers to subtract: ");
    						incorrectInputTester = 1;
     
    							int subtractInput1 = input.nextInt();
    							int subtractInput2 = input.nextInt();
     
    								out.println((subtractInput1) + " - " + (subtractInput2) + " = " + (subtractInput1 - subtractInput2));
     
    					}
     
    						if (output.equals(times)) {
    							out.println("Please enter two numbers to times: ");
    							incorrectInputTester = 1;
     
    								int timesInput1 = input.nextInt();
    								int timesInput2 = input.nextInt();
     
    									out.println((timesInput1) + " * " + (timesInput2) + " = " + (timesInput1 * timesInput2));
     
    						}
     
    							if (output.equals(divide)) {
    								out.println("Please enter two numbers to divide: ");
    								incorrectInputTester = 1;
     
    									int divideInput1 = input.nextInt();
    									int divideInput2 = input.nextInt();
     
    										if (divideInput2 == 0) {
    											out.println("You are a silly person, you cannot use 0");
    										} else {
     
    										out.println((divideInput1) + " / " + (divideInput2) + " = " + (divideInput1 / divideInput2));
    										}	
    							}
     
    								if (output.equals(terminate)) {
    									incorrectInputTester = 1;
     
    									out.println();
    									out.println("##################");
    									out.println("PROGRAM TERMINATED");
    									out.println("##################");
     
    										loop++;
     
    								}
     
    								if (incorrectInputTester != 1) {
    									out.println("Incorrect input");
    									incorrectInputTester = 0;
    								}
    									if (loop != 1) {
    										out.println();
    										out.println("Please choose another operation");
     
    									}
     
    		}
    	}
    }
    Last edited by DrPerry; August 15th, 2012 at 07:59 PM.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can't work out how to detect and incorrect input

    Quote Originally Posted by DrPerry View Post
    ...I only came here because I wanted someone to tell me how to fix my code, not tell me how to figure out how to fix my code.
    Well that is not what I am here to do...


    Quote Originally Posted by DrPerry View Post
    It's not exactly spoonfeeding if I only need help with one issue in my code and it's the first time I've needed it.
    I offered help. In fact I wrote out part of your pseudocode for you.


    Quote Originally Posted by DrPerry View Post
    I appreciate the help you've given me, but could you please just tell me how to fix it?
    I told you how to fix it. Write code to do the actions in post #4.


    Quote Originally Posted by DrPerry View Post
    If not, I'll just move onto another forum.
    Don't let the door hit you in the backside on the way out. I can spend my time helping other people who want help instead of answers.


    Quote Originally Posted by DrPerry View Post
    Thanks.
    You are welcome.


    Quote Originally Posted by DrPerry View Post
    Oh it doesn't matter anyway, I fixed it.
    As long as you are happy with it I guess.

Similar Threads

  1. Incorrect results
    By kprofgold in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2012, 08:08 PM
  2. Java Calculation Incorrect
    By cow23 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 08:09 AM
  3. Incorrect Key Input
    By risen375 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 28th, 2011, 01:17 PM
  4. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM
  5. How to detect audio input from a telephone call?
    By ces_31 in forum Java Theory & Questions
    Replies: 0
    Last Post: August 12th, 2009, 12:45 AM