|
-
November 5th, 2011, 12:26 PM
#1
Beginner Programming Problem
Hey so I've been working on a lab for my intro to software developing class and have been trying to get my program to work for several days now. It's a real basic program, im sure someone will be able to figure out the issue im having. Hopefully once I learn what I'm doing I'll be able to return the favor, but until then thanks, heres the code.
There are no errors indicated by eclipse but i want to use the jtextfield to input the number of the month AND then when you press enter i want the same textfield to display the month...I am supposed to do this using data.setText() as i did, but the problem happens after i enter the month number....the program freezes heres my code
package lab8;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class GUIMonthNames extends JFrame implements ActionListener {
private JLabel instructions;
private JTextField data;
public GUIMonthNames() {
super("Month Names: GUI version");
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container myPane = getContentPane();
myPane.setLayout(new FlowLayout());
instructions = new JLabel("Enter a number (1-12) below");
data = new JTextField(20);
add(data);
data.addActionListener(this);
add(instructions);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
int monthNumber = new Integer(data.getText());
Scanner keyboard = new Scanner(System.in);
monthNumber = keyboard.nextInt();
final String MONTH_TABLE = "January February March April May June " +
"July August SeptemberOctober November December ";
int start = (monthNumber - 1) * 9;
int stop = start + 9;
String monthName = MONTH_TABLE.substring(start, stop);
data.setText(monthNumber + " is '" + monthName.trim() + "'.");
}
public static void main(String[] args) {
new GUIMonthNames();
}
}
-
November 9th, 2011, 11:39 AM
#2
Re: Beginner Programming Problem
Your problem is you are trying to get keyboard input from the console, just remove the scanner lines ie these 2 lines and it will work.
Code:
Scanner keyboard = new Scanner(System.in);
monthNumber = keyboard.nextInt();
BTW why are you trying to extract the month names from a long string of names, why not use put the names into an array and use the entered month number (minus 1) as an index into the array.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|