Note that there are some explanatory texts on larger screens.

plurals
  1. POfor loop for array only processing one element in java?
    primarykey
    data
    text
    <p>I can't figure out why whenever I cycle through my array using the for-loop it only produces one element (the first) to console? I'm pretty sure it's a rookie-mistake I'm looking over, so any tips and suggestions would help. </p> <p>I'm making a program for fun that compares two strings typed in a text field and if they don't exist in the array it produces a JOPtionPane message on the contrary. It's for a battle-hack I may produce in the future for vBulletin forum, but I'm messing around with algorithms before I move to that step. Thanks, guys!</p> <pre><code>package battleoptionspart1; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.lang.*; import javax.swing.border.*; public class BattleOptionsPart1 extends JFrame{ JButton newthread, previewpost; JRadioButton battle1; JTextField postcount, oppA, oppB; JLabel battle2, max; JPanel panel; String [] array = {"Bill","Tom","Wendy", "Paula"}; public BattleOptionsPart1 () { panel = new JPanel(); Toolkit tool = Toolkit.getDefaultToolkit(); Dimension dim = tool.getScreenSize(); this.setSize(500, 500); this.setTitle("Battle Options"); GridLayout grid = new GridLayout(0,1,2,2); this.setLayout(grid); newthread = new JButton("Post New Thread"); previewpost = new JButton("Preview Post"); postcount = new JTextField("", 4); oppA = new JTextField("",10); oppB = new JTextField("",10); battle1 = new JRadioButton(); battle2 = new JLabel("Would you like to start a recorded battle?"); max = new JLabel("Enter max post count user must have to vote"); ListenForButton listen = new ListenForButton(); newthread.addActionListener(listen); previewpost.addActionListener(listen); JPanel opponents = new JPanel(); Border oppBorder = BorderFactory.createTitledBorder("Battlers"); opponents.setBorder(oppBorder); opponents.add(oppA); opponents.add(oppB); JPanel battle = new JPanel(); Border battleBorder = BorderFactory.createTitledBorder("Start Battle"); battle.setBorder(battleBorder); battle.add(battle1); battle.add(battle2); JPanel buttons = new JPanel(); Border buttonBorder = BorderFactory.createTitledBorder("Create Thread"); buttons.setBorder(buttonBorder); buttons.add(newthread); buttons.add(previewpost); JPanel restriction = new JPanel(); Border resBorder = BorderFactory.createTitledBorder("Restrictions"); restriction.setBorder(buttonBorder); restriction.add(postcount); restriction.add(max); this.add(opponents); this.add(battle); this.add(restriction); this.add(buttons); this.add(panel); int xPos = (dim.width / 2) - (this.getWidth() / 2); int yPos = (dim.height / 2) - (this.getHeight() / 2); this.setLocation(xPos,yPos); //places form in the middle this.setVisible(true); // users can see form this.setResizable(false); //users can't resize the form this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private class ListenForButton implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String compareA = oppA.getText(); String compareB = oppB.getText(); if (e.getSource() == newthread) { System.out.println(compareA + "\n" + compareB); for(int j = 0; j &lt; array.length; j++) { System.out.println(array[j]); if(!compareA.equals(array[j])) { JOptionPane.showMessageDialog(null, compareA + " doesn't exist!", "Error Message", JOptionPane.ERROR_MESSAGE); oppA.requestFocus(); break; } if (!compareB.equals(array[j])) { JOptionPane.showMessageDialog(null, compareB + " doesn't exist!", "Error Message", JOptionPane.ERROR_MESSAGE); oppB.requestFocus(); break; } else { JOptionPane.showMessageDialog(null, "New thread created successfully!", "Success", JOptionPane.INFORMATION_MESSAGE); break; } } } else if (e.getSource() == previewpost) { System.exit(0); } } } public static void main(String[] args) { BattleOptionsPart1 battle = new BattleOptionsPart1(); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload