Reputation: 17
this is basically my first java program except for Hello world. I've tried getting out as much errors as possible for the last hours, but I keep getting new ones. I try to Google solutions but in the end they end up giving me new errors, but I guess that's how u learn ^^
I'm stuck though, I get the following error: https://i.sstatic.net/mUUsb.png
I've tried a couple of different ways to achieve my goal, which is selecting a random string from the list lijstKaarten. The whole goal of the program is that u pick a card by inputting which type (Hearts, clubs, diamonds, spades) and a card number. The computer has to also pick a card, my exercise tells me to use a default card but I'd rather generate a random type and number to show the computer's choice.
Here's my code:
package userinput;
import javax.swing.JOptionPane;
import java.*;
import java.io.*;
import java.util.*;
public class Thuisopdracht {
public static void main(String[] args) throws InterruptedException {
List<String> lijstKaarten = new LinkedList<String>();
lijstKaarten.add("Harten");
lijstKaarten.add("Ruitens");
lijstKaarten.add("Klaveren");
lijstKaarten.add("Schuppen");
Random rand = new Random();
while (true) {
int computerKeuze = rand.nextInt(lijstKaarten.size());
String stringcomputerKeuze;
stringcomputerKeuze = lijstKaarten.get(computerKeuze);
}
double computerNummer = (Math.random() * (13 - 1)) + 1;
String typeKaart;
typeKaart = JOptionPane.showInputDialog("Welk type kaart kies je?");
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
System.out.println("Welke kaartnummer kiest u? (1-13)");
String kaartNummer = bufRead.readLine();
int intKaartNummer = Integer.parseInt(kaartNummer);
System.out.println("De computer koos " + stringcomputerKeuze + " " + computerNummer);
System.out.println("Jij koos " + typeKaart + " " + kaartNummer);
}
}
Upvotes: 0
Views: 758
Reputation: 93872
The variable String stringcomputerKeuze;
is not initialized and declared inside the while loop. You cannot access it from the println you did. Initialize it and declare it outside the loop.
Random rand = new Random();
String stringcomputerKeuze = "";
while (true) {
int computerKeuze = rand.nextInt(lijstKaarten.size());
stringcomputerKeuze = lijstKaarten.get(computerKeuze);
}
You need also a stop condition in your loop cause it will run indefinitely. You can add a counter per example :
String stringcomputerKeuze = "";
int counter = 0;
while (counter != 5) {
int computerKeuze = rand.nextInt(lijstKaarten.size());
stringcomputerKeuze = lijstKaarten.get(computerKeuze);
counter++;
}
Upvotes: 1
Reputation: 1270
A variable is only visible within the brackets it was declared in.
So, in the following code:
while (true) {
int computerKeuze = rand.nextInt(lijstKaarten.size());
String stringcomputerKeuze;
stringcomputerKeuze = lijstKaarten.get(computerKeuze);
}
stringcomputerKeuze
only exists within the brackets of the while, when you refer to it later, Java has no idea what you're talking about.
Move the declaration outside of the brackets.
Upvotes: 0
Reputation: 122018
With in the method you have to initialize the variables,before using them
you have not initialized
String stringcomputerKeuze;
try to initialize like
String stringcomputerKeuze =null;
or
String stringcomputerKeuze ="";
Upvotes: 0