hello, i am new to java and i have no idea how to solve this... i decided to ask you guys because i want to learn java to write some plugins and i have no idea where to ask it. with new i mean really new btw Code: import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("What's your name?"); String name = s.nextLine(); System.out.println("Welcome, " + name); System.out.println("Do you know the answer of 12*12?"); String Number = s.nextLine(); if(true) { System.out.println("that's correct! the answer is 144!"); } else { System.out.println("WRONG! you suck! the answer is 144!"); } } } how do i get this to work? i know i should put something before if and else but i have no idea what, could someone help me? pim
try to fill in 28634654 at the place where it asks for the answer whatever you fill in you'll get Code: System.out.println("that's correct! the answer is 144!"); i don't know how to make it clear to java that only 144 is correct
Code: package errorscheck1; import java.util.Scanner; public class Errorscheck1 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("What's your name?"); String name = s.nextLine(); System.out.println("Welcome, " + name); System.out.println("Do you know the answer of 12*12?"); String Number = s.nextLine(); if ("144".equals(Number)) { System.out.println("that's correct! the answer is 144!"); } else { System.out.println("WRONG! you suck! the answer is 144!"); } } } This code should work, and I'll try to explain it as thoroughly as I can, I am a beginner myself: What you did wrong: if(true) is checking for a boolean value that doesn't have to do with what you are trying to check Why the above code works if("144".equals(Number)) is checking if the string "Number" is equal to the string value "144", and if the user enters anything but 144 then it will return a boolean "false" value, which is what if() statements look for. In a nutshell, you weren't actually checking if Number was equal to 144 If anything I've said here is unclear, then please let me know. To anyone else: if I have gotten anything here wrong, please correct me because I wouldn't doubt it.
Code: if (Number.equals("144")) { System.out.println("Correct!"); } else { System.out.println("Incorrect!"); } Should work, i think EDIT: Bleh, I see I took way too long and Ahniolater gave a much better answer anyway
@Pim1234 First, welcome to Java. Now, the problem is that you're testing if true is true (which it will always be, no matter what) to print that the answer is correct. What you want to do instead is check if the answer they inputted is 144. My suggestion would be to do this: 1. Instead of declaring and initializing the String named Number, change that String to an Integer. To do this is actually very simple: First, change String to int. Now the variable "Number" is an Integer. You may see a red underline. This is because when you use the method (If you don't know what a method is, don't worry, you'll learn it later on) nextLine(), it returns, or gives you back a String, and it's not possible to store a String variable inside an Integer variable (Think of trying to take a bunny and stuff it inside a potato, it just doesn't work). You'll need to change this to nextInt(). The method nextInt() returns an Integer variable, so you'll be able to store the next Integer (or whole number) they input inside your Number variable. 2. Now that you have changed the Number variable from a String to an Integer, you need to test if it's 144 in the if statement. This is the simple part. You just do this: if(Number == 144). You may notice I used two equal signs instead of one. When you type two equal signs next to each other in Java (and most other programming languages) it means "is equal to". So basically, you're saying this: "If Number is equal to 144, then print out that the answer is correct. Else, print out that the answer is wrong." Hopefully this helped. (P.S. Are you learning from thenewboston (Bucky)?
i know that it wasn't checking for anything to be true or false, i just decided not to show my miserable tries to egt it to work i had no idea what to fill in there. thanks you all for helping me. no i am learning this from http://www.javavideotutes.com/ and after lesson 13 (i thought it was 13) i decided to experiment a bit, but i failed. i wanted to solve this before i would continue with the course to thanks again
one more question: why does this not work? Code: if ("144".equals(Number)); { System.out.println("that's correct! the answer is 144!"); } else { System.out.println("WRONG! you suck! the answer is 144!"); }
Also, not sure if anyone mentioned it yet, but it is easier (to read) when you place the thing to compare with under equals, and the thing you wish to compare before it. It's not wrong, but it looks rather weird. Plus, if you want to change the function a bit (like equalsIgnoreCase, or contains) you don't have to rewrite everything. Besides that, an important Java syntax rule: class names start with capitals, variables and methods (members) start without a capital. This will ensure others (you and also future colleagues (?)) to understand what is what. Code: public class MyClass { public String myString; public void myFunction(String myString) { this.myString = myString; } } Code: if (number.equals("144")) { System.out.println("that's correct! the answer is 144!"); } else { System.out.println("WRONG! you suck! the answer is 144!"); } See it as some positive remarks you can work on.
if (this && that) { // do if this and that is true } else if (a || b) { // do if a or b is true } else { // something } Use boolean operators if you want one statement, but you can have branching if and else statements.
ok, thanks a lot and something else again: how to set a delay between 2 commands? EDIT: what's wrong with this? Code: if (name == "Wout" || name == "Hans") {
Strings should be compared with .equals, so: Code: if(name.equals("Wout") || name.equals("Hans")); If you use == you are comparing the pointer (when dealing with objects), which you don't want in this case. Some examples when comparing strings or other objects can be seen below. (Remember that an int is not an object, nor is a float, double, boolean, byte, and probably more I forgot) Code: String a = "Wololo"; String b = a; String c = "Wololo"; if (a == b); // true if (a == c); // false if (b == c); // false if(a.equals(c)); // true If you don't get that, please ask what you don't get, because I am having a bit of trouble thinking of what to say to clarify this...