Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

variable help

Why when I run this code, the final line of text says Nice "the variable 'day'", instead of "the variable 'name'"?

Code
import java.util.Scanner;

class MyClass {
  public static void main(String[ ] args) {
    System.out.println("Enter your name");
    Scanner name = new Scanner(System.in);
    System.out.println("Hello " + name.nextLine() + ", What month were you born in?");  
    Scanner month = new Scanner(System.in);
    System.out.println("What day of " + month.nextLine() + "? (Ex. 21, 4, 19)");  
    Scanner day = new Scanner(System.in);
    System.out.println("Nice " + name.nextLine() + "! You have the same birthday as");
    
  }
}
[Image: 9Y60Xjq.png]
I don't know what your question is, but you only need one instance of a Scanner, not three.
You only need one instance of Scanner, and you should store user input in variables. Your issue has to do with calling name.nextLine() instead of using a variable. For example:

Code
Scanner scanner = new Scanner(System.in);
        
System.out.println("Enter a string:");
String string1 = scanner.nextLine();
        
System.out.println("Enter another string:");
String string2 = scanner.nextLine();

So, you could do something like:
Code
Scanner scanner = new Scanner(System.in);
          
System.out.println("Enter your name:");
String name = scanner.nextLine();
          
System.out.println("Hello, " + name + ", what month were you born in?");
String month = scanner.nextLine();
          
System.out.println("What day of " + month + " were you born in?");
int day = scanner.nextInt();
          
System.out.println("Nice, " + name + "! You have the same birthday as");

You'll notice I stored the day in an integer using scanner.nextInt() 🙂
Because you are not saving the input that the user enters.

Code
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String name;
        String month;
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter your name");
        //Saving the users input to String so you can use it later
        name = scanner.nextLine();
        System.out.println("Hello " + name + ", What month were you born in?");
        month = scanner.nextLine();

        System.out.println("What day of " + month + "? (Ex. 21, 4, 19)");  
        scanner.nextLine();
        
        System.out.println("Nice " + name + "! You have the same birthday as");
    
    }
}
Got it, oh and shouldn't this work? Why when I put January 1, it still goes to the else statement? I'm a noob dont hate.

Code
import java.util.Scanner;

class MyClass {
  public static void main(String[ ] args) {
    
      Scanner scanner = new Scanner(System.in);
      
      System.out.println("Enter your name:");
      String name = scanner.nextLine();
                
      System.out.println("Hello, " + name + ", what month were you born in?");
      String month = scanner.nextLine();
                
      System.out.println("What day of " + month + " were you born in? (Ex. 21, 4, 19)");
      int day = scanner.nextInt();
      
      if(month == "January" && day == 1) {
          System.out.println("You have the same birthday as Nia Lovelis");
        }
      else {
          System.out.println("test");
      }
  }
}
[Image: 9Y60Xjq.png]
(Nov 22, 2016 10:22 PM)Woes Wrote:  Got it, oh and shouldn't this work? Why when I put January 1, it still goes to the else statement? I'm a noob dont hate.

Code
import java.util.Scanner;

class MyClass {
  public static void main(String[ ] args) {
    
      Scanner scanner = new Scanner(System.in);
      
      System.out.println("Enter your name:");
      String name = scanner.nextLine();
                
      System.out.println("Hello, " + name + ", what month were you born in?");
      String month = scanner.nextLine();
                
      System.out.println("What day of " + month + " were you born in? (Ex. 21, 4, 19)");
      int day = scanner.nextInt();
      
      if(month == "January" && day == 1) {
          System.out.println("You have the same birthday as Nia Lovelis");
        }
      else {
          System.out.println("test");
      }
  }
}

month is a String therefore to compare it you must use:
Code
if(month.equals("January") && day == 1)
you might want to do a try/catch statement as well for data types/null values:

try {
code here...
}
} catch (InputMismatchException e) {
System.out.println("Enter a valid value");
}
(Nov 26, 2016 04:01 AM)throwAway445 Wrote:  you might want to do a try/catch statement as well for data types/null values:

try {
code here...
}
} catch (InputMismatchException e) {
System.out.println("Enter a valid value");
}

Adding on to that, you could use try-with-resources to ensure that the Scanner object is closed too.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Shell] by DoxinYourBotnetM8Mar 19, 2015 06:23 PM 21 394 Mar 19, 2015 06:23 PM
Last Post: Nakamoto.
[Shell] by Topkek_Is_LifeDec 9, 2014 09:15 PM 2 117 Dec 9, 2014 09:15 PM
Last Post: White Light
[Shell] by 'PalladiumOct 6, 2014 10:39 PM 5 113 Oct 6, 2014 10:39 PM
Last Post: 'Palladium