Solved

core java. isInteger methods. Finding bug.

Posted on 2016-08-04
10
73 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 7

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 45
Updating Java 9 87
allStar challenge 1 43
java set up 1 48
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now