Solved

core java. isInteger methods. Finding bug.

Posted on 2016-08-04
10
82 Views
Last Modified: 2016-08-05
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 !
0
Comment
Question by:SunnyX
10 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 41742650
At first glance, maybe because you are advancing the scanner too early.
0
 
LVL 15

Expert Comment

by:gurpsbassi
ID: 41742654
why don'y you try setting breakpoints and debugging to find out what going on?
1
 
LVL 16

Expert Comment

by:krakatoa
ID: 41742661
Remove this line :

input.nextLine();

Open in new window

0
 
LVL 8

Assisted Solution

by:James Bilous
James Bilous earned 50 total points
ID: 41742691
I believe OP needs the nextLine() read in order to advance the scanner past the '\n' character after the integer is read. I would simply move it to the end of the while loop instead of at the beginning.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41742722
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 41742780
That code is wrong in so many ways that it's hard to know where to begin. Let's begin with the first line:

a. your code has the name of a boolean method yet it returns int. Why?
b. in the event of your replying that you misnamed the method then why do you skip a line of input (which could have contained an int)?
1
 
LVL 16

Accepted Solution

by:
krakatoa earned 400 total points
ID: 41743428
I think if you just want to get your loop working, you can go along these lines:

while (true){
		System.out.println("Input a number");
		if(!(scanner.hasNextInt())){System.out.println("Your input isn't integer.Please, try again. ");scanner.next();continue;}		
		correctNumber = scanner.nextInt();			
}

Open in new window

0
 

Author Comment

by:SunnyX
ID: 41744053
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 !
0
 

Author Closing Comment

by:SunnyX
ID: 41744055
Everybody many thanks !
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41744057
Yes, that's it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
reasons why a "inside Menu" would not appear for a web server 6 55
maven example error 3 51
Java SE 8u111  Lot of stuff broke 11 52
Apps blocked by Java 9 55
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now