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

Thread: Problem with the output

  1. #1
    Junior Member
    Join Date
    May 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Problem with the output

    Hey Guys! I have an issue here. This is my current exercise what I must complete but the but the output is not what I want. This is my first post btw and I wait your hints, tips and help. The problem is that my result println do not print Result 5 for the output but print Result 2. I try to change catch block and change nextInt(); methods and correct the if statement but canīt figure out what is the issue.
    I appreciate any help ! Thanks !

    import java.util.Scanner;
    import java.util.*;

    public class ExceptionInAddition {
    public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int firstNumber = 0, secondNumber = 0, result;
    boolean inputCorrect = true;

    do {
    try {
    System.out.println("Type in the first number: ");
    firstNumber = reader.nextInt();
    inputCorrect = true;
    } catch (java.util.InputMismatchException p) {
    inputCorrect = false;
    }
    } while (!inputCorrect);

    do {
    try {
    System.out.println("Type in the second number: ");
    reader.next();
    inputCorrect = true;
    } catch (java.util.InputMismatchException p) {
    inputCorrect = false;
    }
    } while (!inputCorrect || (secondNumber == 0));
    if (inputCorrect == true) {
    result = firstNumber + secondNumber;
    System.out.println("Result: " + result);
    } else {
    System.out.println("You did not type in an integer!");
    }
    }

  2. #2
    Junior Member
    Join Date
    Mar 2018
    Posts
    10
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Problem with the output

    What input are you using? The reason I ask is because the way the code is now it will never print the result... unless i'm miss reading the question.

    The reason it will not print any result is because the variable "secondNumber" is never set. So it is always zero. That means it will not go past the while loop for the second number.

    As a separate issue, it might be a good idea to add the line "reader.nextLine();" after "inputCorrect = false;" for both instances. That will allow the Scanner to block while the user enters the input. There's a good explanation for this here: https://stackoverflow.com/a/13102066 Otherwise there will be some unexpected behavior after any invalid input.
    Last edited by User2009; May 16th, 2018 at 07:44 PM. Reason: elaboration of original response.

Similar Threads

  1. [SOLVED] Java runtime get result output from prompt problem with a larger output in type
    By kingwang98 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2014, 08:52 AM
  2. problem in understanding output.
    By vinaysa86 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 5th, 2012, 01:22 AM
  3. I cannot get the right output in the right place. Where is my problem?
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2011, 07:28 AM
  4. problem with output
    By Timur in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 11th, 2011, 09:56 AM
  5. JTextArea output problem
    By grimx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 8th, 2010, 07:54 PM