1

I want to get the output when I enter a odd number (eg. 3) it will be give the output '3' and if I enter a even number it will print 'wrong input'. I know I can use if else but I want to use the while loop to get it. I am a beginner in programming so I would appreciate if someone can help me thanks

This are my codes right now. When I run it there is an error.

import java.util.Scanner;

public class UserInput {

public static void main(String[] args) {

    int num;
    num = readOddNum();
    System.out.println("The odd integer is" + num);

}
public static int readOddNum() {
    int num = 0;

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter an odd number: ");
    int num1 = sc.nextInt();
    sc.close();

    while ((num < 0) &&(num % 2 == 0)) {
        System.out.println("Wrong input!");

   }
    return readOddNum();
}
}
Ali
  • 3,057
  • 4
  • 33
  • 50
user
  • 11
  • 3

3 Answers3

0

You should change your code to

public static int readOddNum() {

  Scanner sc = new Scanner(System.in);
  System.out.print("Enter an odd number: ");
  int num1 = sc.nextInt();

  while ((num1 < 0) || (num1 % 2 == 0)) {
    System.out.println("Wrong input! - try again");
    num1 = sc.nextInt();

  }
  sc.close();
  return num1;


}
Scary Wombat
  • 41,782
  • 5
  • 32
  • 62
  • Don't ever close system resources like `System.in` – Flown Jul 21 '17 at 05:29
  • @Flown I hear what you are saying, but if the resource is not going to be used again in your program then see https://stackoverflow.com/questions/15613626/scanner-is-never-closed – Scary Wombat Jul 21 '17 at 05:33
  • This is exactly one of these rare occasions where you should add a comment why you did this and add a `@SuppressWarning`. – Flown Jul 21 '17 at 05:36
  • This is where a do-while-loop gets handy IMO – Obenland Jul 21 '17 at 05:47
  • @Obenland Assuming that the user can not follow easy instructions *Enter an odd number:* a do-while loop would be perfect – Scary Wombat Jul 21 '17 at 05:54
0

Change your readOddNum logic to this..

public static int readOddNum() {
    int num;

    Scanner sc = new Scanner(System.in);
    num = readNumber(sc);

    while ((num < 0) || (num % 2 == 0)) {
        System.out.println("Wrong input!");
        num = readNumber(sc);
    }

    sc.close();

    return num;
}

public static int readNumber(Scanner sc){
    System.out.print("Enter an odd number: ");
    return sc.nextInt();
}
Ritesh
  • 2,558
  • 2
  • 15
  • 38
0

The code can be rewritten in the following way

import java.util.Scanner;

public class UserInput
{

public static void main(String[] args)
{

    //int num;
    readOddNum();
    //System.out.println("The odd integer is" + num);

}
public static void readOddNum()
{
    int num = 0;

    Scanner sc = new Scanner(System.in);
    System.out.print("Enter an odd number: ");
    int num1 = sc.nextInt();
    sc.close();

    while ((num1 < 0) ||(num1 % 2 == 0))
    {
        System.out.println("Wrong input!");
    break;

   }
System.out.println("The odd integer is" + num1);

   // return readOddNum();
}
}

As you can see, I have directly called the method readOddNum(); and defined it as void instead of the earlier version. Also, notice the changes in the while loop. You were initialising num with 0 and using it in the while loop whereas the loop should run on num1 which is the actual user input, not num. Next thing is you used &&(logical AND) instead of ||(logical OR) and both obviously have different meanings. && will mean that even if user enters an even number like 2 it should also be less than 0 which isn't the case. So you need to use ||. Also, you will need to use break to stop the loop once the statement is printed, else it will keep on executing infinitely as there's no other stopping condition. Remember that if else has been created for scenarios like these where you have to choose between two or more situations and execute only one, so while loop will work but it will also execute the print statement, thereby stating wrong input and odd number. This is the output you get

C:\Java\jdk1.8.0_141\bin>java UserInput
Enter an odd number: 3
The odd integer is3

C:\Java\jdk1.8.0_141\bin>java UserInput
Enter an odd number: 2
Wrong input!
The odd integer is2

Hope you understood the concept. Appreciate it, if you like it. Thank you.

Gaurav Gilalkar
  • 120
  • 2
  • 11