This question already has answers here:
Take a char input from the Scanner
(24 answers)
Closed 8 years ago.
according to the following:
triangles.java:17: error: cannot find symbol
shape = keyboard.nextChar();
^
symbol: method nextChar()
location: variable keyboard of type Scanner
1 error
why is it not finding symbol? i have keyboard initialized as the scanner. how would i fix this?
this is how i initialized it:
char shape;
shape = scanner.nextChar();
i have changed char to String, but that did nothing. i have imported a scanner, so thats not an issue.
If you're wanting to find the char within a String variable, just do something like:
String str = scanner.nextLine(); // or scanner.next();
char shape = str.charAt(i); // replace i with index of char you're trying to find.
hope that helps
Related
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 1 year ago.
import java.util.Arrays;
public class Caesar {
public static void main (String[] args) {
//user input
String [] input= {"bbcc"};
// turn user input from string to char, creates array.
char [] charinput = input.toCharArray();
// Arrays decleration
char [] abc={'a','b','c','d'};
char [] ABC={'A','B','C','D'};
/*
for (int i = 0; i <abc.length; i++){
// code to check if a char in string is in array abc
}
*/
}
}
why do I still get the error "Cannot resolve method 'toCharArray' in 'String'"?
I looked for solution on line but all I found was to add "import java.util.Arrays;" and then it should work but it still doesn't.
As you are using String array , toCharArray is not present.
Change it to:-
//user input
String input= "bbcc";
// turn user input from string to char, creates array.
char [] charinput = input.toCharArray();
This should work.
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
Tried both in my IDE and in the online IDE my textbook on Zybooks.com gives me to work in. My goal is to check whether or not the variable passCode contains a digit. Here's the code:
public class CheckingPasscodes{
public static void main (String [] args) {
boolean hasDigit = false;
String passCode = "";
int valid = 0;
passCode = "abc";
if (passCode.isDigit(passCode.charAt(0)) || passCode.isDigit(passCode.charAt(1)) || passCode.isDigit(passCode.charAt(2))) {
hasDigit = true;
}
if (hasDigit) {
System.out.println("Has a digit.");
}
else {
System.out.println("Has no digit.");
}
}
}
I get the error on line 9 (where the first if starts):
java: cannot find symbol
symbol: method isDigit(char)
location: variable passCode of type java.lang.String
Tried changing passCode.charAt(0) (or 1 and 2) to simply 'a' (or 'b' and 'c') to test whether it was because I was putting a method inside another method, but I seem to get the same error.
I eventually solved the problem by asking a friend, who provided me this, instead:
char s = passCode.charAt(0);
char s1 = passCode.charAt(1);
char s2 = passCode.charAt(2);
if ((s>'0' && s<'9') || (s1>'0' && s1<'9') || (s2>'0' && s2<'9')) {
hasDigit=true;
}
It makes perfect sense, and I did think of doing something similar, but the chapter in which this exercise is isn't about .charAt()—we covered that before—but rather about .isDigit() and other character operations, so doing what I did is all but cheating.
This is driving me nuts.
P.S.: I'm pretty new to Java, so, if the answer is something really obvious, I'm really sorry. And thanks for taking your time to read all this!
You're calling .isDigit() on a String object, which doesn't have such a method. It's a static method of the Character class.
Character.isDigit(passCode.charAt(0));
A big hint is that your error message stated the following:
location: variable passCode of type java.lang.String
which should automatically prompt you to look at the String class for the documentation on whatever method you're looking for - and, subsequently, to discover it doesn't have such a method.
java.lang.String doesnt have a isDigit method, that is only for Char class...
use it as
boolean hasDigit = Character.isDigit('1');
This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I am new to programming, so just consider my mistakes. I am writing a program where the user gives two input numbers in one line separated by whitespace. I have to assign the first input to an integer variable and second to a double and have to perform some mathematics and show the result. Following is my code:
import java.util.Scanner;
public class foo{
public static void main(String[] args){
String b = null;
Scanner sc = new Scanner(System.in);
b = sc.next();
String[] split = b.split(" ");
int i = Integer.parseInt(split[0]);
double d = Double.parseDouble(split[1]);
System.out.println(i+20);
System.out.println(d-1.50);
}
}
And following is the error i am getting while running it.
F:\java\work\codechef>javac foo.java
F:\java\work\codechef>java foo
20 300.50
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at foo.main(foo.java:9)
First I tried making it with b=sc.readLine(); but there i got the following error while compiling:
error: cannot find symbol
b = sc.readLine();
^
symbol: method readLine()
location: variable sc of type Scanner
1 error
Why I am getting these errors and how to solve the above problem.
You used sc.next() which returns the next token (a token is something that had delimiter before and/or after) so it contains only the 20 or 300.50 in your case.
You should use sc.nextLine() to use split later on, this will return the full line.
Or use:
int i = Integer.parseInt(sc.next());
double d = Double.parseDouble(sc.next());
And get rid of lines:
b = sc.next();
String[] split = b.split(" ");
This question already has answers here:
Take a char input from the Scanner
(24 answers)
Closed 8 years ago.
This is what I have in my code
char guess = Keyboard.readChar();
but the error message comes up as "The method readChar() is undefined for the type scanner" The scanner i have is Scanner keyboard = new Scanner (System.in). Why Is this wrong?
you need to use this
char guess = keyboard.next().charAt(0);
Scanner does not have a method to read a char. Fundamentally, System.in is a buffered stream. You can read a line,
while(keyboard.hasNextLine()) {
String line = keyboard.nextLine();
char[] chars = line.toCharArray(); // <-- the chars read.
}
You can trying using nextLine() which reads in a String of text.
char code = keyboard.nextLine().charAt(0);
charAt(0) takes in the first character of the received input.
Additional Note:
If you want to convert user inputs to upper/lower case. This is especially useful.
You can chain String methods together:
char code1 = keyboard.nextLine().toUpperCase().charAt(0); //Convert input to uppercase
char code2 = keyboard.nextLine().toLowerCase().charAt(0); //Convert input to lowercase
char code3 = keyboard.nextLine().replace(" ", "").charAt(0); //Prevent reading whitespace
This question already has answers here:
java.lang.System error in Console() [closed]
(3 answers)
Closed 4 years ago.
This is my code :---
import java.lang.*;
class Console
{
public static void main(String args[])
{
char i;
i=System.console().readLine("this is how we give he input to the string");
System.out.println("this is what we want to print:0)");
System.out.println(i);
}
}
and the output I am getting is this:-
Output:-
.java:7: cannot find symbol
symbol : method console()
location: class java.lang.System
i=System.console().readLine("this is how we give he input to the string");
^
1 error
Tool completed with exit code 1
If anyone can help me out...
Mistake with the jdk version, because it must be jdk1.6 or later, and when changed to a newer jdk,
there's a compilation problem, System.console().readLine() returns a String, but you assigned char
Also, some IDE's have trouble with the console class (possibly because they are using it themselves to redirect output to a window/dialog)
So a really good work around is using:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readline();
//or if you want a char
char i = str.charAt(0);
Hope that helps