Reputation:
import java.util.*;
public class OverloadingFG2 {
public static void main(String[] args) {
userenter();
userenter(false);
}
public static void userenter(){
Scanner input = new Scanner(System.in);
System.out.println("Enter your Fullname: ");
String fullname = input.nextLine();
System.out.println("");
System.out.println("Are you Male or Female?");
System.out.println("M for Male F for female: ");
String sex = input.nextLine();
String male = "M";
String female = "F";
if(sex.equalsIgnoreCase(male)){
System.out.println("Hello Mr. " + fullname + " Welcome to the party!");
}else if(sex.equalsIgnoreCase(female)){
System.out.println("Hello Ms. " + fullname + " Welcome to the party!");
}
System.out.println("");
}
public static boolean userenter(boolean yes){
Scanner input = new Scanner(System.in);
System.out.println("Are you with a company? Yes/No");
String ans = input.nextLine();
String y = "yes";
if (ans.equalsIgnoreCase(y)){
userenter("withmate");
}else
System.out.println("Have a Good Day ahead!");
return yes;
}
public static void userenter(String withmate){
Scanner input = new Scanner(System.in);
System.out.println("Enter your mates Fullname: ");
String fullname = input.nextLine();
System.out.println("");
System.out.println("Are you Male or Female?");
System.out.println("M for Male F for female: ");
String sex = input.nextLine();
String male = "M";
String female = "F";
if(sex.equalsIgnoreCase(male)){
System.out.println("Hello Mr. " + fullname + " Welcome to the party!");
}else if(sex.equalsIgnoreCase(female)){
System.out.println("Hello Ms. " + fullname + " Welcome to the party!");
}
System.out.println("");
}
}
** I have trouble getting the result from the first user. The plan is to greet both of them but I can't seem to get the value of the other method I'm using. The goal is to use overloading and to greet the both of them now I just have a problem on greeting the both of the user. I mean greet them together **
Upvotes: 0
Views: 85
Reputation: 36
I am not sure if I am understanding the question correctly. Do you want to greet both of the users towards the end?
Then you'll need to set up a global variable, like so:
import java.util.*;
public class OverloadingFG2 {
public static String fullName1;
public static void main(String[] args) {
userenter();
userenter(false);
}
public static void userenter(){
Scanner input = new Scanner(System.in);
System.out.println("Enter your Fullname: ");
fullName1 = input.nextLine();
System.out.println("");
System.out.println("Are you Male or Female?");
System.out.println("M for Male F for female: ");
String sex = input.nextLine();
String male = "M";
String female = "F";
if(sex.equalsIgnoreCase(male)){
System.out.println("Hello Mr. " + fullName1 + " Welcome to the party!");
}else if(sex.equalsIgnoreCase(female)){
System.out.println("Hello Ms. " + fullName1 + " Welcome to the party!");
}
System.out.println("");
}
public static boolean userenter(boolean yes){
Scanner input = new Scanner(System.in);
System.out.println("Are you with a company? Yes/No");
String ans = input.nextLine();
String y = "yes";
if (ans.equalsIgnoreCase(y)){
userenter("withmate");
}else
System.out.println("Have a Good Day ahead!");
return yes;
}
public static void userenter(String withmate){
Scanner input = new Scanner(System.in);
System.out.println("Enter your mates Fullname: ");
String fullname = input.nextLine();
System.out.println("");
System.out.println("Are you Male or Female?");
System.out.println("M for Male F for female: ");
String sex = input.nextLine();
String male = "M";
String female = "F";
if(sex.equalsIgnoreCase(male)){
System.out.println("Hello Mr. " + fullname + " and " + fullName1 + " Welcome to the party!");
}else if(sex.equalsIgnoreCase(female)){
System.out.println("Hello Ms. " + fullname + " and " + fullName1 + " Welcome to the party!");
}
System.out.println("");
}
}
Upvotes: 0
Reputation: 36
In response to your question:
In that case yes, you'll have to create a global variable for their gender. I've simplified the code for you for demonstration:
import java.util.*;
public class OverloadingFG2 {
public static String fullName1;
public static String gender1;
public static void main(String[] args) {
userenter();
userenter(false);
}
public static void userenter() {
Scanner input = new Scanner(System.in);
System.out.println("Enter your Fullname: ");
fullName1 = input.nextLine();
System.out.println("");
System.out.println("Are you Male or Female?");
System.out.println("M for Male F for female: ");
String sex = input.nextLine();
gender1 = (sex.equalsIgnoreCase("M") ? "Mr. " : "Ms. ");
System.out.println("Hello " + gender1 + fullName1 + " Welcome to the party!");
System.out.println("");
}
public static boolean userenter(boolean yes) {
Scanner input = new Scanner(System.in);
System.out.println("Are you with a company? Yes/No");
String ans = input.nextLine();
String y = "yes";
if (ans.equalsIgnoreCase(y)) {
userenter("withmate");
} else
System.out.println("Have a Good Day ahead!");
return yes;
}
public static void userenter(String withmate) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your mates Fullname: ");
String fullname = input.nextLine();
System.out.println("");
System.out.println("Are you Male or Female?");
System.out.println("M for Male F for female: ");
String sex = input.nextLine();
String gender2 = (sex.equalsIgnoreCase("M") ? "Mr. " : "Ms. ");
System.out.println("Hello " + gender2 + fullname + " and " + gender1 + fullName1 + " Welcome to the party!");
System.out.println("");
}
}
Upvotes: 0