Announcement

Collapse
No announcement yet.

A really stupid programmer needs help with a really basic JavaScript question

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • A really stupid programmer needs help with a really basic JavaScript question

    I am a Junior in High School taking a basic JavaScript class and I am in the middle of a project.

    For this project and I need to use If-Else statements while reading user inputs. I need to ask the user to input his first name. Ask if [user] likes rock music. If [user] says 'Y' for yes give three suggestions and if 'N', tell the [user] to have a good day.

    Here is what I came up with:

    package askpaul;
    import java.util.Scanner;

    /**
    *
    * Author: ************ DLM: 10/6/2014
    */

    public class AskPaul {

    public static void main(String[] args) {

    String userChoice;

    String userName;

    Scanner in = new Scanner( System.in );


    System.out.println("Dear sir/madam, please enter your first name \n ==>");
    userName = in.next(); //User inputs his name

    if (userName.equals ("Paul")) {

    System.out.println("Paul, do you like rock music? \n Answer 'Y' for yes and 'N' for no");
    userChoice = in.next(); //User gives answer

    if (userChoice.equals ("Y")) {

    System.out.println("Paul, here are some suggestions: \n 'Butterflies and Hurricanes' by Muse \n 'Jukebox Hero' by Foreigner \n 'Bohemian Rhapsody' by Queen ");
    }

    else {

    System.out.println("Thanks for visiting!");
    }

    }
    }
    }


    MY QUESTION:

    So as you can see, the program only gives suggestions if the users name is "Paul" (the project said to work with a user named Paul).
    I want to be able to work with any name given but System.out.println does not let me.

    System.out.println("userName, do you like rock music?") gives the output of: 'userName, do you like rock music?' because it is outputting a string....

    So basically I'm asking how do I output "userName, do you like rock music?" and it shows up with whatever name the user used.

    I feel like I'm missing something very obvious so feel free to throw it in my face :).

    “Big Brother is Watching You.”
    ― George Orwell, "1984"

  • #2
    Re: A really stupid programmer needs help with a really basic JavaScript question

    Your storing the name in the variable "userName" so you'll just need to figure out how to output that. My coding experience is just a couple C++ classes and fiddling with stuff at home over the years(html,java,coding things for Battlefield) so don't take what I say as Gospel.

    Having never messed with Javascript but seeing obvious similarities to other languages I would wager it is something like this.

    Code:
    System.out.println(userName + ", do you like rock music? \n Answer 'Y' for yes and 'N' for no");
    |TG-12th|mantis

    Comment


    • #3
      Re: A really stupid programmer needs help with a really basic JavaScript question

      Thanks! I forgot to use the addition sign :P.

      “Big Brother is Watching You.”
      ― George Orwell, "1984"

      Comment


      • #4
        Re: A really stupid programmer needs help with a really basic JavaScript question

        Try using a variable as the default username and the default for that variable can be Paul.

        Ex. Name=varName
        varName="Paul"

        Comment


        • #5
          Re: A really stupid programmer needs help with a really basic JavaScript question

          Mantis got it in one.
          |TG-18th| Acreo Aeneas
          TG World of Tanks Clan Executive Officer
          Former 9th & 13th

          Pronounciation: Eh-Cree-Oh Ah-Nay-Ess
          Still can't say it? Call me Acorn then. -.-





          SSDs I Own: Kingston HyperX 3K (240 GB), Samsung 840 Pro (256 GB), Samsung 840 EVO (250 GB), Samsung 840 x 2 (120 GB), Plextor M5S (120 GB), OCZ Vertex (30 GB)

          TG Primer and Rules

          Comment


          • #6
            Re: A really stupid programmer needs help with a really basic JavaScript question

            I'm stuck on another question.I am trying to create a basic guessing game using a looping system.
            I want to have a player guess a number (7) between 1-10 and give a "Try Again" continuously until the user guesses it right. I'm guessing I'm going to have to use a Do-While Loop and this is what I have so far. I set it so it will loop as long as userGuess != 7. So as soon as I guess 7 it should stop and display the message outside of the loop...

            Code:
            public class MyGuessingGameJava {
                public static void main(String[] args) throws IOException {
            
                    System.out.println("Welcome Player! Guess a number between 1-10");
            
            int userGuess;
            
            do {
                userGuess = (int) System.in.read();
            
                System.in.read();
            
                System.out.println("Try Again!");
            }while(userGuess != 7);
            
            System.out.println("You guessed it!");
            }
            }
            Even if I input "7" after it says "Try Again" instead of going outside the loop.
            What am I doing wrong?

            “Big Brother is Watching You.”
            ― George Orwell, "1984"

            Comment


            • #7
              Re: A really stupid programmer needs help with a really basic JavaScript question

              //add these codes at the top:
              import java.io.IOException;
              import java.util.Scanner;

              //here is the do part of the loop:
              int userGuess;
              Scanner keyboard = new Scanner(System.in);

              do {
              userGuess = (int) keyboard.nextInt();
              System.out.println("Try Again!");

              //read wasn't doing what you were expecting it to
              //to find out what was going on put this right after read

              System.out.println(userGuess); //You'll see that you are not getting the numbers you expected

              and technically, you do not even need the do part, you can put it all in the while loop.

              "Quando omni flunkus moritati" -Red Green Show
              [Link to my Youtube!] [Link to my Twitch!]
              [support]

              Comment


              • #8
                Re: A really stupid programmer needs help with a really basic JavaScript question

                Originally posted by penandpencilman View Post
                Even if I input "7" after it says "Try Again" instead of going outside the loop.
                What am I doing wrong?
                Whatever is in that do loop will be completed before it checks the while condition. So whatever integer is being read in, "Try Again!" is always going to be printed the way you currently have the code.


                Need something more like this. This is just basic Pseudocode to get the logic/flow.

                Code:
                guess created
                
                While( guess not equal to 7) {
                
                guess = user input;
                
                if ( guess not equal to 7) {
                  print "Try again!"
                 }
                
                }
                
                Print "You finally guessed it!"
                |TG-12th|mantis

                Comment

                Connect

                Collapse

                TeamSpeak 3 Server

                Collapse

                Advertisement

                Collapse

                Twitter Feed

                Collapse

                Working...
                X