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: Loops that work for the most part except for after incorrect user input

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loops that work for the most part except for after incorrect user input

    This is a project for my class and I'm new to coding. It works if valid user input is done the first time but generates exception when invalid input is done (error info at the bottom). I swear i had this working correctly a few days ago.

    import java.util.Scanner;

    public class DingDong {
    public static void main(String[] args) {
    boolean done;
    do {
    int n1,n2;
    Scanner kb = new Scanner(System.in);

    System.out.print("Enter two integers between 2 and 15 (included): ");
    n1 = kb.nextInt();
    n2 = kb.nextInt();

    if (!((!(n1 < 2) && !(n1 > 15)) && (!(n2 < 2) && !(n2 > 15)))) {
    done = false;
    System.out.print("Invalid input. Please try again.");

    } else {
    done = true;
    double i = 1;

    while (i <= 50){
    if ((i/n1 % 1 == 0) && (i/n2 % 1 == 0)){
    System.out.printf("DingDong ");
    } else if (i/n1 % 1 == 0){
    System.out.printf("Ding ");
    } else if (i/n2 % 1 == 0){
    System.out.printf("Dong ");
    } else {
    System.out.printf("%.0f ", i);
    }

    i++;
    }
    }
    kb.close();

    } while (!done);


    }
    }

    Invalid input. Please try again.Exception in thread "main" Enter two integers between 2 and 15 (included): java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at DingDong.main(DingDong.java:11)

    Thank you for any input.

    Wish i knew how to delete this post now.
    Last edited by VampyreBowler; November 3rd, 2017 at 10:30 PM.

  2. #2
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Loops that work for the most part except for after incorrect user input

    What is included? It says it throws an exception. It won't compile. The value you desire probably requires more information. Can you have a default value set when you compile the class file? Maybe. System.out.println("included: +" n1 ); like this? The conditional operators such as && and || are not clear. Correct the boolean statements. If then, else...

    You have 3 conditions, less 2, greater than 15, that are false. 2>= || <= 15 are true. Set up your conditions with the 3 known output.

    Ok. The program should print only this.. use a do while loop which continues until a condition is met. A printoutln for this condition is "not included".

    Included: "n1"
    Included: "n2"

    Or not included...
    Last edited by planetHollywood; December 21st, 2017 at 11:35 PM.

Similar Threads

  1. JCIFS - Incorrect Authenticated User Identified
    By Ahijah in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 22nd, 2014, 01:08 PM
  2. Trying to get user input to work correctly
    By jcfor3ver in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 6th, 2013, 08:08 AM
  3. [SOLVED] How do I make a picture-part interact with a user?
    By SOG in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 12th, 2013, 08:34 PM
  4. Can't work out how to detect and incorrect input
    By DrPerry in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 15th, 2012, 10:27 PM
  5. Incorrect Key Input
    By risen375 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 28th, 2011, 01:17 PM