1,105,546 Community Members

Beginner java problems

Member Avatar
jhamill
Light Poster
25 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Right now there are no errors indicated by Eclipse, is it only a syntax error then, i feel like im missing something.

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();
	}
	
}
Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

When you have a problem, please give us a complete description of it - full text of any error messages, or an exact description of what's wrong with the output.
Anyway... this is a GUI program, and on line 31 you get monthNumber from the field in your GUI - that's OK - but then you use a Scanner to replace that value with one from the console - that looks like a mistake.

Member Avatar
jhamill
Light Poster
25 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i want to use the jtext field to input the number of the month AND then when you press enter it will display the month...I am supposed to do this using data.setText() as i did.

Member Avatar
DarkLightning7
Junior Poster
158 posts since Jan 2011
Reputation Points: 12 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

Please describe what you are seeing that is wrong with your program. It is hard to help if we don't know what you need help with.

Member Avatar
JamesCherrill
... trying to help
10,387 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

Yes, OK, but what are lines 32 and 33 doing there - the input is in the text field, not in System.in. Just delete those 2 lines and see what happens.

Member Avatar
jhamill
Light Poster
25 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

jesus thats so painfully obvious. Thank you. It makes sense now but I figured it needed a scanner to recognize when the date was input. It works thank you!

Question Answered as of 2 Years Ago by JamesCherrill and DarkLightning7
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: