Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with instantiating a class within a switch statement

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with instantiating a class within a switch statement

    Hello everyone, I am not to sure how to go about doing this. What I want to do is instantiate an object from a class and then adjust the values via keyboard input. I think the problem may be that I am trying to adjust the values within switch statements.
    Here is the code from the testprogram:
    public static void main(String[] args) 
        {
        	String name;
        	String address;
        	int id;
        	double hours;
        	Client x;
        	for(int select=0;select!=3;)
        	{
        	Client num1= new Client("11","111",111,111);
        	Scanner scan = new Scanner(System.in);
        	System.out.println("Please select from the following menu:");
        	System.out.println("1:Add Client");
        	System.out.println("2:Create Invoice");
        	System.out.println("3:Quit");
            select = scan.nextInt();
        	switch(select)
        	{
     
        	case 1:	
        	System.out.println("Please enter the Client's Name: ");
        	name = scan.next();
        	x.setClientName(name);
     
        	System.out.println("Please enter the Client's ID Number: ");
        	id = scan.nextInt();
        	x.setClientId(id);
     
        	System.out.println("Please enter the Client's Address: ");
        	address = scan.next();
        	x.setClientAddress(address);
     
        	System.out.println("Please enter the number of hours worked: ");
        	hours = scan.nextDouble();
        	x.setClientHours(hours);
     
    Here is the class that I am using.
     
    {
        	clientName=name;
        	clientAddress=address;
        	clientId=id;
        	clientHours=hours;
     
        }
        public void setClientName(String name)
        {
        	clientName=name;
        }
        public void setClientAddress(String address)
        {
        	clientAddress=address;
        }
        public void setClientId(int id)
        {
        	clientId=id;
        }
        public void setClientHours(double hours)
        {
        	clientHours=hours;
        }
     
        public String getClientName()
        {
        	return clientName;
        }
        public String getClientAddress()
        {
        	return clientAddress;
        }
        public int getClientId()
        {
        	return clientId;
        }
        public double getClientHours()
        {
        	return clientHours;
        }
    All of the x.sets turn up errors when I compile. Is it possible to Instantiate an object or modify its variables from within switch statements? If it is how would I go about doing that?
    Thank You in advance


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with instantiating a class within a switch statement

    I'm really not sure what that code is supposed to do. You'll probably want to throw together an SSCCE that demonstrates what you're talking about.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  3. Need help with instantiating a class
    By suxen in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2011, 03:35 PM
  4. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  5. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM