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

Thread: If statement called before user input ?

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If statement called before user input ?

    Hello all,

    I've started with java code yesturday and I'm fooling around with it a little now, but...
    I ran in to my first ever java problem!

    I have this code down here and I want the user to either type Yes or No but I can't, the if statement is called before I even put anything.
    This is probably easy to fix but since I'm completely new to java, I have no idea how to do so.

    Code:

    package core;

    import java.util.Scanner;

    public class Main {
    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter Name: ");
    String name = input.nextLine();
    System.out.println("Enter Age: ");
    int age = input.nextInt();

    System.out.println("Name: " + name);
    System.out.println("Age: " + age);

    System.out.println("Start loop ? Type Yes or No.");
    String doLoop = input.nextLine();

    int loop = 0;

    if (doLoop == "Yes") {

    System.out.println("Loop will start.");

    while (loop < 5) {

    System.out.println("Looping: " + loop);

    }

    } else if (doLoop == "No"){

    System.out.println("No loop will start, end of script.");

    }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: If statement called before user input ?

    That is a standard problem with using Scanner methods when a next... method is called before a call to nextLine. See this link:
    https://www.geeksforgeeks.org/why-is...ext-functions/
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: If statement called before user input ?

    Quote Originally Posted by Norm View Post
    That is a standard problem with using Scanner methods when a next... method is called before a call to nextLine. See this link:
    https://www.geeksforgeeks.org/why-is...ext-functions/
    Thank you

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: If statement called before user input ?

    Another problem I see in your code is using the == operator to compare String objects. Always use the equals() method to compare objects.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] How do I accept user input?
    By Jag_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 19th, 2019, 06:27 AM
  2. User Input help
    By dx2731 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 13th, 2013, 12:46 AM
  3. User Input help
    By lanmonster in forum What's Wrong With My Code?
    Replies: 20
    Last Post: January 20th, 2013, 10:44 AM
  4. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  5. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM