This question already has answers here:
Array size changing after creation
(4 answers)
Closed 5 years ago.
I have a Java code associated with BigInteger class below,
import java.math.BigInteger;
public class Main
{
public static void main (String[] args)
{
BigInteger num = new BigInteger("123456789101112131415");
String str;
char []Char = new char[10];
str = num.toString();
Char = str.toCharArray();
System.out.println(str.length());
System.out.println(Char.length);
}
}
In that code, I have a BigInteger num which has 21 digits. I converted it into a String then into a Char array. But I assigned the length of that char array is just 10 before. So how this char array holds 21 characters altogether???
I really misunderstand the fact.
And one more question here, is there any method to convert BigIntegers into char arrays directly? :)
To answer your first question, the variable Char (consider changing your variable names) is a reference to a char array. You set it to point to an array of size 10. But later, thanks to...
Char = str.toCharArray();
...you are making the reference point to another array. The original array no longer has a reference associated with it and is garbage collected.
To answer your second question, you can cascade functions in a single line.
char[] Char = num.toString().toCharArray();
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:
How to convert a char array back to a string?
(14 answers)
Closed 3 years ago.
My problem is I have 4 arrays, a[1]=1, a[2]=3, a[3]=4, a[4]=5, and want to save as new string/ char, so the output will be s[ ]={1345}
I try to define like this, but it doesn't works
char s[]= new char [5];
s={'a[1]','a[2]','a[3]','a[4]'};
Instead of initialising the char array s[] and then setting the value in the next line, you can directly initialize the array like: char s[] = {a[0], a[1], a[2], a[3]};
In Java the concept of String is fairly simple. You dont have to define it as a character array. Just take a String variable and concat the array values to it. The below is how you can do it.
public static void main(String[] args) {
int[] a = {1,2,3,4};
String output = a[0]+a[1]+a[2]+a[3];
System.out.println(output);
}
Hope it shall work for you.
This question already has answers here:
What is the easiest/best/most correct way to iterate through the characters of a string in Java?
(16 answers)
Closed 4 years ago.
so i am learning how to use the HashMap object. My question is, if I use the scanner object to ask a user for a String and I save that to a String variable. How can I take that String and "compare" it to my HashMap.
For example if a user inputs "abc".
My HashMap has ("a","abra") ("b","blastoise") ("c","charizard")
I want to print to System.out.println the result -- abra blastoise charizard instead of abc.
I will share my code that i have now but i am stuck with the next step and i hope that i am being clear with my question.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String keyboard = "";
HashMap hm = new HashMap();
hm.put("A","Abra");
hm.put("B", "Blastoise");
hm.put("C", "Charizard");
hm.put("D", "Dewgong");
keyboard = sc.nextLine();
Thank you in advanced :)
You can use chars as the key of your Map such put('a', "Abra") then you compare each char.
char[] str = string.toLowerCase().toCharArray();
for(char c : str)
System.out.println(map.get(c));
Also note that 'A' is different from 'a', you need to handle such case the best one would calling toLowerCase() in the string before getCharArray().
This question already has answers here:
Using charAt method, won't add them as an Int, and wont print as string. Will explain better
(4 answers)
Using charAt in System.out.println displays integer?
(5 answers)
Why does this code print the ASCII value instead of the character
(2 answers)
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 5 years ago.
I'm getting the user to enter a string of numbers, then a for loop should pick out each number from the string and add it to an ArrayList. I'm sure someone can help me out fairly quickly
My problem is as follows. When I print out all the values in the ArrayList, It is printing out much higher numbers e.g. 1234 = 49 50 51 52.
I think what is happening is that it is printing out the ASCII values rather than the numbers themselves. Can anyone spot where and why this is happening?
I have tried changing the int variable barcodeNumberAtI to a char, which yields the same result.
Apologies for lack of comments but this was only supposed to be a quick program
int tempNewDigit;
String barCode, ans;
int barcodeNumberAtI;
ArrayList <Integer> numbers = new ArrayList <Integer>();
public void addNumbers(){
Scanner s = new Scanner(System.in);
do{
System.out.println("Please enter a 12 digit barcode\n");
barCode = s.nextLine();
for(int i = 0; i < barCode.length(); i++){
barcodeNumberAtI = barCode.charAt(i);
System.out.println(barcodeNumberAtI);
numbers.add(barcodeNumberAtI);
}
System.out.print("Would you like to add another? y/n\n");
ans = s.nextLine();
} while (!ans.equals("n"));
}
public void displayNumbers(){
for(int i = 0; i < numbers.size(); i++){
System.out.print(numbers.get(i));
}
}
Happens at this line: barcodeNumberAtI = barCode.charAt(i);
barCode.charAt(i) returns a char which is converted to a int by using its ASCII value.
Use this instead:
barcodeNumberAtI = Character.digit(barCode.charAt(i), 10);
What Character.digit does is converting its first argument from the type char to the corresponding int in the radix specified by the second argument.
Here's a link to the documentation
This question already has answers here:
String's Maximum length in Java - calling length() method
(7 answers)
Closed 9 years ago.
these's something strangly i found that is when String length in java exceeding a value, then it shown odd.
there is my test code
public static void main(String[] args) {
int length = 4096;
char[] chars = new char[length];
for (char c : chars) {
c = 'c';
}
String str = new String(chars);
System.out.println(str.length());
System.out.println(str);
}
when i run above code on my computer, i get 4096 space character output from console.
then i change the length variable to 4095, this time output is correct, 4095 c character.
but on another computer, the output does not correct unless the length variable less than 2900.
i just can't figure out why?
EDIT
i think i figured out what's going on, i try to run it again in command window, even though the length value is big enough , it's print correct.
so it seemed like some limit about eclipse's console.
but i checked my eclipse console buffer size, its 800000
You are only changing the value of the local variable c and not the values of the entries in the char[].
To change the actual values, use a "regular" for loop, and not a for-reach loop:
for (int i = 0; i < chars.length; i++) {
chars[i] = 'c';
}
As a side note, a String in java is NOT a char[] - it's a String object.