Reputation: 542
My code wont execute the way I want. I put some println
statements throughout the actionPerformed class and I think its getting stuck at the end of the foe loop because it only goes through the if statement once. There is probably something huge that I'm overlooking, can you see whats wrong?
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class Frame extends JFrame implements ActionListener{
public JPanel contentPane;
public int yesNum, noNum;
public JProgressBar progressBar;
// ######### CONFIG ##########
public int genNum = 100_000;
//Edit genNum to change number of tries
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.apple.laf.AquaLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame frame = new Frame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Frame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 195);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lab = new JLabel("Click to generate " + genNum + " times");
lab.setHorizontalAlignment(SwingConstants.CENTER);
lab.setBounds(5, 5, 289, 16);
contentPane.add(lab);
JButton b = new JButton("Generate");
b.setIcon(new ImageIcon("/Users/Colby/Desktop/checl.png"));
b.setActionCommand("Generate");
b.setBounds(80, 33, 139, 36);
contentPane.add(b);
JLabel yesLab = new JLabel("Yes Number: " + yesNum);
yesLab.setBounds(22, 81, 117, 16);
contentPane.add(yesLab);
JLabel noLab = new JLabel("No Number: " + noNum);
noLab.setBounds(22, 109, 117, 16);
contentPane.add(noLab);
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
progressBar.setBounds(5, 141, 289, 20);
progressBar.setMaximum(genNum);
contentPane.add(progressBar);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
Random rand = new Random();
int num = rand.nextInt(1);
int yesCounter = 0;
int noCounter = 0;
for(int counter = 1; counter < 100_000;){
if(num == 0){
System.out.println("testing yes");
yesCounter++;
System.out.println(counter+ ": Yes");
progressBar.setValue(counter);
} else if(num == 1){
System.out.println("testing no");
noCounter++;
System.out.println(counter+ ": No");
progressBar.setValue(counter);
}
num = rand.nextInt();
}
yesCounter = yesNum;
noCounter = noNum;
}
}
Upvotes: 0
Views: 147
Reputation: 56
Also, int num = rand.nextInt(1);
always returns zero. Because the Java documentation says:
public int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
So, you need to put int num = rand.nextInt(2);
to get values between 0 and 1.
(also you need to check the line num = rand.nextInt();
inside the for loop, because without the int n
parameter, the return values are not constrained to 0 and 1)
Upvotes: 0
Reputation: 5055
The problem is in this line
for(int counter = 1; counter < 100_100;)
You're not increasing the counter.
for(int counter = 1; counter < 100_100; counter++){
//
}
Upvotes: 4
Reputation: 3928
Change
for(int counter = 1; counter < 100_000;)
to
for(int counter = 1; counter < 100;counter++)
Upvotes: 2
Reputation: 133577
for(int counter = 1; counter < 100_000; )
^^^
You are missing the iteration step, you should increment counter
there (++counter
).
In addition a long blocking task like yours shouldn't be executed in the EDT, it should have it's own Thread
, since you are working with a gui.
Upvotes: 7