Reputation: 1
I'm working on a program that needs to generate a three-digit random number and then scan each of those numbers to compare against input for a guessing game.
I did initialize instance variables I just didn't put them here. I also have other methods as well, I don't think that affects what I'm having an issue with right now.
Honestly, I'm pretty new to programming and Java so it's probably a lot less complicated than I think. But my issue is that when I create a Scanner object called randScan and try to set it to scan my secretNumber object (which is generated randomly) I get an error that says "No suitable constructor found for Scanner(int)..." and then a lot of other errors underneath it (way too much to type out). I just don't understand why it wont scan the randomNumber since it is an int.
Any help would be greatly appreciated! :)
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
// Generates three random single digit ints. The first cannot be zero
// and all three will be different. Called by public method play()
public String generateSecretNumber()
{
do
{
secretNumber = (int)(generator.nextInt(888)+101) ;
// scan the random integer
Scanner randScan = new Scanner(secretNumber) ; //<- THIS IS THE PROBLEM!
num1 = randScan.nextInt(); // scan the first digit
num2 = randScan.nextInt() ; // scan the second digit
num3 = randScan.nextInt() ; // scan the third digit
}
while ( num1 == 0 || num1 == num2 ||
num2 == num3 || num1 == num3) ; // re-generate if any digits are the same
return number ;
Upvotes: 0
Views: 1820
Reputation: 6873
You should deal secretNumber
by
String secretNumberString = new String(secretNumber);
as a String and after then You need to try Scanner#hasNextInt
According to the Doc
Returns true if the next token in this scanner's input can be
interpreted as an int value in the default radix using the nextInt() method.
The scanner does not advance past any input.
So I guess it may resolve your problem
So your code would be like
secretNumber = (int)(generator.nextInt(888)+101) ;
String secretNumberString = new String(secretNumber);
Scanner randScan = new Scanner(secretNumberString) ;
if(randScan.hasNextInt())
num1 = randScan.nextInt();
//Remaining code
Upvotes: 1
Reputation: 5373
Available constructors for scanner:
Scanner(File source)
Scanner(File source, String charsetName)
Scanner(InputStream source)
Scanner(InputStream source, String charsetName)
Scanner(Readable source)
Scanner(ReadableByteChannel source)
Scanner(ReadableByteChannel source, String charsetName)
Scanner(String source)
For single digits, you should probably pass a string with String.valueOf(yourInt)
Upvotes: 0
Reputation: 1501163
If you're just trying to get the three digits of secretNumber
(as integer values), you can just use:
num1 = secretNumber / 100;
num2 = (secretNumber / 10) % 10;
num3 = secretNumber % 10;
There's no need to convert to use a string here. On the other hand, if you don't need secretNumber
itself, surely you just need to generate three numbers between 1 and 9. The simplest way to do that is to use something like:
List<Integer> digits = new ArrayList<Integer>();
for (int i = 1; i <= 9; i++) {
digits.add(i);
}
Collections.shuffle(digits, generator);
... then use the first three values in the list:
num1 = digits.get(0);
num2 = digits.get(1);
num3 = digits.get(2);
Upvotes: 4