Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a static method named getOddInteger that accepts as its parameter a Scanne

ID: 3760301 • Letter: W

Question

 Write a static method named getOddInteger that accepts as its parameter a Scanner for the console and repeatedly prompts the user until an odd integer (an integer not evenly divisible by 2) is entered, then returns this integer. If the user enters an integer that is even, or a token that is not an integer, an error message is displayed and the user is prompted again. You may assume that the user will type a single token as their response each time.  For example, the following log of execution represents the output from one call to your method. (User input is underlined.) After this output, the method returns 17, because this is the first odd integer typed by the user.  Type an odd integer: hello Invalid input, try again. Type an odd integer: okay Invalid input, try again. Type an odd integer: 268 Invalid input, try again. Type an odd integer: 1.875 Invalid input, try again. Type an odd integer: -24 Invalid input, try again. Type an odd integer: 17  The following is another example log of execution in which the user types an odd integer on the first try. After this output, the method returns 285.  Type an odd integer: 285 

Explanation / Answer

I have the below java solution it is only handle integers not strings and decimal numbers..

import java.util.Scanner;

class ScannerTest{
public static void main(String args[]){   
   Scanner sc=new Scanner(System.in);
   int number, quit;
   System.out.println("Enter your Number");
   do{

       number = sc.nextInt();
       if (number%2 != 0)
           {
               System.out.println("Odd Number is: "+ number);
               quit = 1;
           }
       else
       {
           System.out.println("Invalid input, try again.");
           //number = sc.nextInt();       
           quit = 0;          
       }
      
      
   } while(quit != 1);

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote