Basic Java I / O Issues

I am new to java. This code I wrote checks to see if an integer is odd or even. I ran into problems when I typed a letter, the error was mainly in expecting an integer, nothing else. My question is, how can I check if the input is a real integer and nothing more than what I assign to inputChar?

thank

import java.util.Scanner;
class Oddeven {
public static void main(String[] arguments) {

  System.out.println("Type in any integer");
  Scanner inputChar = new Scanner(System.in);

  int i = inputChar.nextInt();

  if (i != 0) {
    if (i % 2 == 0) 
      System.out.println(i + " is even");
      else {
      System.out.println (i + " is odd") ;
      }
      }
   else {
   System.out.println("Zeros are not allowed, bye!");
   }
  }
 }
+3
source share
5 answers

You have to use

inputChar.hasNextInt ()

and if this function returns true, it means that the next input is an integer. If it returns false, then this input is not an integer. You can use it that way.

if(inputChar.hasNextInt())
{
 if (i != 0) {
      if (i % 2 == 0) 
      System.out.println(i + " is even");
      else {
      System.out.println (i + " is odd") ;
      }
      }
   else {
      System.out.println("Zeros are not allowed, bye!");
   }
}
else {
     System.out.println("Other than integer inputs are not allowed, bye!");
}
}
+2
source

try-catch, inputChar.nextInt(); " ", ( , ).

:

int i = 0;  
try
{
    i = inputChar.nextInt();
}
catch (InputMismatchException e)
{
    System.out.println("Not a number");
}
+1

Use hasNextInt () to check input from the scanner.

import java.util.Scanner;

class Oddeven {

    public static void main(String[] arguments) {

        System.out.println("Type in any integer");
        Scanner inputChar = new Scanner(System.in);

        int i;

        if(!inputChar.hasNextInt())
            System.out.println("Not a number");
        else{

            i = inputChar.nextInt();

            if (i != 0) {

                if (i % 2 == 0)
                    System.out.println(i + " is even");
                else {
                    System.out.println (i + " is odd") ;
                }
            } else {
                System.out.println("Zeros are not allowed, bye!");
            }
        }
    }
}
+1
source
import java.util.Scanner;
class Oddeven {
public static void main(String[] arguments) {

  System.out.println("Type in any integer");
  Scanner inputChar = new Scanner(System.in);

  while (true) {
      Integer i=0;
      Object test = inputChar.next();
      if(test instanceof Integer)
      {
          i=(Integer)test;
          if (i == 0) {
            System.out.println("Zeros are not allowed, try again!");
            continue;
          }

          System.out.println(i + " is " + ((i % 2 == 0) ? "even" : "odd"));

      }
      else{
          System.out.println("Please Enter An integer");
      }

    }
}
+1
source

Ok, this is my solution with an infinite loop that breaks when user 0 is entered or when the program ends:

import java.util.Scanner;

class Oddeven {

    public static void main(String[] arguments) {

        Scanner inputChar = new Scanner(System.in);

        int i;

        while (true) {

            System.out.println("Type in any integer:");

            if(!inputChar.hasNextInt()) {

                System.out.println("Not a number");
                inputChar.next();

            } else {

                i = inputChar.nextInt();

                if (i != 0) {

                    if (i % 2 == 0)
                        System.out.println(i + " is even");

                    else {
                        System.out.println (i + " is odd") ;
                    }

                } else {

                    System.out.println("Zeros are not allowed, bye!");
                    break;

                }
            }
        }
    }
}

Ask if you do not understand something, I hope this helps, welcomes!

+1
source

All Articles