We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,018 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Beginner java problems

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();
	}
	
}
3
Contributors
5
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
jhamill
Light Poster
25 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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.

jhamill
Light Poster
25 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

DarkLightning7
Junior Poster
111 posts since Jan 2011
Reputation Points: 15
Solved Threads: 9
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,516 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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!

jhamill
Light Poster
25 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by JamesCherrill and DarkLightning7

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0801 seconds using 2.72MB