String expected Error? [closed] - java

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 ;.

Related

Java - "while" with using not equal object >> not working [closed]

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 {

I can not get my code to run due to a run time error when trying to assign the String userAns to input [closed]

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();

Why `.read()` Method of a number return different number [closed]

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.

Java - Having trouble converting char to string (beginner) [closed]

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.");
}

If statements not running [closed]

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
The strings were parsed to integers, so the condition of the if statements are properly right set. Why are the if statements not running? Why there doesn't appear a MessageDialog with a response?
class process{
public static void whoIs(){
JFrame frame=new JFrame("The Oldest");
String a=JOptionPane.showInputDialog(frame, "Enter, please, the first name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE);
String b=JOptionPane.showInputDialog(frame, "Enter, please, the second name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE);
String age1=a.replaceAll("[^\\d]","");
String age2=a.replaceAll("[^\\d]","");
String name1=a.replaceAll("\\d","");
String name2=b.replaceAll("\\d","");
int age1int=Integer.parseInt(age1);
int age2int=Integer.parseInt(age2);
if (age1int>age2int){
JOptionPane.showMessageDialog(frame, name1+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE);
}
if (age2int>age1int) {
JOptionPane.showMessageDialog(frame, name2+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE);
}
}
}
Your both ages are same and your if condition will not match since you are not considering the equality. I think you missed this, Next time try with debugger then you can identify these kind of issues by your own.
String age1=a.replaceAll("[^\\d]","");
String age2=a.replaceAll("[^\\d]","");
This should change this to following.
String age1=a.replaceAll("[^\\d]","");
String age2=b.replaceAll("[^\\d]","");

Categories

Resources