Avatar of SunnyX
SunnyX

asked on 

core java. isInteger methods. Finding bug.

public static int isInputInteger(){
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);   

		int correctNumber;
		
			while(true){   
			input.nextLine();
				    if(input.hasNextInt()){   
				    	correctNumber = input.nextInt();
				        break;
				    }
				    else{
				    	System.out.println("Your input isn't integer.Please, try again. ");
				    }
			}
			return correctNumber;
			}

Open in new window


class PrintFromInput {

	protected static int  printNumber() {
    	System.out.printf("Please, enter integer: ");
    	int intYourConsoleInput = Assert.isInputInteger();
         return intYourConsoleInput; 
    }
}

public class Task {
    public static void main(String[] args) {
    	
    	System.out.println("Congratulation! Your correct integer input is: "+ PrintFromInput.printNumber()); 
    	
    }
}

Open in new window


Inside class Assert there is a method "isInputInteger" everything work but not as I expect :
Session 1
input : f
output: {nothing }
input : f
output : Your input isn't integer.Please, try again.
input: g
output : Your input isn't integer.Please, try again.
input : 3
Congratulation! Your correct integer input is: 3
Session OVER
My question is why there isn't any message after first "f". Please, help me to find bug. Thx in advance !
JavaJava EEAlgorithms

Avatar of undefined
Last Comment
krakatoa
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

At first glance, maybe because you are advancing the scanner too early.
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

why don'y you try setting breakpoints and debugging to find out what going on?
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Remove this line :

input.nextLine();

Open in new window

SOLUTION
Avatar of James Bilous
James Bilous
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes, indeed.  I thought the OP would be able to work out the required code move once he sees the result of just deleting it.
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of SunnyX
SunnyX

ASKER

The solution is :
public static int onlyIntegerInput(){
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);   

		int correctNumber;
		
		while (true){

			if(!(scanner.hasNextInt())){
				System.out.println("Your input isn't integer. Please, try again. ");
				scanner.next();
				continue;
				}
			else{
			correctNumber = scanner.nextInt();
			break;
			}
		}
			return correctNumber;
	}

Open in new window



Dear experts thx you so much for your help !
Avatar of SunnyX
SunnyX

ASKER

Everybody many thanks !
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes, that's it.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo