variable help - Woes - Nov 22, 2016
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");
}
}
* - Stealth™ - Nov 22, 2016
I don't know what your question is, but you only need one instance of a Scanner, not three.
* - SoyNuevo - Nov 22, 2016
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() 🙂
* - ka$h - Nov 22, 2016
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");
}
}
* - Woes - Nov 22, 2016
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");
}
}
}
* - ka$h - Nov 22, 2016
(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)
* - throwAway445 - Nov 26, 2016
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");
}
* - CAFEEFAC - Nov 28, 2016
(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.
|