Java - String to number [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
For example, "Hello" will be turned to a number and 10 will be turned to 10. If I will try again "Hello" I will get the same number I got before.
How can I do that?
Edit:
I don't know what "Hello" be turned to, because I don't have a program which does what I want. I don't want a specific number to be displayed.

You could use String::hashCode for Strings, and first try to parse to an integer to get numbers:
public static int convert(String str) {
try{
return Integer.parseInt(str.trim());
}catch(Exception e) {
return str.hashCode();
}
}
But if you have things like "10 10", you will still get the hash code and not just 1010

In case which word is assigned to a number, use dictionary.
For example you could use a hashmap, where for each string you would store the corresponding integer.
When you will want to get the corresponding integer, you would just seek the value of the string key (in your example "Hello")

Related

Multiplying a string containing numbers with another string containing same? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have a string
String string = "1.18";
I want to multiply the above string with another string that is
String f1= "2.54";
What would I have to do to multiply these two values? I looked this up online and came across a function called Integer.ParseInt() that converts the string to it's numerical values, so i tried
System.out.println(Integer.parseInt(string) * Integer.parseInt(f1));
It doesn't work. I want to know what am I doing wrong. What's the proper way of doing it? I would find it very helpful if someone could help me understand, thanks!
Try Double.parseDouble(string)
Integer is not for decimal number. Use Double.parseDouble() or use BigDecimal type if you need exact result.

I have a string with a number and a letter, is there a way to move the integer into a separate int variable? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So I'm working on a school project and it includes a Combined Gas Law calculator where the user may input the temperature in °celsius (e.g. 1°C) and the code converts it to kelvin; if the user's input is in kelvin already, it does not do that and continues with the equation. So does anyone know how I can separate the two data types into two different variables in Java?
You could split over a specific string (degree character for example), store in a String array and parse the first element. Something like this:
String str = "47°C"
String[] strArray = str.split("°");
int number = Integer.parseInt(strArray[0]);
Congratulations, you are working on something but not writing it's code. I can tell you a few tips about how to implement it's code.
You have a string that has some numbers in it, and also has unit. Try searching 'How to extract numbers from a string'. Now you have a number.
Find the unit in the string by looking the last character in the string.
If the condition is ok for your homework, calculate the new value.
Print the result.

how to get values from a Text view android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to store values from text view for example i am performing the following plus function like 5+6.Now i want to store 5 as value-1 and 6 as value-2.I only have one text view where i am displying like 5+6 in my project.I search a lot about my issue but could not found any proper solution. thanks
try this:
String string =text.getText().toString();
String[] separated = string.split("+");
String firstValue=separated[0];
String secondValue=separated[1];
You can try this and you will able to get the addition of two numbers at once. .
String[] getText = textview.getText().split("\\+");
int total=0;
for(String s:getText){
total+=Integer.parseInt(s);
}
in the end of iteration total will give the final result.From this method it gives the total of any numbers of numbers.

how do i compare each character of a same string using a for loop? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So I have to make a program where I have to compare each character of a string to see if they occur in the string more than once or not, if they occur more than once then I have to delete that string from an array list for instance "peer" and "pear" then it should remove "peer" because there are 2 e's in "peer", so how do I go about doing this?
Here is a hint. To check whether there is any duplicate character you can use a Set.
For each character of String
check if the char is in the set
if yes
then you can delete this String from arraylist and stop checking this string
else
add this char to the set
Try this: if(Pattern.matches("\w{2,}",ID)==true){//do what you want}. Then I wish you know how to remove the string.
Pythonic answer:
charlist=['a','b','c','d','e','f','g','h','i','j','k','p','r']
stringlist=['peer','pear']
for string in stringlist:
for character in charlist:
if string.count(character) > 1:
stringlist.remove(string)
print (stringlist)

How to manipulate unicode characters in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to replace "\u0023 \u0024 ab"
with:
"\\u0023 \\u0024 ab" to maintain its encoding before storing it in database.
There can me many different values after \u, not just 0023, 0024.
I tried using str.replace("\"\\"); or ("\u","\\u") but it doesn't work in java because it treats \u0023 as one character.
Any suggestions how to achieve this ?
I tried like this
public class test {
public static void main(String args[]) {
String s = "\u0023";
s = s.replace("\\", "\\\\");
System.out.println(s);
}
}
but it is giving following output as:
#
but I am expecting:
\\u0023
As said by the first answer here, you can retrieve the (numerical) unicode value of a character with:
// works up to Unicode 3.0
String hexString = Integer.toHexString(s | 0x10000).substring(1);
Using this number, simply print it out:
System.out.println("\u" + hexString);
(Note: code is untested: give me feedback if it doesn't work)
Hope this helps!
String s = "\\u0023";
s = s.replace("\\", "\\\\");

Categories