Why is this in the console?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Why is this in the console?

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    74

    Why is this in the console?

    I have an applet with 7 textfields (3 for input and 4 for display). Now, the applet runs fine without any syntactic or semantic errors, however I get this:

    Code:
    Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For input string: ""
    	at java.lang.NumberFormatException.forInputString(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at BirthdayApplet.actionPerformed(BirthdayApplet.java:86)
    	at java.awt.TextField.processActionEvent(Unknown Source)
    	at java.awt.TextField.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For input string: ""
    	at java.lang.NumberFormatException.forInputString(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at BirthdayApplet.actionPerformed(BirthdayApplet.java:89)
    	at java.awt.TextField.processActionEvent(Unknown Source)
    	at java.awt.TextField.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Whenever I input something in the textfield and hit enter. Is this a problem, or just console output?

  2. #2
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    It would really help if you posted your code.

    I am not a java guru but I think it might be because the input is in bad format, or it is not getting the input at all
    Last edited by Wizard1988; 09-30-2005 at 05:49 PM.

  3. #3
    Join Date
    Sep 2005
    Posts
    74
    Yeah I'm pretty sure most things in this program are formatted badly right now (I have no idea how to do this program..so I've been trying to work 'around' so to speak and it isn't working out too well). The code isn't commented very much right now but the problem is this:

    Write a program that tells the day of the week someone's birthday falls on this year.

    Somewhere along the way my professor wants us to call a method that validates if the year the user put in is a leap year (I'm not sure why). A couple of things are still messed up (January 1, 2000 and January 1, 2001 both come out to be Saturday even though I incremented the date if the year was a leap year).

    Here is the code:
    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class BirthdayApplet extends Applet implements ActionListener 
    {
    
    	//	Declare instance variables
    	private int year;
    	private int month;
    	private int day;
    	private int dayOfYear;
    	private String dayOfWeek;
    	
    	/*	
    	 * Declare GUI components
    	 */
    	
    	//	Labels
    	private Label yearLabel = new Label ("Please enter what year you were " +
    										"born (ex: 1968, 1987, 1999)");
    	private Label monthLabel = new Label ("Please enter what month you were " +
    										 "born (ex: 5, 10, 11)");
    	private Label dayLabel = new Label("Please enter what day you were born " +
    										  "(ex: 5, 10, 15)");
    	private Label programmer = new Label ("Programmed by:  Victor Celaschi, "
    			  + " EECS 1560");
    	private Label comboLabel = new Label ("This year your birthday falls on: ");
    	
    	//	Textfields
    	private TextField yearField = new TextField(10);
    	private TextField monthField = new TextField(10);
    	private TextField dayField = new TextField(10);
    	private TextField yearResultField = new TextField(10);
    	private TextField monthResultField = new TextField(10);
    	private TextField dayResultField = new TextField(10);
    	private TextField comboField = new TextField(20);
    	
    	
    	public void init()
    	{
    		// Make inputField editable and add an actionListener
    		yearField.setEditable(true);
    		yearField.addActionListener(this);
    		yearResultField.setEditable(false);
    		monthField.setEditable(true);
    		monthField.addActionListener(this);
    		monthResultField.setEditable(false);
    		dayField.setEditable(true);
    		dayField.addActionListener(this);
    		dayResultField.setEditable(false);
    		comboField.setEditable(false);
    		
    		//	Add the components to the interface
    		add(monthLabel);
    		add(monthField);
    		add(monthResultField);
    		add(dayLabel);
    		add(dayField);
    		add(dayResultField);
    		add(yearLabel);
    		add(yearField);
    		add(yearResultField);
    		add(comboLabel);
    		add(comboField);
    		add(programmer);
    		
    		//	Set the applet's size
    		setSize(600,150);
    	}// init()
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		month = Integer.parseInt(monthField.getText());
    		String getMonth = getMonthOfYear(month);
    		monthResultField.setText(getMonth);
    		day = Integer.parseInt(dayField.getText());
    		dayOfWeek = Integer.toString(day);
    		dayResultField.setText(dayOfWeek);
    		year = Integer.parseInt(yearField.getText());
    		String getYear = Integer.toString(year);
    		yearResultField.setText(getYear);
    		dayOfYear = dayOfYear(month, day, year);
    		dayOfYear = dayOfYear % 7;
    		String stringStuff = getDayOfWeek(dayOfYear);
    		comboField.setText(stringStuff);
    		
    	}
    	
    	//	Returns the day of the year, given month and day
    	public int dayOfYear(int month, int day, int year)
    	{
    		int assignMonth = month;
    		int assignDay = day;
    		int assignYear = year;
    		int returnValue;
    		
    		switch (assignMonth)
    		{
    		case 1:
    			returnValue = 0;
    			break;
    		case 2:
    			returnValue = 31;
    			break;
    		case 3:
    			returnValue = 59;
    			break;
    		case 4:
    			returnValue = 90;
    			break;
    		case 5:
    			returnValue = 120;
    			break;
    		case 6:
    			returnValue = 151;
    			break;
    		case 7:
    			returnValue = 181;
    			break;
    		case 8:
    			returnValue = 212;
    			break;
    		case 9:
    			returnValue = 243;
    			break;
    		case 10:
    			returnValue = 273;
    			break;
    		case 11:
    			returnValue = 304;
    			break;
    		case 12:
    			returnValue = 334;
    			break;
    		default:
    			returnValue = 0;
    		}
    		
    		if (assignMonth > 2 && Year.isLeapYear(assignYear))
    		{
    			returnValue++;
    			return returnValue + assignDay;
    		} else {
    			return returnValue + assignDay;
    		}
    	}
    	
    	// Returns the day of the week for days in 2005
    	public String getDayOfWeek(int days)
    	{
    		int assignDayOfWeek = days;
    		
    		//	First day of year Saturday
    		switch (assignDayOfWeek)
    		{
    		case 0:
    			return "Friday";
    		case 1:
    			return "Saturday";
    		case 2:
    			return "Sunday";
    		case 3:
    			return "Monday";
    		case 4:
    			return "Tuesday";
    		case 5:
    			return "Wednesday";
    		case 6:
    			return "Thursday";
    		case 7: 
    			return "Friday";
    		default:
    			return "No date set";
    		} // switch()
    	} // getDayOfWeek()
    	
    	//	Returns the month of the year for months in 2005
    	public String getMonthOfYear(int months)
    	{
    		int assignMonthOfYear = months;
    		
    		switch (assignMonthOfYear)
    		{
    		case 1:
    			return "January";
    		case 2: 
    			return "February";
    		case 3:
    			return "March";
    		case 4:
    			return "April";
    		case 5: 
    			return "May";
    		case 6:
    			return "June";
    		case 7:
    			return "July";
    		case 8:
    			return "August";
    		case 9:
    			return "September";
    		case 10:
    			return "October";
    		case 11:
    			return "November";
    		case 12:
    			return "December";
    		default:
    			return "No month set";
    		}// switch()
    	}// getMonthOfYear()
    } // BirthdayApplet
    Now, I don't know if incrementing the date if the year was a leap year is right or not (because no matter what the date is still going to fall on the same day this year no matter what year you were born). But we were asked to put a check in..so I did.

  4. #4
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    What is this code supposed to do?

    Code:
    if (assignMonth > 2 && year.isLeapYear(assignYear))
    		{
    			returnValue++;
    			return returnValue + assignDay;
    		} else {
    			return returnValue + assignDay;
    		}
    I don't understand what you are trying to do with

    year.isLeapYear(assignYear)

    Is that supposed to check if the current year is a leap year and thats all?

  5. #5
    Join Date
    Sep 2005
    Posts
    74
    If the month is greater than February then it checks if the year is a leap year. If so, it adds one to the day variable, adjusting the day of the week accordingly. I don't really know if that needs to be there..because one of the months is bound to have 32 days now throwing everything off, and even if someone was born on a leap year their birthday will still fall on the same day of the week this year.

    I wonder if I should just get rid of that check, or make it so only February is affected by the leap year?

  6. #6
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    I don't know if your professor wants you do do the calculations by yourself, but I am pretty sure you can use GregorianCalandar to figure out the Day of the week of a given data

  7. #7
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    I found a shortcut for performig the check. I don't know if this is the way your professor wants this to be done.

    I used GregorianCalendar, First I created a date and I just used its method to return the day of the week.

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import java.util.*;
    
    
    public class BirthdayApplet extends Applet implements ActionListener 
    {
    
    	//	Declare instance variables
    	private int year;
    	private int month;
    	private int day;
    	private int dayOfYear;
    	private String dayOfWeek;
    	
    	/*	
    	 * Declare GUI components
    	 */
    	
    	//	Labels
    	private Label yearLabel = new Label ("Please enter what year you were " +
    										"born (ex: 1968, 1987, 1999)");
    	private Label monthLabel = new Label ("Please enter what month you were " +
    										 "born (ex: 5, 10, 11)");
    	private Label dayLabel = new Label("Please enter what day you were born " +
    										  "(ex: 5, 10, 15)");
    	private Label programmer = new Label ("Programmed by:  Victor Celaschi, "
    			  + " EECS 1560");
    	private Label comboLabel = new Label ("This year your birthday falls on: ");
    	
    	//	Textfields
    	private TextField yearField = new TextField(10);
    	private TextField monthField = new TextField(10);
    	private TextField dayField = new TextField(10);
    	private TextField yearResultField = new TextField(10);
    	private TextField monthResultField = new TextField(10);
    	private TextField dayResultField = new TextField(10);
    	private TextField comboField = new TextField(20);
    	
    	
    	//Used for determining if year is a leap year
    	GregorianCalendar calendar = new GregorianCalendar();
    
    	
    	
    	public void init()
    	{
    		// Make inputField editable and add an actionListener
    		yearField.setEditable(true);
    		yearField.addActionListener(this);
    		yearResultField.setEditable(false);
    		monthField.setEditable(true);
    		monthField.addActionListener(this);
    		monthResultField.setEditable(false);
    		dayField.setEditable(true);
    		dayField.addActionListener(this);
    		dayResultField.setEditable(false);
    		comboField.setEditable(false);
    		
    		//	Add the components to the interface
    		add(monthLabel);
    		add(monthField);
    		add(monthResultField);
    		add(dayLabel);
    		add(dayField);
    		add(dayResultField);
    		add(yearLabel);
    		add(yearField);
    		add(yearResultField);
    		add(comboLabel);
    		add(comboField);
    		add(programmer);
    		
    		//	Set the applet's size
    		setSize(600,150);
    	}// init()
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		month = Integer.parseInt(monthField.getText());
    		String getMonth = getMonthOfYear(month);
    		monthResultField.setText(getMonth);
    		day = Integer.parseInt(dayField.getText());
    		dayOfWeek = Integer.toString(day);
    		dayResultField.setText(dayOfWeek);
    		year = Integer.parseInt(yearField.getText());
    		String getYear = Integer.toString(year);
    		yearResultField.setText(getYear);
    		
    		String stringStuff = getDayOfWeek(month, day, year);
    		comboField.setText(stringStuff);
    		
    	}
    	
    	//	Returns the day of the year, given month and day
    //	public int dayOfYear()
    //	{
    		
    		
    //	}
    	
    	// Returns the day of the week for days in 2005
    	public String getDayOfWeek(int month, int day, int year)
    	{
    		
    		calendar.set(year,month,day);
    		int dayInWeek = calendar.DAY_OF_WEEK;
    		
    		
    		//	First day of year Saturday
    		switch (dayInWeek)
    		{
    		case 1:
    			return "Sunday";
    		case 2:
    			return "Monday";
    		case 3:
    			return "Tuesday";
    		case 4:
    			return "Wednesday";
    		case 5:
    			return "Thursday";
    		case 6:
    			return "Friday";
    		case 7:
    			return "Saturday";
    		default:
    			return "No date set";
    		} // switch()
    	} // getDayOfWeek()
    	
    	//	Returns the month of the year for months in 2005
    	public String getMonthOfYear(int months)
    	{
    		int assignMonthOfYear = months;
    		
    		switch (assignMonthOfYear)
    		{
    		case 1:
    			return "January";
    		case 2: 
    			return "February";
    		case 3:
    			return "March";
    		case 4:
    			return "April";
    		case 5: 
    			return "May";
    		case 6:
    			return "June";
    		case 7:
    			return "July";
    		case 8:
    			return "August";
    		case 9:
    			return "September";
    		case 10:
    			return "October";
    		case 11:
    			return "November";
    		case 12:
    			return "December";
    		default:
    			return "No month set";
    		}// switch()
    	}// getMonthOfYear()
    } // BirthdayApplet

    I hope this helps

  8. #8
    Join Date
    Sep 2005
    Posts
    74
    Interesting. Although I figured out why I was getting junk in the console..I had no e.getSource() parameters..so the console didn't know that the textfields were actually getting input. All works great now, except one minor thing. If someone types a string into the textfield..the console will fill up with junk again because it doesn't know how to handle strings. Is there someway I can check that the input is a number?

    Thanks for all your help so far.

  9. #9
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108
    Try this

    Code:
    try 
    { 
    i=Interger.parseInt(TextField.getText());
    }
    catch(Exception e){System.out.println("Bad Input");}

    It doesen't really check, but If there is something wrong with the input it allows you to print out a message saying that something is wrong with the input

Similar Threads

  1. Simple Console question
    By Steve in forum .NET
    Replies: 4
    Last Post: 05-21-2002, 03:04 AM
  2. console window
    By Bill Gauvey in forum Enterprise
    Replies: 0
    Last Post: 07-31-2001, 06:10 PM
  3. mess. - console
    By Linda Aust in forum Java
    Replies: 1
    Last Post: 04-22-2001, 03:27 PM
  4. Scraping Output from a console application
    By Christopher Lutz in forum VB Classic
    Replies: 8
    Last Post: 05-08-2000, 05:06 PM
  5. Outputing message to console screen
    By Saiful in forum VB Classic
    Replies: 0
    Last Post: 04-05-2000, 11:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center