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 5 of 5

Thread: Problem with if

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Problem with if

    import java.util.Scanner;

    public class aritmetik {

    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    int tal1, tal2, result;

    System.out.print("Enter a number: ");
    tal1 = keyboard.nextInt();
    System.out.print("Enter another number: ");
    tal2 = keyboard.nextInt();

    System.out.println("How much is: " +tal1 + "+" +tal2);
    result = keyboard.nextInt(tal1 + tal2);


    if(tal1 + tal2 == result){
    System.out.println("The answer was correct!");
    }else{
    System.out.println("The answer was wrong!");
    }
    }
    }


    In future purposes I will add some kind of a loop (when I´ve learned a bit about it) and asking the user again and again if the answer is wrong. But for now I just want this code to work.

    Something is wrong with the If statement, dont really now what but I get this error message:
    Exception in thread "main" java.util.InputMismatchException: For input string: "8"
    at java.util.Scanner.nextInt(Scanner.java:2097)
    at aritmetik.main(aritmetik.java:16)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with if

    What is the value of expression in the call to nextInt() on line 16?
    Why are you using those args with the nextInt() method?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with if

    Quote Originally Posted by Norm View Post
    What is the value of expression in the call to nextInt() on line 16?
    Why are you using those args with the nextInt() method?
    @Norm I really don´t know. Just started programming two days ago. Suggestions?

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with if

    I made it work Norm ;D


    import java.util.Scanner;

    public class aritmetik {

    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    int tal1, tal2, resultat;

    System.out.print("Ange ett heltal: ");
    tal1 = keyboard.nextInt();
    System.out.print("Ange ett till heltal: ");
    tal2 = keyboard.nextInt();

    System.out.println("Vad blir: " +tal1 + "+" +tal2 +"?");
    resultat = keyboard.nextInt();

    if(resultat == tal1 + tal2){
    System.out.println("Du svarade rätt");
    }else{
    System.out.println("Du svarade fel");
    }
    }
    }

    Sometimes you just gotta think things through a couple of times. Now I want to tweak my code, I want to make a loop where the program asks the user if he wants to continue with a new number YES/NO if yes then make the process once more. Is it a do loop?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with if

    Yes, a do{}while() would work.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM