Results 1 to 12 of 12

Thread: Why error?

  1. #1
    JavaNinja is offline Member
    Join Date
    Jul 2015
    Posts
    8
    Rep Power
    0

    Default Why error?

    Ok so i've fixed the error but it still only gives an 0 as output whats wrong with it

    Java Code:
    import java.util.Scanner;
    
    public class Algorithm1 {
    public static int FirstNumber, SecondNumber, TheAnswer;
    public static String Action;
    public static Scanner ScanInt1, ScanString;
    public static boolean hasString;
    public static boolean hasString2, hasInt;
    public static void main(String args[]){
    hasString = false;//sets up the booleans
    hasString = false;
    hasInt = false;
    ScanInt1 = new Scanner(System.in);
    ScanString = new Scanner(System.in);
    while  (FirstNumber < 1){
    FirstNumber = ScanInt1.nextInt();	
    }
    System.out.println("The SecondNumber Please");
    hasString = true;
    if (SecondNumber < 1){
    SecondNumber = ScanInt1.nextInt();		
    }
    else {
    System.out.println("Now your action please");
    hasString2 = true;
    }
    Action = ScanString.nextLine();
    System.out.println(TheAnswer);
    }
    public static void Answer(){
    //This method is for the math
    	if (Action.equals("*")){
    		TheAnswer = FirstNumber * SecondNumber;
    		
    	} 
    if (Action.equals("/")){
    	TheAnswer = FirstNumber / SecondNumber;
    	
    }
    if (Action.equals("+")){
    	TheAnswer = FirstNumber + SecondNumber;
    	
    }
    if (Action.equals("-")){
    	TheAnswer = FirstNumber - SecondNumber;
    	
    }
    
    }
    
    }
    Last edited by JavaNinja; 07-07-2015 at 12:24 PM.

  2. #2
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Why error?

    What error?

  3. #3
    JavaNinja is offline Member
    Join Date
    Jul 2015
    Posts
    8
    Rep Power
    0

    Default Re: Why error?

    Quote Originally Posted by trcooke View Post
    What error?
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Algorithm1.main(Algorithm1.java:21)

  4. #4
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Why error?

    What do the docs say for Scanner nextInt()?

    Scanner (Java Platform SE 8 )

  5. #5
    JavaNinja is offline Member
    Join Date
    Jul 2015
    Posts
    8
    Rep Power
    0

    Default Re: Why error?

    I've fixed the error but now it only gives 0 as an output

  6. #6
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Why error?

    For what input?

  7. #7
    JavaNinja is offline Member
    Join Date
    Jul 2015
    Posts
    8
    Rep Power
    0

    Default Re: Why error?

    Quote Originally Posted by trcooke View Post
    For what input?
    What do you mean?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    32

    Default Re: Why error?

    Can you copy the full contents of the command prompt window from when you execute the program and paste it here?
    It should should everything that the program prints and what the user input.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    384
    Rep Power
    10

    Default Re: Why error?

    Quote Originally Posted by JavaNinja View Post
    What do you mean?
    You said that 'I've fixed the error but now it only gives 0 as an output'

    What input did you enter into your program?

  10. #10
    JavaNinja is offline Member
    Join Date
    Jul 2015
    Posts
    8
    Rep Power
    0

    Default Re: Why error?

    Quote Originally Posted by sehudson View Post
    You said that 'I've fixed the error but now it only gives 0 as an output'

    What input did you enter into your program?
    It did it every time but i've fixed it. Yes !!!!!

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,966
    Rep Power
    32

    Default Re: Why error?

    i've fixed it. Yes !!!!!
    Is your problem solved now?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Why error?

    JavaNinja, I'd encourage you to not go back and significantly edit your earlier posts as it makes the replies look like nonsense. Continuing the conversation in new posts is just fine.

    Also, would you like to share what you did to fix your problem?

Similar Threads

  1. Replies: 3
    Last Post: 05-08-2012, 05:26 PM
  2. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  3. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  4. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •