Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
package firstproject;
import java.util.Scanner;
public class walkthrough {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String a = in.nextLine();
System.out.println("Welcome home!");
System.out.println("Would you like to go in?");
String door = a;
if(a.equals(door));
System.out.println("Your are now in the house what now?");
}
}
This:
if(a.equals(door));
System.out.println("Your are now in the house what now?");
is:
if(a.equals(door)) { }
System.out.println("Your are now in the house what now?");
The print statement will be executed regardless of the value of the expression in the if condition.
Remove the redundant ; after the if condition.
Also please follow the conventions and rename your class to begin with a capital letter.
Semicolon after if statement. Change ; in {
if(a.equals(door)); // semicolon - statement
System.out.println("Your are now in the house what now?");
Which means System.out.println("Your are now in the house what now?"); is executed irrespective of if statement.
Class names should start with a capital letter in Java: walkthrough -> Walkthrough
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Ok SO I made an escape the room program and the boolean for light doesn't work
class Main {
public static void main(String[] args) {
System.out.println("Welcome To The Hogwarts Escape Room");
System.out.println("To Play The Hogwarts Escape Room Press Y");
Scanner sc = new Scanner(System.in);
String useranswer = sc.nextLine();
boolean light = false;
// This is not my entire code but the part that was necessary.
System.out.println("Press U to look up");
useranswer = sc.nextLine();
if (usernaswer.equals("U"){
System.out.println(" You look above you and see nothing as the ceiling is too dark to see in.");
System.out.println("Would you like to: \n Look Closer press A \n Return To Your Original Position press B");
useranswer = sc.nextLine();
System.out.println(light);
// this part doesn't work.. I checked and made sure the useranswer was A and the boolean does equal to false.
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
else if (useranswer.equals("B")){
original(sc, useranswer, inventory, light);
}
// this was the setup to the constructor
public static void ceilingCloserlightFalse(Scanner sc, String useranswer, String [] inventory, boolean light){
System.out.println("You go closer and see something faintly but cant tell what it is without a light");
System.out.println("You returned to the original position because there was nothing else to do there.");
original(sc, useranswer, inventory, light);
}
}
}
Ok So I wonder if I am doing something wrong with the method or the && operator. I checked to make sure that useranswer was a and I also checked to make sure that
Here you are assigning false to the variable, not comparing it:
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
You want to use ==, not =. Also, consider that useranswer might be null, so you should be doing "A".equals(useranswer) to avoid a NullPointerException
Some developers use Yoda speak to avoid the common mistake we have here.
Instead of:
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
Write your comparison backward like
if (useranswer.equals("A")&&(false = light)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
Then you'd get a compiler error pointing you to the fact you're using = instead of ==.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
for some context i'm trying to make a program that tells you different things depending on your number input. For example: number '0' is hello, world! number '6' is goodbye and else is Jad this is all stuff that is placeholders the error that keeps showing up is
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=1][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E] in the console
here's the code:
public static void main(String[] args) {
int a = 6;
if (a == 0) {
System.out.println("hello, World");
} else if (a == 6) {
System.out.println("goodbye");
} else {
System.out.println("Jad");
}
Scanner userInput;
userInput = new Scanner(System.in);
a = userInput.nextInt();
System.out.println("You have " + userInput + " eggs");
}
you typo the variable name, the last line should be:
System.out.println("You have " + a);
"else if (a == 6)" do have a "if". you can not write "else if" without "if".
To solve this you can move the "else if" block outside the if block
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to delete the user integer input from an array. However, I can't get the value of the input.
while(keepGoing)
{
while (!scan.hasNextInt() )
{
scan.next();
System.out.print('\n'+"Choose a valid number: ");
}
int unitA = scan.nextInt();
if (unitA < 1)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else if (unitA > 14)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else
lengthValue.remove(unitA);
scan.close();
keepGoing = false;
}
//lengthValue.remove(int unitA);
System.out.println(unitA);
In my opinion, you forget to press "Enter" after entering input. A scanner can read the input if only you press the "Enter" key. Your solution seems correct to me. I was able to properly run it on my PC.
You can find a similar question here:
How to use Scanner to accept only valid int as input
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Example code:
import java.util.Scanner;
public class Split {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a few words: ");
String wordsWhole = scan.next();
String[] wordsSplit = new String[4];
wordsSplit = wordsWhole.split("//s+");
System.out.println("Second word: " + wordsSplit[1]);
}
}
The output:
Enter a few words: Why no work
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
at test.Split.main(Split.java:12)
My String isn't splitting into the array like I would expect it to. Any ideas on why this is?
Line 12:
System.out.println("Second word: " + wordsSplit[1]);
There are several problems:
Scanner.next() will only return the first word (space-separated) in the input, use Scanner.nextLine() to get the entire line.
I'm guessing you're trying to split by spaces. If so, you should use backslashes rather than forward slashes in your regex ("\\s+").
You don't need to allocate the array before assigning it to the result of the split. Just use String[] wordsSplit = wordsWhole.split("\\s+");
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
import java.io.*;
public class StringInput{
public static void main (String args[]);{
String Name "StringInput";
System.out.print ("Enter your name: ");
InputStreamReader input = new InputStreamReader(System.in);
InputStreamReader reader = new BufferedReader(input);
name = reader.readline();
catchException e;{
}//Exception
System.out.println ("Hello"+Name+"How are you?");
}//main
}//class
That is the code that I was doing, theres one error. Line 4, I can't figure out what it is.
We were doing it in class (I just started Computer Programming 12) and we didn't have time to finish it and everything.
If someone could help me figure out what is wrong with line 4 that'd be really helpful.
Thanks ! :)
Removed the semi colon, still get String Name "StringInput";
^
1 error
Process completed.
Remove the semicolon(;) after main method:
Change this:
public static void main (String args[]);{
to
public static void main (String args[]) {
Another error in your code is related to try/catch usage:
Change this:
name = reader.readline();
catchException e;{
}//Exception
to
try {
name = reader.readline();
catch(Exception e){
e.printStackTrace();
}//Exception
Final Note: Learn the language syntax first before jumping to coding.