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.");
}
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 1 year ago.
Improve this question
i try to see if the char of a string == "1" but it shows error as bad operand types for binary operator.
import java.util.Scanner;
public class Q3 {
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter any number: ");
int num = scan.nextInt();
int length = String.valueOf(num).length();
int reverse = 0;
if (length<=3) {
while(num != 0) {
int remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num/10;
}
String word = "";
String numword=String.valueOf(reverse);
char character = numword.charAt(0);
System.out.println(character=="1");}}}
edit: i debugged it. i had to use '' instead of "" thanks for the answer everyone....
Chars in Java are set by quotes instead of double quotes.
Please try
character=='1'
instead.
The double quotes are used to instantiate an Object of type String. In your case you want to compare a Character, which is instantiate with simple quote.
It should do the trick for you :
character == '1'
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 1 year ago.
Improve this question
college student learning java. I am working on a homework assignment on creating class exceptions. For some reason my if statement seems to keep generating my class exception method. Take a look.
public class ICDEDriver {
public static void main(String[] args) throws InvalidDocumentCodeException {
Scanner scan = new Scanner(System.in);
InvalidDocumentCodeException problem = new InvalidDocumentCodeException("this is my error message")
System.out.println("Enter a valid document code: ");
String code = scan.nextLine();
if(code.charAt(0) != 'U' || code.charAt(0) != 'C' || code.charAt(0) != 'P')
throw problem;
System.out.println("End of main method");
}
}
So my issue is that when I enter a "valid document code" (document code must start with U, C, or P) it still triggers the InvalidDocumentCodeException. How can I fix it to where the if statement will accept U, C, or P as the first character? Thanks!
Here try to use the && operator
if((code.charAt(0) != 'U' && code.charAt(0) != 'C' && code.charAt(0) != 'P'))
throw problem;
The || operator does evaluate to true even if one side of the logic is false, && will make sure all of them are true.
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 {
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
I am a student and I am learning Java.I recently got a question which said I had to find the largest word in a given string..I wrote a code but it is giving me an error that string index is out of bounds even though I limited it to the length of the string..Can someone help me with the code..Please use simple language(I am not an expert)
Code
import java.util.*;
class word
{
void def()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s1=sc.nextLine();
int length=s1.length();
length++; //My name
int j=0; //0123456
int word=0;
int findex=0;
int lindex=0;
int lword=0;
for(int i=0;i<length;i++)
{
if(s1.charAt(i)==' ' && j==0)
{
lword=i;
findex=0;
lindex=i;
j=i;
}
else if(s1.charAt(i)==' ')
{
if(i-j-1>lword)
{
findex=j;
lindex=i;
lword=i-j-1;
}
j=i;
}
else if(i==length-1)
{
if(i-j-1>lword)
{
findex=j;
lindex=i;
}
}
}
System.out.println("Largest word is:"+s1.substring(findex,lindex+1));
}
}
By doing length++ you make sure that your length variable will be larger then the actual string length. at the last loop step there will be no char at s1.charAt(i)
Just remove the line with length++
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.