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

Thread: Help with my code

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my code

    Hello, I am fairly new to Java and decided it would be fun to mess around to see what I can do so far. I was wondering if anyone could help me with my problem. I am making a interactive sort of game where you use scanners to take the next course. I have created some code so far but for some reason when I try to confirm the players name it takes both of the scanner I set up already and mashes them into one. I know this might be a bad explanation but if someone could figure out what I mean and help me so that I answer one question at a time that would be great. Thanks.

    import java.util.*;
    public class fun
    {
    public static void main (String [] args)
    {
    Scanner console = new Scanner (System.in);
    System.out.println("Hello Traveler, Welcome to THE GAME OF CHANCE!");
    System.out.println("My name is Za, Ruler of All. I will be your guide on your journey.");
    int start = 0;
    while (start == 0)
    {
    int wait = 1;
    System.out.println("Type in your name, it can't be too long and it doesn't have to be your real name so make it a good one.");
    String name = console.nextLine();
    if (name.equals("your name"))
    {
    System.out.println ("Oh we have a comedian I see");
    }
    else if (name.equals("too long"))
    {
    System.out.println ("Seriously?? I said it can't be 'too long'");
    }
    else if (name.equals("your real name"))
    {
    System.out.println ("I said it doesn't have to be 'your real name'");
    }
    else if (name.equals("a good one"))
    {
    System.out.println ("That is...not what i meant");
    }
    else
    {
    start = 1;
    wait = 0;
    while(wait == 0)
    {
    System.out.println ("Your name is " + name + "? (Answer with 'yes' or 'no'");
    String yesorno = console.next();
    if (yesorno.equals("yes"))
    {
    System.out.println("Alrighty then " + name + " let's get you on your way");
    wait = 1;
    }
    else if (yesorno.equals("no"))
    {
    System.out.println("Then what is your name?");
    wait = 1;
    start = 0;
    }
    else
    {
    System.out.println("Invalid input");
    wait = 1;
    start = 0;
    }
    }
    }
    }
    }
    }

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Help with my code

    when I try to confirm the players name it takes both of the scanner I set up already and mashes them into one
    Can you elaborate more?
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    May 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my code

    Quote Originally Posted by John Joe View Post
    Can you elaborate more?
    If you put in the code into Java, and you type in a name it then goes into confirmation of the name and if you put 'no' saying that is not your name it restarts the code up to the top asking for your name but when I do that it asks whats your name and if it is correct at the same time.

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Help with my code

    Try change the position to this and see

    public static void main(String[] args) {
            Scanner console = new Scanner(System.in);
            System.out.println("Hello Traveler, Welcome to THE GAME OF CHANCE!");
            System.out.println("My name is Za, Ruler of All. I will be your guide on your journey.");
            int start = 0;
            System.out.println("Type in your name, it can't be too long and it doesn't have to be your real name so make it a good one.");
            while (start == 0) {
                int wait = 1;
                String name = console.nextLine();
                if (name.equals("your name")) {
                    System.out.println("Oh we have a comedian I see");
                } else if (name.equals("too long")) {
                    System.out.println("Seriously?? I said it can't be 'too long'");
                } else if (name.equals("your real name")) {
                    System.out.println("I said it doesn't have to be 'your real name'");
                } else if (name.equals("a good one")) {
                    System.out.println("That is...not what i meant");
                } else {
                    start = 1;
                    wait = 0;
                    while (wait == 0) {
                        System.out.println("Your name is " + name + "? (Answer with 'yes' or 'no'");
                        String yesorno = console.next();
                        if (yesorno.equals("yes")) {
                            System.out.println("Alrighty then " + name + " let's get you on your way");
                            wait = 1;
                        } else if (yesorno.equals("no")) {
                            System.out.println("Then what is your name?");
                            wait = 1;
                            start = 0;
                            System.out.println("Type in your name, it can't be too long and it doesn't have to be your real name so make it a good one.");
                            name = console.nextLine();
                        } else {
                            System.out.println("Invalid input");
                            wait = 1;
                            start = 0;
                        }
                    }
                }
            }
        }
    Whatever you are, be a good one

  5. #5
    Junior Member
    Join Date
    May 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my code

    It worked! Thank you so much

  6. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Help with my code

    You're welcome.
    Whatever you are, be a good one

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  2. Replies: 4
    Last Post: January 24th, 2013, 11:20 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM

Tags for this Thread