Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Please Help

$
0
0
Hey guys, I am trying to learn java in my spare time. My goal was to learn how a main method can call other classes (hope I’m saying that right). When I execute main.java, in case 2 and 3, the program doesn’t wait to for me to type anything in for name or temp before executing the program. I have attempted to move the Scanner input = new Scanner(System.in); around to the different cases but it didn’t work. If anyone can point me towards the right direction ill greatly appreciate it.



import java.util.Scanner;
public class Main {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);
		
		System.out.print(" (1) Using Multiple Classes (2) Use Methods with Parameters (3) Many Methods and Instances (5) Constructors ");
		int choice = input.nextInt();
		
		switch (choice){
			
		
		case 1:
		     one oneObject = new one();
		     oneObject.simpleMessage();	
		     break;
		     
			case 2:
				
			    two twoObject = new two();
				
				System.out.println("what do you want stored");
				String name = input.nextLine();
				
				twoObject.simpleMessage(name);
			
				// case 2 does not wait for you to insert your input why?
				break;
				
			case 3:			
			    three threeObject = new three();
				
				System.out.println("Enter name of first gf here:");
				String temp = input.nextLine();
				threeObject.setName(temp);
				threeObject.saying();
				
			//	send temp to equal girl name
			//case 3 has the same problem as case 2
				break;
		        
			default:
			System.out.println("you are ordering tacos");
		       break;
		
		}
	}

}






public class two {
	public void simpleMessage(String name)
	{
		System.out.println("Use Methods with Parameters " + name);
	}
}






public class three {
	private String girlName;
	public void setName(String name){
		girlName=name;
	}
	public String getName(){
		return girlName;
	}
	public void saying(){
		System.out.printf("Your first girlfreind was %s", getName());
    
	}
	
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles