Small program - how can I make this code work with a string instead of a character?
I am slowly trying to write a program that converts a hexadecimal number to decimal. I am not interested in reading ready-made, well-known codes because I want to do it myself. I have an idea, but something is bothering me.
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String any = input.nextLine();
char[] cArray = any.toCharArray();
for(int i=0; i<cArray.length; i++){
System.out.print(cArray[i]+" ");
}
}
}
Input: ab12
Output: a b 1 2
I want to replace a
with 10
, b
with 11
, c
with 12
, etc.
It works if I add an if-statement inside the for loop.
for(int i=0; i<cArray.length; i++){
if(cArray[i] == 'a'){
cArray[i] = '10'; // doesn't work, read below
}
System.out.print(cArray[i]+" ");
}
The problem is that I want to replace a
with 10
and is 10
no longer a symbol since it is two letters long. So I would like to know how to do this code with strings
instead of characters
?
source to share
Instead of replacing the values ββin cArray
I will create a StringBuilder and add all the values ββto this (since presumably you want to print the result): -
StringBuilder str = new StringBuilder();
for(int i=0; i<cArray.length; i++){
if(cArray[i] == 'a'){
str.append(10);
} else if (cArray[i] == 'b'){
[etc]
} else {
str.append(cArray[i]);
}
}
System.out.print(str.toString());
source to share
I am assuming that you want to access the converted decimal digits after extracting them. Use List of String to store your output
Scanner in = new Scanner(System.in);
String any = in.nextLine();
char[] cArray = any.toCharArray();
List<String> output = new ArrayList<String>();
for(int i=0; i<cArray.length; i++){
if(cArray[i] >= 'a'){ // Strore a,b,c,d,e
output.add(String.valueOf(10+(cArray[i]-'a')));
} else { // Store numbers
output.add(String.valueOf(cArray[i]));
}
}
for(String s : output){
System.out.println(s);
}
source to share
Let's take a look at this:
if (cArray[i] == 'a') {
cArray[i] = '10';
}
This is not valid Java for two reasons:
-
'10'
is not a valid literal. It is not a character literal, because there are two characters ... and a character literal can only represent one character. It is not a string literal because the string literal is enclosed in double-quoted characters; for example"10"
. -
Assuming we are changing
'10'
to"10"
... this is still wrong. Now the problem is whatcArray[i] = "10";
assigns the String object to a character array.
The next problem is that you cannot "insert" into an array. Arrays are of fixed size. The size of the array cannot change (unless you create a new array). All you can do is update the symbol at the given position.
But that doesn't work either. You can try to move the characters to the right to make room for additional characters. However, then you would not have enough space in the array to store all the characters.
In short, you need to represent the changed / rewritten symbols as a new data structure. The class StringBuilder
is most appropriate. See @ SteveSmith's answer for a solution using StringBuilder
.
source to share
To make your code work with a string instead of a character, change the char array to a string array:
Scanner input = new Scanner(System.in);
String any = input.nextLine();
//char[] cArray = any.toCharArray(); // first change this line
String [] cArray = any.split(""); // split input into single characters as string
for(int i=0; i<cArray.length; i++){
System.out.print(cArray[i]+" ");
}
for(int i=0; i<cArray.length; i++){
if(cArray[i].equals("a")){ // use String.equals("anotherString") method to check equality
cArray[i] = "10";
}
System.out.print(cArray[i]+" ");
}
source to share