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.
Related
This question already has answers here:
Java: parse int value from a char
(9 answers)
How can I convert a char to int in Java? [duplicate]
(4 answers)
Closed 2 years ago.
I have this problem where I need to create a method that takes in a String and creates an integer array of each character. I have checked each step of the for loop and the array before and after the loop, and I am lost.
It correctly shows each character as '3' '4' and '5', however once it is inserted into the array, it always prints [51, 52, 53]. I am so lost where those numbers even came from? Thanks so much...
public class CodingBat {
public static void main(String[] args) {
String text = "345";
int[] create = new int[text.length()];
for(int i = 0; i < text.length(); i++) {
System.out.printf("Current array: %s\n", Arrays.toString(create));
//System.out.println(text.charAt(i));
create[i] = text.charAt(i);
System.out.printf("Adding %c\n", text.charAt(i));
}
System.out.println(Arrays.toString(create));
}
}
You're inserting a char into a int array, if i remember, the numbers that you see printing the array are the ASCII code.
So, if you want to get the number, use:
create[i] = Character.getNumericValue(text.charAt(i))
This question already has answers here:
Java: println with char array gives gibberish
(6 answers)
Closed 3 years ago.
I got a passwd by Console.readPasswd().When I save it in char array (char[] passwd) and use System.out.println(passwd) .The result is
I input is "123".
import java.io.Console;
public class ConsoleTest1 {
public static void main(String[] args){
Console console = System.console();
String username = console.readLine("user name :");
System.out.println("user name ="+username);
char[] password = console.readPassword("passwd:");
System.out.println("password="+password);
}
}
But when save the passwd in String passwd(I know it's not recommended)
import java.io.Console;
public class ConsoleTest1 {
public static void main(String[] args){
Console console = System.console();
String username = console.readLine("user name :");
System.out.println("user name ="+username);
String password = new String(console.readPassword("passwd:"));
System.out.println("password="+ password);
}
}
It can be accurately print:
Why?
Printing a char array directly using System.out.println will print the object representation of the array, hence the [C#232204a1.
In fact, even that representation tells you that it is a char array, as the prefix is [C, the [ indicating the printed object is an array, with the type being C, a char. For more, see.
You need to convert it to a string like in the second example you posted in order for it to be in a human readable format (ignoring all questions of should it be in a human readable format.
This question already has answers here:
Get string character by index
(13 answers)
Closed 4 years ago.
I'm attempting to create a new array of indexes from str2 parameter, but getting this error: "Array required, but string found."
I'm learning Java, and only comfortable writing in Javascript. Could someone explain what this error message means?
public class Scramblies {
public static boolean scramble(String str1, String str2) {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
int[] inOfStr2Nums = new int[str2.length()];
for (int i = 0; i < str2.length(); i++){
inOfStr2Nums[i] = alphabet.indexOf(str2[i]);
}
System.out.println(inOfStr2Nums);
}
}
To fix the error:
inOfStr2Nums[i] = alphabet.indexOf(str2.charAt(i));
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.
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