Reputation: 25
I am trying to create a program that accepts two numbers and outputs the smallest digit in one number that is larger than the other number(for example, given 4687 and 5, the program should output 6).
The problem that I'm having is that when I compile the program, even though I'm getting no errors, after inputting the two numbers, no output is being shown. The cursor just continues blinking where it is. This is the code:
import java.io.*;
import java.util.*;
public class Numbers {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int smallest = 10;
int num;
System.out.printf("Enter a value for n: \n");
int n = in.nextInt();
System.out.printf("Enter a value for num: \n");
num = in.nextInt();
int ch = System.in.read();
while (ch>n && ch<smallest) {
smallest=ch;
ch = System.in.read();
}
System.out.printf("Smallest number that is larger is %d", smallest);
}
}
Upvotes: 0
Views: 489
Reputation: 38541
I ran your program just fine. What you are experiencing is probably just that the program is waiting on input from you that you do not expect to enter.
Here's a sample output from your program:
Enter a value for n:
100
Enter a value for num:
2
0 // <-- This value corresponds to your program prompting for int ch = System.in.read();
Smallest number that is larger is 10D
HINT: You probably want to convert the value entered for n to a String
, then use the toCharrArray()
method on String
so you can traverse each character, like what you're doing in the while
loop.
Try something like:
for(char ch : Integer.toString(n).toCharArray()) {
// rest of your while loop logic
}
Upvotes: 1