Display welcome message

program not compiling

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 13508 Views - Last Post: 10 September 2009 - 11:22 AM Rate Topic: -----

#1 idle_09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 02-March 08

Display welcome message

Posted 09 September 2009 - 11:09 AM

Write a Java Program that prompts the user to enter his/her first name and last name, then displays a message to welcome the user into the class CIS 226. If the user hits Cancel on either first or last name, show the error message: " Sorry, you have to enter your name to enroll into CIS 226."

package Assign2;

import javax.swing.JOptionPane;

public class Assign2 
{
	// main method begins execution of Java application
	   public static void main( String[] args )
	{
	   String firstname = JOptionPane.showInputDialog("Please enter your first name");
	   String lastname = JOptionPane.showInputDialog("Please enter your last name");
	   JOptionPane.showMessageDialog(null, "Hello, " + firstname + " " + lastname + ", " + "Welcome to CIS 226"); 
	
	}

}


This post has been edited by idle_09: 09 September 2009 - 11:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Display welcome message

#2 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Display welcome message

Posted 09 September 2009 - 11:14 AM

I dont see anything that looks obviously wrong. What error message are you getting?
Was This Post Helpful? 1
  • +
  • -

#3 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Display welcome message

Posted 09 September 2009 - 11:15 AM

what is your problem? the program compiles and runs OK
Was This Post Helpful? 0
  • +
  • -

#4 idle_09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 02-March 08

Re: Display welcome message

Posted 09 September 2009 - 11:20 AM

when i run this program, it gives me this message:

java.lang.NoClassDefFoundError: Assign2 (wrong name: Assignment2/Assign2)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Exception in thread "main"
Was This Post Helpful? 0
  • +
  • -

#5 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: Display welcome message

Posted 09 September 2009 - 11:44 AM

View Postidle_09, on 9 Sep, 2009 - 05:20 PM, said:

when i run this program, it gives me this message:

java.lang.NoClassDefFoundError: Assign2 (wrong name: Assignment2/Assign2)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Exception in thread "main"

change into the directory where the .class file is and type
java Assign2
and it should work
Was This Post Helpful? 1
  • +
  • -

#6 idle_09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 02-March 08

Re: Display welcome message

Posted 09 September 2009 - 11:48 AM

ok, i have changed the code and new error message:

Description Resource Path Location Type
The declared package "" does not match the expected package "Assign2" Assign2.java fig02_01/Assign2 line 1 Java Problem

Thank you all for helping me. The program runs fine now but with the error stated above. Thank You.

This post has been edited by idle_09: 09 September 2009 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 carltech   User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Display welcome message

Posted 09 September 2009 - 11:56 AM

I'm guessing that you are using an IDE and not a command prompt or terminal. In that case what is the name of your file?
Was This Post Helpful? 0
  • +
  • -

#8 idle_09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 02-March 08

Re: Display welcome message

Posted 09 September 2009 - 11:59 AM

the name of the file is Assign2

also can someone tell me whats the code for this:
If the user hits Cancel on either first or last name, show the error message: " Sorry, you have to enter your name to enroll into CIS 226."

This post has been edited by idle_09: 09 September 2009 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#9 carltech   User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Display welcome message

Posted 09 September 2009 - 12:07 PM

Ok first off comment out the first line like so //package Assign2;.


And second you can check for the cancel since the name strings will be empty....if(firstname.isEmpty())
Was This Post Helpful? 0
  • +
  • -

#10 idle_09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 02-March 08

Re: Display welcome message

Posted 09 September 2009 - 12:09 PM

if i comment it out, i am getting the same error message, "The declared package "" does not match the expected package "Assign2" Assign2.java fig02_01/Assign2 line 1 Java Problem."
Was This Post Helpful? 0
  • +
  • -

#11 idle_09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 02-March 08

Re: Display welcome message

Posted 09 September 2009 - 12:21 PM

package Assign2;

import javax.swing.JOptionPane; 

public class Assign2 
{
	// main method begins execution of Java application
	   public static void main( String[] args )
	{
	   String firstname = JOptionPane.showInputDialog("Please enter your first name");
	   if(firstname.isEmpty())
		   JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226");
		   
	   String lastname = JOptionPane.showInputDialog("Please enter your last name");
	   if(lastname.isEmpty())
		   JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226");
	   JOptionPane.showMessageDialog(null, "Hello, " + firstname + " " + lastname + ", " +  "Welcome to CIS 226"); 
	
	}

}



when i run this code, it gives an error message if you leave first name or last name balnk, but it doesn's ask user again for the name.

if i do this
 //package Assign2; 
, then the program doesn't run!!!
Was This Post Helpful? 0
  • +
  • -

#12 carltech   User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Display welcome message

Posted 09 September 2009 - 12:27 PM

This is what i did and it ran fine for me:
//package Assign2;

import javax.swing.JOptionPane;

public class Assign2 
{
	// main method begins execution of Java application
	   public static void main( String[] args )
	{
	   String firstname = JOptionPane.showInputDialog("Please enter your first name");
	   String lastname = JOptionPane.showInputDialog("Please enter your last name");
	   
	   
	   JOptionPane.showMessageDialog(null, "Hello, " + firstname + " " + lastname + ", " + "Welcome to CIS 226"); 
	
	}

}



for the first problem anyways

This post has been edited by carltech: 09 September 2009 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#13 idle_09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 02-March 08

Re: Display welcome message

Posted 09 September 2009 - 12:33 PM

carltech, did i use if statement correctly?
Was This Post Helpful? 0
  • +
  • -

#14 AntonWebsters   User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Display welcome message

Posted 09 September 2009 - 12:53 PM



import javax.swing.JOptionPane; 

public class BankAccount
{
	// main method begins execution of Java application
	   public static void main( String[] args )
	{
		   String firstname = JOptionPane.showInputDialog("Please enter your first name");
		do{  
		   JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226");
		   firstname = JOptionPane.showInputDialog("Please enter your first name");
		}while(firstname.isEmpty());   
	   String lastname = JOptionPane.showInputDialog("Please enter your last name");
	   do{
		   JOptionPane.showMessageDialog(null, "Sorry, you have to enter your name to enroll into CIS 226");
		   lastname = JOptionPane.showInputDialog("Please enter your last name");
	   }while(firstname.isEmpty());
	   JOptionPane.showMessageDialog(null, "Hello, " + firstname + " " + lastname + ", " +  "Welcome to CIS 226"); 
	
	}

}



Well, this works for me.

This post has been edited by AntonWebsters: 09 September 2009 - 11:49 PM

Was This Post Helpful? 0
  • +
  • -

#15 carltech   User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Display welcome message

Posted 09 September 2009 - 12:59 PM

@idle: Yes but you need to make sure the end message isn't shown at the same time as the error message so you need an else

@AntonWebsters I was trying to get him to figure the loops and stuff on his own. but good stuff though

This post has been edited by carltech: 09 September 2009 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2