Save multiple array in a String [duplicate] - java

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.

Related

Wrong output- Converting Java String to Array. Numbers are correct before inserting into array? [duplicate]

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))

Using HashMap in Java to make a morse code [duplicate]

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().

What is the use of Java Char array? [duplicate]

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();

How to convert component of String[] or Object[] to other type [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Java - Convert integer to string [duplicate]
(6 answers)
Closed 8 years ago.
I have knowledge of wrapper class methods, but I want to know how to convert the component of String[] or Object[] to other type like int,float or Date format. for example
BufferedReader br = new BufferedReader(new FileReader(csvfile));
String curr;
while((scurr=br.readLine())!null) {
String[] input = scurr.split(",");
}
Now I want assign the component of String to primitive type(assume that my input string contains integer value)
but when I am trying to do
int i = input[0]
I am getting following suggestions:
1. change the type of i to String or
2. change the type of input to int
Is there any way to tackle the above scenario
edit:
I am really sorry guys, I really don't want ask duplicate questions, but after going through your answers and analyzing my scenario, I understood my mistake. So I would like to delete this post. How to do that without impacting the community please guild me
You can use Integer.parseInt() without an issue.
int i = input[0] // i is an int while input[0] is a String
Now
int i=Integer.parseInt(input[0])
will convert String to int
Integer.parseInt()

Is it possible to create variable names from characters in a string? [duplicate]

This question already has answers here:
Dynamic variable names Java
(3 answers)
Closed 9 years ago.
So say I have a string like "ABC", is there any possible way for me to pull out these characters individually and assign values to those as if they were a variable name?
For instances
String temp = "ABC"; //temp.charAt(1) being 'B' and assigning 5 to
int temp.charAt(1) = 5; //the variable name 'B'
Obviously that syntax is no where near correct, I'm just using it to explain what I'm trying to achieve.
Is this even a possible thought?
Sorry for the trivial question, I'm fairly new to java.
Use a data structure instead, like this:
Map<Character, Integer> vars = new HashMap<Character, Integer>();
String temp = "ABC";
vars.put(temp.charAt(1), 5); // B = 5
System.out.println(vars.get('B')); // 5
System.out.println(vars.get(temp.charAt(1))); // 5

Categories

Resources