View Single Post
  #2 (permalink)  
Old 05-08-2008, 10:31 PM
hardwired hardwired is online now
Senior Member
 
Join Date: Jul 2007
Posts: 1,027
hardwired is on a distinguished road
This blocks input at the console
Code:
int arraySize = console.nextInt();
Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class AnArrayTest { Scanner console = new Scanner(System.in); JFrame frame; public AnArrayTest() { frame = new JFrame("Array"); JButton button = new JButton("Enter Here"); button.addActionListener(new MyAction()); JPanel panel = new JPanel(); panel.add(button); frame.add(panel); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public class MyAction implements ActionListener{ public void actionPerformed(ActionEvent e){ String input = JOptionPane.showInputDialog(null, "Enter the size of the array: "); int arraySize = //console.nextInt(); Integer.parseInt(input.trim()); int[] list = new int[arraySize]; for (int arrayIndex = 0; arrayIndex < list.length; arrayIndex++) { list[arrayIndex] = arraySize - arrayIndex; } String values = Arrays.toString(list); JOptionPane.showMessageDialog(frame, values + " " + "is your number", "Your Silly Numbers", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] args){ new AnArrayTest(); } }
Reply With Quote