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

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.

Related

Creating a simple calculator using strings in Java [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 1 year ago.
Improve this question
I'm trying to learn Java. My current assignment is to build a simple four function calculator..... this would be easy given if/else and/ or switch statements, but I'm supposed to build this using methods.
The original input has to be put in as a single string, so, in my mind, I'm going to have to take the single string and create substrings, then somehow convert these substrings into double values, while deleting whatever whitespace could possibly be between characters. My current idea is to somehow identify the "+,-,*, or /" within the string and divide into substrings before and after these values, using the appropriate defined method for whichever operator to do the calculations....
The problem is that I can't see a good way to divide these up into substrings or how to convert the numbers involved into double values. Anyone got any advice for me? Keep in mind, what we have gone through is pretty limited and I feel like I'm missing something REALLY simple out there.
You can split a string based on a particular character using str.split("\\+"), for example. You can convert the split pieces of the string to doubles by using Double.parseDouble(str);

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.

What is string frequency [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
Write a java code to check if the given string is even or not? Eg. aabbcc, aacbbc is even string.
I was asked this program in one interview. Actually i did not understand what is frequency here.
For a string s of length n, consider s[0] XOR s[1] ... XOR s[n - 1] where [i] is the (i)th letter of the string. Use java.lang.String#charAt(int) in java to extract a character.
If that is zero you have an even string, else you have an odd string.
Test n % 2 first for an immediate pay rise: If that is not zero then there must be at least 1 occurrence of a character that appears an odd number of times.
Normally folk who wrote computer games in machine code as kids in the 1980s will ask this question as it seems obvious to them. I doubt it is any more: XOR was a very fast way of writing sprite images.
Depending on what the interviewer was asking, string frequency is either,
how many times a string is found in another string.
how many times a character is found in a string.

Java - String to number [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 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")

How do you add JTextFields? [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 8 years ago.
Improve this question
I am making a GUI in Java, and I was wondering how do you add user inputs (integers) in a JTextField? If so, please post with some code so I can understand. (I am just a beginner programmer)
What all you need to do:
jTextField.setText(String.valueOf(intValue));
Because JTextField takes a String to set its value you have to convert the int to String.
See This for more details.
A JTextField contains text. You get this text using textField.getText(), which returns a String. This String might happen to represent a valid integer. If you want to get the value of the integer, you need to transform the String into an int. That's done using the Integer.parseInt() method:
int sum = Integer.parseInt(textField1.getText()) + Integer.parseInt(textField2.getText());
Of course, if one of the text fields contains text that doesn't represent an integer, you'll get an exception, as documented in the Integer.parseInt() javadoc.
Given your question, it seems you don't really understand basic notions such as types, and basic classes such as String and Integer. My advice would be to forget about Swing for the moment, and exercise with basic programs not involving any UI.

Categories