Java JLabel array remove element after use - java

So I have a JFrame in Netbeans that holds 20 labels of math equations.
mathLabel1 is "2 + 2"
mathLabel2 is "4 * 4"
etc...
and if mathLabel1 is shown and the user guesses the right answer (4) then I want to setVisible(false) and remove that element from my array, so it doesn't come up as a question again.
Basically, no repeats.
Here is a short version of my code:
//declare variables
String strUserAnswer;
int i;
Random r = new Random();
int randvalue = r.nextInt(19);
JLabel[] math = {mathLabel1, mathLabel2, mathLabel3, mathLabel4, mathLabel5, mathLabel6, mathLabel7, mathLabel8, mathLabel9, mathLabel10,
mathLabel11, mathLabel12, mathLabel13, mathLabel14, mathLabel15, mathLabel16, mathLabel17, mathLabel18, mathLabel19, mathLabel20};
JLabel test;
//method that chooses random math equation
public void random(JLabel test) {
r = new Random();
randvalue = r.nextInt(19);
test = math[randvalue];
if (test == math[0]) {
mathLabel1.setVisible(true);
}
else if (test == math[1]){
mathLabel2.setVisible (true);
}
else if (test == math[2]){
mathLabel3.setVisible (true);
}
else if (test == math[3]){
mathLabel4.setVisible (true);
}
else if (test == math[4]){
mathLabel5.setVisible (true);
}
else if (test == math[5]){
mathLabel6.setVisible (true);
}
else if (test == math[6]){
mathLabel7.setVisible (true);
}
else if (test == math[7]){
mathLabel8.setVisible (true);
}
else if (test == math[8]){
mathLabel9.setVisible (true);
}
else if (test == math[9]){
mathLabel10.setVisible (true);
}
else if (test == math[10]){
mathLabel11.setVisible (true);
}
else if (test == math[11]){
mathLabel12.setVisible (true);
}
else if (test == math[12]){
mathLabel13.setVisible (true);
}
else if (test == math[13]){
mathLabel14.setVisible (true);
}
else if (test == math[14]){
mathLabel15.setVisible (true);
}
else if (test == math[15]){
mathLabel16.setVisible (true);
}
else if (test == math[16]){
mathLabel17.setVisible (true);
}
else if (test == math[17]){
mathLabel18.setVisible (true);
}
else if (test == math[18]){
mathLabel19.setVisible (true);
}
else if (test == math[19]){
mathLabel20.setVisible (true);
}
}
private void guessButtonActionPerformed(java.awt.event.ActionEvent evt) {
// User clicks guess to enter answer, if correct part of puzzle appears
strUserAnswer = answerText.getText();
test = math[randvalue];
//if the math equation chosen is 2+2...
if (test == math[0]) {
//show math equation
mathLabel1.setVisible(true);
//if answer is right...
if (strUserAnswer.equals("4")) {
JOptionPane.showMessageDialog(null, "Yay!! That is right!");
//show puzzle piece, hide equation, and choose a new one
label1.setVisible(true);
mathLabel1.setVisible(false);
//test.remove(math[0]);
test = math[randvalue];
answerText.setText(null);
random(test);
//if answer is wrong...
} else {
JOptionPane.showMessageDialog(null, " Sorry, try again!");
answerText.setRequestFocusEnabled(true);
}
}
and that's repeated for math[1], math[2], math[3], etc...
So how would I do this? I tried the remove() method but that was a shot in the dark...

ok, so this might make your day or break your heart, but you are doing way more than you need to with your random() method. first off, it doesn't seem like you need to take in a parameter
because it looks like you are manually changing the value before you ever use it. also, because each value in the array is in fact a JLabel, you can just say math[randValue].setVisible(true) instead of going through the whole if statement thing. and to solve your problem of removing stuff, there is a quick and dirty way you can do it that i will show you, but you are better off using an ArrayList instead of an Array.
public void random() {
Random r = new Random();
randValue = r.nextInt(math.length); //make sure the index is always within the array
JLabel[] temp = new JLabel[math.length - 1]; //this will do the trick
math[randValue].setVisible(true);
for (int i = 0; i < randvalue; i++) {
temp[i] = math[i]; //fill the new array up to the chosen label
}
for (int i = randValue; i < temp.length; i++) {
temp[i] = math[i + 1]; //fill the rest, omitting the chosen label
}
math = new JLabel[temp.length]; //math is now shorter
math = temp; //put everything back in the original array
}
this should work as a solution using arrays.
hope it helps.

If your data structure will constantly be changing, try using a List instead of an Array:
List<JLabel> labels = new ArrayList<JLabel>();
int numLabels = 20;
for (int i = 0; i < numLabels; i++) {
labels.add(new JLabel(i + " " + i));
}
From there you could always call:
labels.get(4).setVisible(false);
or
labels.remove(4);
And then revalidate your JPanel.
EDIT 2:
I may have misunderstood your question - it seems you want to remove a number and never create a label for it again. This is the correct way to do it:
int numIntegers = 20;
Set<Integer> possibleNumbers = new HashSet<Integer>();
for (int i = 0; i < numIntegers; i++) {
possibleNumbers.add(i);
}
When you want to remove an item, use:
possibleNumbers.remove(14);
Then when you want to present this data, you can use:
panel.clear();
for (Integer number : possibleNumbers) {
panel.add(new JLabel(number + " " + number));
}
(Please note I was incorrect calling JLabels data - they are part of the presentation.)

Related

How would i use an Abstract Class or Interface in my code to minimise duplication?

I am currently trying to refactor my code for a basic connect four game.
I have noticed that I have a lot of repetition in my play.java file, with regards to knowing which player creates a winning pattern either horizontally or vertically.
As the horizontal and vertical winning criteria remains constant, how would I go about creating an Abstract Class or Interface to minimise code duplication here?
play.java
public class play {
private Connect4Game connect;
public play(Connect4Game connect) {
this.connect=connect;
}
public void playGame() {
System.out.println("Welcome to Connect 4");
System.out.println("To play the game type in the number of the column you want to drop you counter in");
System.out.println("Player One = r Player 2 = y");
System.out.println("");
board boardObj = new board(connect);
boardObj.printBoard();
boolean win = false;
while(!win){
// player 1
String userInput = getUserInput();
int move = Integer.parseInt(userInput);
counter counterObj = new counter(connect);
counterObj.placeCounter('r', move);
boolean hasWon = false;
int count = 0;
// check horizontal
for(int i=0; i<connect.board.length; i++){
for(int j=0; j<connect.board[i].length; j++){
if(connect.board[i][j] == 'r'){
count = count + 1;
if(count == 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
// check vertical
count = 0;
for(int i=0; i<connect.board[0].length; i++){
for(int j=0; j<connect.board.length; j++){
if(connect.board[j][i] == 'r'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
boardObj.printBoard();
if(hasWon){
win = true;
System.out.println("You Have Won!!!");
}
else {
//player 2
userInput = getUserInput();
move = Integer.parseInt(userInput);
counterObj.placeCounter('y',move);
hasWon = false;
count = 0;
// check horizontal
for(int i=0; i<connect.board.length; i++){
for(int j=0; j<connect.board[i].length; j++){
if(connect.board[i][j] == 'y'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
// check vertical
count = 0;
for(int i=0; i<connect.board[0].length; i++){
for(int j=0; j<connect.board.length; j++){
if(connect.board[j][i] == 'y'){
count = count + 1;
if(count >= 4){
hasWon = true;
}
}
else{
count = 0;
}
}
}
boardObj.printBoard();
if(hasWon){
win = true;
System.out.println("You Have Won!!!");
}
}
}
}
public String getUserInput(){
String toReturn = null;
try{
toReturn = connect.input.readLine();
}
catch(Exception e){
}
return toReturn;
}
}
Neither would help in this instance. An abstract class would be used to establish the predetermined methods that would be supported by most subclasses. It would also include method signatures of methods that would be useful where implementations might be different, depending on how that class is be used. Even then, the predefined methods can be overridden as required.
An interface simply formalizes the required methods via signatures (abstract method declarations) for a class to implement to enforce a contract between the user and the class.
As you are not creating multiple classes there would be no benefit.
The best I could recommend would be to consolidate your horizontal and vertical checks into methods and to add other methods as needed to reduce duplicate code.
Start refactoring by using extract method. Your two sets of nested loops can be turned into a method. Maybe you can get it down to one set of nested loops on a method that you call four times.

Retrieving status of JCheckbox inside a JTable

For some reason, i cannot find out what is wrong with my code. Basically
I have a counter (total) initialized to 2 in this case
Increment to a max of 6, each time a JCheckbox is checked (in this case 4 )and throws a message when too much is clicked.
At first, it throws null pointer when exactly a total of 6 is checked; but works fine if more than 6 checkboxes are selected, then reduced to 6 with more than one button click.
Can anyone guide me to where it is messing with my head? Thank you very much.
public void panelThree(JTable user, JButton save, JButton logout, JButton exit, int j) {
System.out.println("J in external main is: " + j);
save.addActionListener(new ActionListener() {
boolean arr[] = new boolean[user.getRowCount()];
public void actionPerformed(ActionEvent e) {
int total = j;
System.out.println("Total in listener is: "+total);
System.out.println("No of rows: " + user.getRowCount());
for (int z = 0; z < arr.length; z++) {
arr[z] = false;
}
for (int i = 0; i < user.getRowCount(); i++) {
if (Boolean.valueOf(user.getModel().getValueAt(i, 3).toString()) == true) {
if (arr[i] == false) {
arr[i] = true;
total++;
}
} else {
if (arr[i] == true) {
arr[i] = false;
total--;
}
}
}
System.out.println("Total is: " + total);
if (total > 6) {
JOptionPane.showMessageDialog(null, "Please choose up to a maximum of " + (6 - j) + " modules");
} else {
int reply = JOptionPane.showConfirmDialog(null, "Are you sure to enroll in these modules?", "Yes", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Enrollment Successful");
}
}
}
});
}
The line if (Boolean.valueOf(user.getModel().getValueAt(i, 3).toString()) == true) seems to be responsible for all the error all the edit i am making console says null pointer at that line
Your table seems to contain null values. So you need to consider it in your check. Something like this:
if (user.getModel().getValueAt(i, 3) == Boolean.TRUE)
If my suggestion don't work please provide a SSCCE so we can also reproduce your problem.

How can I avoid this error with while JAVA Android

I'm designing an app, what it does is a kind of "secret friend", I tell the app obviously not to repeat the person playing with the selected person, see this:
if (lista2.isEmpty())
{
(Toast.makeText(getApplicationContext(), this.getString(R.string.nospace), Toast.LENGTH_LONG)).show();
return;
} else {
Random random = new Random();
int start2;
do
{
start2 = random.nextInt(lista2.size() - 0) + 0;
}
while (lista2.get(start2).equals(spinner1.getSelectedItem().toString()));
}
At the while, sometimes the app crash, because there's no more items on lista2, just the same person(spinner1.selectedItem), how can I solve this?
What you should do is to
first remove the person(spinner1.selectedItem from lista2
Then do a random get
This way, you need not use the do-while loop
just do a check first
if (lista2.isEmpty())
{
(Toast.makeText(getApplicationContext(), this.getString(R.string.nospace), Toast.LENGTH_LONG)).show();
return;
} else {
//do a check here
for(int i = 0; i < lista2.size(); i++){
if(!lista2.get(start2).equals(spinner1.getSelectedItem().toString())){
break;
}
if(i == lista2.size() - 1){
Toast.makeText(getApplicationContext(), this.getString(R.string.nospace), Toast.LENGTH_LONG)).show();
return;
}
}
Random random = new Random();
int start2;
do
{
start2 = random.nextInt(lista2.size() - 0) + 0;
}
while (lista2.get(start2).equals(spinner1.getSelectedItem().toString()));
}

NumberFormatException and other problems while making a calculator in java

So I'm making a Java Calculator and walking into some errors upon clicking the function buttons (it compiles perfectly fine though).
The error on the 1st line in my CMD was a NumberFormat Exception. After some searching I figured out that somewhere in my code I was trying to get a double out of an empty string (basically what I'm trying to do is parse a double from the label text). I'm guessing it's this part:
// if statement that puts the labels text into the first or second number
if(firstNumber)
{
number1 = Double.parseDouble(label.getText().trim());
} else {
number2 = Double.parseDouble(label.getText().trim());
}
Basically the problems are when i push the / * + or - button i get a NumberFormatExeption: For input string "/" etc.
I can't quite figure out how i have to fix this error (I'm still (somewhat) a beginner in Java).
Further down the line of errors (there were quite alot) were a whole lot of errors I didn't understand such as EventDispatchThread, EventQueue and many others. I couldn't find an explanation on my level of experience either so I'm asking for help here.
The numeric buttons all work fine.
At run: [http://gyazo.com/71cb4dde449ccf7ece44017388a71a0f]
Putting in numbers: [http://gyazo.com/5c7ab6c54ac6da180845c66866d66f8f]
All other buttons give errors in my CMD.
Here's my code (the spacing might be messed up in some parts):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
// import for Nimbus look
import javax.swing.UIManager.*;
public class Calculator extends JFrame implements ActionListener
{
private JPanel bottom = new JPanel(); private JPanel top = new JPanel();
private JLabel label = new JLabel(" ");
private JButton[] buttons = new JButton[16];
// booleans for calculator functions
boolean add = false, substract = false, devide = false, multiply = false, firstNumber = true;
// numbers that will be calculated
double number1, number2;
public Calculator()
{
setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
// bottom panel
bottom.setPreferredSize(new Dimension(100,100));
bottom.setLayout(new BorderLayout());
//add bottom panel to frame
add(bottom);
// top panel
top.setPreferredSize(new Dimension(300,400));
top.setLayout(new GridLayout(4,4,3,3));
top.setBackground(Color.BLACK);
//dont add top panel to frame: you want top to be on bottom
// add top panel to bottom panel
bottom.add(top);
// label
label.setFont(new Font("Courier", Font.PLAIN, 20));
label.setBackground(Color.BLACK);
label.setForeground(Color.WHITE);
label.setHorizontalAlignment(SwingConstants.RIGHT); // text is right-aligned
label.setOpaque(true);
// add the label to the bottom panel
bottom.add(label, BorderLayout.NORTH);
// creating buttons
for(int i = 0; i < buttons.length; i++)
{
buttons[i] = new JButton("789/456*123+c0=-".substring(i, i+1));
buttons[i].addActionListener(this);
// add them to the top panel
top.add(buttons[i]);
}
// Nimbus look
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
//set to default somehow o.o
}
// frame setters
setTitle("Calculator");
setSize(400,400);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void Clear()
{
label.setText(" ");
}
#Override
public void actionPerformed(ActionEvent e)
{
//if's for all function buttons
if(e.getSource() == buttons[3])
{
// devide
devide = true;
substract = false;
add = false;
multiply = false;
firstNumber = false;
Clear();
label.setText("/");
}
if(e.getSource() == buttons[7])
{
// multiply
multiply = true;
substract = false;
devide = false;
add = false;
firstNumber = false;
Clear();
label.setText("*");
}
if(e.getSource() == buttons[11])
{
// add
add = true;
substract = false;
devide = false;
multiply = false;
firstNumber = false;
Clear();
label.setText("+");
}
if(e.getSource() == buttons[12])
{
// clear
label.setText("0");
number1 = 0.00;
number2 = 0.00;
add = false;
substract = false;
devide = false;
multiply = false;
firstNumber = true;
Clear();
}
if(e.getSource() == buttons[15])
{
// substract
substract = true;
add = false;
devide = false;
multiply = false;
firstNumber = false;
Clear();
label.setText("-");
}
// for loops that add the numbers on the buttons to the label
for(int i = 0; i < 3; i++)
{
if(e.getSource() == buttons[i])
{
if(label.getText() == "0")
{
label.setText("");
label.setText(label.getText() + buttons[i].getText());
} else {
label.setText(label.getText() + buttons[i].getText());
}
}
}
for(int i = 4; i < 7; i++)
{
if(e.getSource() == buttons[i])
{
if(label.getText() == "0")
{
label.setText("");
label.setText(label.getText() + buttons[i].getText());
} else {
label.setText(label.getText() + buttons[i].getText());
}
}
}
for(int i = 8; i < 11; i++)
{
if(e.getSource() == buttons[i])
{
if(label.getText() == "0")
{
label.setText("");
label.setText(label.getText() + buttons[i].getText());
} else {
label.setText(label.getText() + buttons[i].getText());
}
}
}
for(int i = 13; i < 14; i++)
{
if(e.getSource() == buttons[i])
{
if(label.getText() == "0")
{
label.setText("");
label.setText(label.getText() + buttons[i].getText());
} else {
label.setText(label.getText() + buttons[i].getText());
}
}
}
// if statement that puts the labels text into the first or second number
if(firstNumber)
{
number1 = Double.parseDouble(label.getText().trim());
} else {
number2 = Double.parseDouble(label.getText().trim());
}
// calculation
if(e.getSource() == buttons[14])
{
// calculate
if(devide){number1 = ((double)(number1) / (double)(number2)); }
if(multiply){number1 = ((double)(number1) * (double)(number2)); }
if(add){number1 = ((double)(number1) + (double)(number2)); }
if(substract){number1 = ((double)(number1) - (double)(number2)); }
label.setText(Double.toString(number1));
}
}
public static void main(String[] args)
{
new Calculator();
}
}
Lastly, the calculations made by the calculator are incorrect. I also can't wrap my head around what's causing that. Please bear in mind that I'm a beginner at Java and this is my first question on stackoverflow. Thanks in advance for helping me, whoever will :)
UPDATE: i fixed the errors by putting my code as follows:
if(e.getSource() == buttons[15])
{
// substract
substract = true;
add = false;
devide = false;
multiply = false;
firstNumber = false;
isNumberKey = false;
if(isNumberKey)
{
if(firstNumber)
{
label.setText(label.getText().replace("/",""));
label.setText(label.getText().replace("*",""));
label.setText(label.getText().replace("+",""));
label.setText(label.getText().replace("-",""));
number1 = Double.parseDouble(label.getText().trim());
} else {
label.setText(label.getText().replace("/",""));
label.setText(label.getText().replace("*",""));
label.setText(label.getText().replace("+",""));
label.setText(label.getText().replace("-",""));
number2 = Double.parseDouble(label.getText().trim());
}
}
Clear();
label.setText("-");
}
All i need to do now is fix the calculations...
Thanks for the help everyone!
Cannot delve totaly into your logic there, but here's some hints:
First, you don't have to guess where the error is. In case of uncaught exception like here, thread that produced it will print it's stack trace to console. It looks like this:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "/"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
at java.lang.Double.parseDouble(Double.java:510)
at mypackage.Calculator.actionPerformed(Calculator.java:229)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6297)
From here you see that exception happened in thread named AWT-EventQueue-0, which is swing's standard event dispatching thread (all GUI applications handles events in single thread). Another thing you search in stack trace is to find what line in your code thrown it. Here it's the line:
at mypackage.Calculator.actionPerformed(Calculator.java:229)
In source, it is line:
number2 = Double.parseDouble(label.getText().trim());
, so you guessed right. What you can do is add another boolean there which will be set only if number button is hit, and then change that part of code to something like:
if( isNumber){
if(firstNumber)
{
number1 = Double.parseDouble(label.getText().trim());
} else {
number2 = Double.parseDouble(label.getText().trim());
}
}
Apart from that, instead of using multiple boolean flags for operation, use Java enum type
enum Operation {devide, substract, add, multiply};
your code will be more readable and "java" styled.
Good starting point, just go ahead!
Disclaimer: I'm on my phone so it's hard to parse your code; with that being said, I believe your number format exception deals with what is stored in the label text.
From the looks of it if you hit an operator button (like multiply), the labelText will be "*", then if you hit number button (like 5), the labelText will be "*5". Finally, if you hit the enter button now to go calculate the answer, Double.parseDouble(label.getText().trim()), will be ran which will throw the NumberFormatException. Using the operation flags, it looks like you don't even need to store the operator (unless you're trying to display the operator to the user); in that case you need to strip the operator from the string before trying the parse the double.
label.setText(label.getText().replace("/",""));
label.setText(label.getText().replace("*",""));
label.setText(label.getText().replace("+",""));
label.setText(label.getText().replace("-",""));
As for the other exceptions, in eclipse you can set a breakpoint to halt the program when a specific exception occurs. This will help you debug your code further.
more info on that here

creating a team of players from a list

Hey im trying to create a random way to pick a team of 4 from a linkedlist and am wondering if this code will work.
heres an example code
public static void enterGame(Client c) {
int n = teamSize;
boolean startNewGame = false;
if(waitingPlayers.size() <= 3) {
return; // not enough players
}
startNewGame = true;
if(startNewGame) {
System.out.println("Starting new game");
for(int i=0; i<n; i++) {
Collections.shuffle(waitingPlayers);
System.out.println("Picking random player");
waitingPlayers.remove(c);
System.out.println("removing from random player list");
players.add(c);
System.out.println("adding player to ingame list");
}
}
}
I would use Collections.shuffle() and a sublist. In order to know if your code works why don't you test it (unit test or just some try)?
Also, this part:
boolean startNewGame = false;
if(waitingPlayers.size() > 3) {
startNewGame = true;
} else {
startNewGame = false;
return;
}
if(startNewGame) {
is over complicated, I would replace it by:
if(waitingPlayers.size() <= 3) {
return; // not enough players
}
Full code:
public static void enterGame(final Client c) {
if(waitingPlayers.size() <= 3) {
return; // not enough players
}
System.out.println("Starting new game");
Collections.shuffle(waitingPlayers);
System.out.println("Picking random players");
// ? to be replaced by the real type of objects inside waitingPlayers
final List<?> picked = waitingPlayers.subList(0, 3);
players.addAll(picked);
waitingPlayers.removeAll(picked);
}
You have a bug - you are picking a number from 0 to n. If n is selected, you will get an out of bounds exception. Other fhan that it seems fine.

Categories