2

I am trying to learn "if else" statements and I am having trouble with the middle 'if else' part of the script.

package practice;
import java.util.Scanner;
public class Practice {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner in = new Scanner(System.in);
    System.out.println("enter a number between 1 and 10 ");
    if (!in.hasNextDouble()) {
        String word = in.next();
        System.err.println(word + " is not a number");
    } else if (!(in.nextDouble() > 0) || !(in.nextDouble() <= 10)) {
        Double wrongnumber = in.nextDouble();
        System.err.println(wrongnumber + " is not between 1 and 10");
    } else {
        System.out.println("It works!");
    }
       return;

   }

 }

There are no errors but in the 'else if' block I can't get it to print the err "..... not between 1 and 10", whether or not I put a number between 1 and 10 or higher. It also wont print the "it works!" line anymore when I add the 'else if' block. any suggestions would be much appreciated.

1
  • nextDouble, is going to take a new Double everytime it is called. You should call it once and store the value in a variable. Double number = in.nextDouble; Commented Aug 20, 2018 at 13:49

4 Answers 4

4

You are caling in.nextDouble() several times in your if else block, so you get something else every time.

if (!(in.nextDouble() > 0) || !(in.nextDouble() <= 10)) {
        Double wrongnumber = in.nextDouble();
        System.err.println(wrongnumber + " is not between 1 and 10");
}

convert it to something like

double next = in.nextDouble();
if (!(next > 0) || !(next <= 10)) {
            Double wrongnumber = next;
            System.err.println(wrongnumber + " is not between 1 and 10");
}

To be logically correct you might switch to Integer instead of Double values.

Sign up to request clarification or add additional context in comments.

Comments

2

You call in.hasNextDouble() several times. Each time it scans new number from input so it may cause your issue. You should also consider how you write conditions. I am aware you may just try what's happening there but this kind of condition is hard to read. You can use

(number <= 1) || (number > 10) (remove negations by inverting operators) for example.

1 Comment

thank you and i will keep in mind inverting operators
1
else if (!(in.nextDouble() > 0) || !(in.nextDouble() <= 10)) {
    Double wrongnumber = in.nextDouble();
  1. I'm not sure but here you operate on 3 different numbers. Before condition, write it to a variable.

  2. Don't compare int with double

3 Comments

just a note: actually there is no int in above code, the compiler converts it to double, or just interprets it as such (but it can be a bit confusing). Worse IMO, is the use of Double...
So, how to check if a Double variable is smaller than 10? Using x < 10 is better than x < 10.0 ?
assuming that x is double, both are the same (compiled code is exactly the same)
1

@RevCarl, According what i understand by your code and the description provided, what you want to do is to check whether the input is a number and whether its between 1 to 10. Also you have not clearly said the type of the input you are expecting. I assume it as integer, and the code below will do the task.

import java.util.Scanner;

public class Demo {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a No between 1 to 10");
        String next = input.next();
        Integer i = Integer.parseInt(next);
        if (null == i) {
            System.out.println("Input is not a number");
        } else {
            if (i > 0 && i < 10) {
                System.out.println("It works");
            } else {
                System.out.println("Not Between 1 to 10");
            }
        }
    }
}

Otherwise u can replace the statement String next = input.next(); with Integer i = input.nextInt(); which takes the integer numbers input from the console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.