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
Why is the below code not working when using "!" not equals; it gave me "while" statement has an empty body? and when I'm removing the "!" it works...
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = "";
while (!input.equals("quit")); {
System.out.print("Input : ");
input = scanner.next().toLowerCase();
System.out.println(input);
}
}
}
you have to remove the semicolon
while(!input.equals("quit")) {
...
}
Remove the ; in the loop before {
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm writing a simple quiz program. The code on line 19 is giving me a run time error. Could someone please advise why?
import java.util.Scanner;
public class javaQuiz {
public static void main(String[] args) {
String questionOne = "Who is the best band member of the beatles?";
String questionOneAns = "John";
String questionTwo = "what is 1 + 1?";
int questionTwoAns = 2;
String questionThree = "What continent is China a part of?";
String questionThreeAns = "Asia";
String questionFour = "Who is the Turing Test named after?";
String questionFourAns = "Alan Turing";
Scanner userI = new Scanner(System.in);
String userAns = scan.NextLine();
System.out.println(questionOne);
if(userAns == questionOneAns) {
System.out.println("Correct!");
} else {
System.out.println("Wrong answer!");
}
}
}
Isnt it because your scanner is called userI instead of scan?
Line 19 should be:
String userAns = userI.nextLine();
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
Please explain the use of System.in.read() method in this example that I'm learning about from another post. As a beginner, I find it unclear when I input a number and get different one in the output, please clarify.
import java.io.IOException;
public class MainClass {
public static void main(String[] args) {
int inChar;
System.out.println("Enter a Character:");
try {
inChar = System.in.read();
System.out.print("You entered ");
System.out.println(inChar);
}
catch (IOException e){
System.out.println("Error reading from user");
}
}
System.in.read() reads values as their binary value; for example reading "a" will result in the value of "97" - the mapping of this is available here https://www.asciitable.com/.
In order to get the textual representation of this in Java you want to read this as either a Character or a String - Character is a single value, while a String is a combination of Characters one after the other. For example:
public class MainClass {
public static void main(String[] args) {
System.out.println("Enter a Character:");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
System.out.print("You entered ");
System.out.println(input);
}
Have a look at the Scanner class to see other options, you can use scanner.nextInt() to get an Integer back.
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 7 years ago.
Improve this question
I am trying to iterate through each character in a string that is inputted and check if any letter is a.
Here is my Java code:
import java.util.Scanner;
public class Input
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String input;
input = in.nextLine();
for (int i=0; i<input.length(); i++)
{
char chararcter = input.charAt(i);
String s = Character.toString(character); //the error is here
if (s.equals("a"))
{
System.out.println("You typed an A.");
}
}
}
}
For reference, here is a Python analog.
input=raw_input()
for i in range (0,len(input)):
if input[i] == "a":
print "You typed an A."
I apologize for the simplistic nature of this question; I'm very new to Java. Thank you for helping.
You've change the spelling in your declaration.
char character = input.charAt(i); // <-- not chararcter (extra rc).
You don't have to convert character to String. just simply do character comparison.
if (chararcter == 'a')
{
System.out.println("You typed an A.");
}
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.util.Scanner;
public class Fernando {
public static void main(String[] args) {
Scanner skype = new Scanner(System.in);
System.out.print("What is your name ")?
String n = skype.nextLine(); // This is the line with the error
System.out.print("\n and how old are you?");
int y = skype.nextInt();
displayInfo(n,y);
}
public String displayInfo(String name, int age){
return name +" is "+ age+" years old.";
}
}
All it says is expected. I don't understand what's wrong at all.
Try changing
System.out.print("What is your name ")?
to:
System.out.println("What is your name?");
This should work, as you have a ? instead of a ;.
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.util.Scanner;
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers");
Scanner chopper = first Scanner();
int x =chopper.nextInt();
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
}
}
}
This keeps telling me that a ';' is expected on the line that starts with scanner chopper, what could the problem be?
P.S. Do you know how i can get it to keep count of how many integers were typed in?
You have written first Scanner() instead of new Scanner(System.in).
You need to specify an input source, and since you want the user to input the numbers, you should use System.in as argument.
This line:
Scanner chopper = first Scanner();
should be:
Scanner chopper = new Scanner(System.in);
new Scanner(System.in) creates a new Scanner object that take input from the console, first Scanner() is syntactically incorrect and is what gives the error.
Why does your code say first Scanner(); instead of new Scanner();? I think that's the problem.