CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2011
    Location
    Washington, DC
    Posts
    0

    Question 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();
    }

    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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
  •  





Click Here to Expand Forum to Full Width

Featured