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 3 of 3

Thread: Do you add objects to an array when using switch statement with a For loop?

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

    Default Do you add objects to an array when using switch statement with a For loop?

    I meant....

    "HOW" do you add objects to an array....

    I have four classes.

    Person is the super class, Student, Undergraduate, and PolymorphismExample.

    I somewhat understand how to add objects into an array, whoever, I'm not sure how to do it when using inputs in a switch statement contained in a for loop. How would I go about in doing that?


    import java.util.Scanner;                                                                             
     
    public	class PolymorphismExample                                                                     
    {                                                                                                     
     
    	public static void main(String[] args)                                                            
    	{                                                                                                 
    		Person[] people = new Person[4];         
    		people[0] = new Person();
    		people[1] = new Undergraduate( );
    		people[2] = new Person();
    		people[3] = new Student();
     
    		char userInput;      
     
    		Scanner keyboard = new Scanner (System.in);                                                                                                  
     
    		for(int i=0; i<4; i++)                                                                      
    		{   
    			String name;
    			int number;
    			int level;
    			System.out.print("Enter the type of Person: ");                             
    			userInput = keyboard.next().charAt(0);
     
    				switch(userInput)                                                                         
    				{                                                                                         
    				case 'P': 
    					System.out.print("Enter in a name: ");                                          
    					name = keyboard.next();                                                       
                                            break;                                                                            
    				case 'S':                                                                             
    					System.out.print("Enter in a name: ");
    					name = keyboard.next();
    					System.out.print("Enter a student number: ");
    					number = keyboard.nextInt();
    					break;                                                                            
    				case 'U': 
    					System.out.print("Enter in a names: "); 
    					name = keyboard.next();
    					System.out.print("Enter a student number: ");
    					number = keyboard.nextInt();
    					System.out.print("Enter a level ");
    					level = keyboard.nextInt();
    					break;
    				default:
    					System.out.println("You picked a value other than P, S, U");
     
     
    			}                                                                                       
    		}                                                                                             
    	}                                                                                                 
    }


    The ultimate goal is to get to here....


    Enter the type of person: P
    Enter a name: Philip Rivers

    Enter the type of person: U
    Enter a name: Vincent Jackson
    Enter a student number: 83
    Enter a level: 3

    Enter the type of person: P
    Enter a name: Ryan Mathews

    Enter the type of person: S
    Enter a name: Antonio Gates
    Enter a student number: 85


    Person 1:
    Name: Philip Rivers

    Person 2:
    Name: Vincent Jackson
    Student Number: 83
    Student Level: 3

    Person 3:
    Name: Ryan Mathews

    Person 4:
    Name: Antonio Gates
    Student Number: 85
    Last edited by TheWhopper858; November 13th, 2011 at 01:14 PM.


  2. #2
    Member
    Join Date
    Nov 2011
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Do you add objects to an array when using switch statement with a For loop?

    Hi,
    For two word input from user try nextLine.

    Core java
    Last edited by mr.miku; January 11th, 2012 at 07:11 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Do you add objects to an array when using switch statement with a For loop?

    I'm not sure how to do it when using inputs in a switch statement contained in a for loop.
    The location of the assignment statement should not make any difference. If you know how to write a statement to assign a value to an element of an array, you can code that statement in the loop or switch statement

Similar Threads

  1. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 10
    Last Post: August 2nd, 2018, 07:47 AM
  2. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 11:50 PM
  3. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  4. Advancing day w/ if/else & switch statement
    By rbread80 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 10:43 PM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM