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: program prints where I dont want it to print

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question program prints where I dont want it to print

    Hello, I've been doing this task for a while now and I am currently having trouble. The program supposed to print Mr. or Mrs. based on the gender on the user input. example is below.
    Input:
    Enter your gender (Male/Female):
    Male

    Output:
    Welcome, Mr. (name) etc and your gender is male.

    however the output becomes " Welcome, male (name)"

     
    package jksdj;
     
    import java.util.Scanner;
     
    public class JavaActivity2 {
     
        public static void main(String[] args) {
     
            Scanner name1 = new Scanner(System.in);
            System.out.println("Enter your first name: ");
            String firstName = name1.nextLine();
     
            Scanner name2 = new Scanner(System.in);
            System.out.println("Enter your middle initial: ");
            char middleInitial = name2.next().charAt(0);
     
            Scanner name3 = new Scanner(System.in);
            System.out.println("Enter your last name: ");
            String lastName = name3.nextLine();
     
            Scanner bday = new Scanner(System.in);
            System.out.println("Enter your Birthdate (mm/dd/yyyy): ");
            String birthDate = bday.nextLine();
     
            Scanner housenum = new Scanner(System.in);
            System.out.println("Enter the number of people in your household: ");
            int peopleHouse = housenum.nextInt();
     
            Scanner salary = new Scanner(System.in);
            System.out.println("Enter your annual salary: ");
            double annualSalary = salary.nextDouble();
     
            String Usergender = "";
     
            Scanner gender = new Scanner(System.in);
            System.out.println("Enter your gender (Male/Female): ");
            Usergender = gender.nextLine();
     
            if (Usergender.equalsIgnoreCase("Male") || Usergender.equalsIgnoreCase("male")) {
                System.out.println("Mr. ");
            } else {
                System.out.println("Ms. ");
            }
     
            Scanner address = new Scanner(System.in);
            System.out.println("Enter your address: ");
            String userAddress = address.nextLine();
     
            String[] bdayArray = birthDate.split("/", 3);
            int currentYear = 2022;
            int age = currentYear - Integer.parseInt(bdayArray[2]);
     
            System.out.println("Welcome," + Usergender + " " + firstName + " " + middleInitial + ". " + lastName
                    + " to our system. Your birthdate is " + birthDate +
                    ",and you are " + age + " years old, your gender is " + Usergender + " and you reside in " + userAddress
                    +
                    ". The total number of people in your household is " + peopleHouse + " and your annual salary is Php "
                    + annualSalary + ".");
     
        }
    }
    Last edited by chrilo; February 15th, 2022 at 11:43 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: program prints where I dont want it to print

    The output is created in the order that the statements are executed.
    Print welcome first, then the mr or mrs and then the name.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Piglatin program that prints no words out:(
    By srr1900 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 22nd, 2019, 05:01 PM
  2. Replies: 2
    Last Post: April 13th, 2014, 03:34 PM
  3. system.out prints twice in a small program
    By vinaysagar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 12th, 2014, 07:36 AM
  4. when i run the program to find the y values it always prints y2 instead
    By jamesR in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 30th, 2012, 08:38 AM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM