how to read and extract an integer from a string in java - java

Given a string
7 + 45 * 65
How to check whether a given character of this string is an integer and then store the whole integer value in an integer variable?
E.g. for 65, check if 6 is an integer, if yes, then store 65 in another integer variable. You can assume that the string can be converted into a character array.

Given that this looks like homework below are some tips for a simple way to parse and store each integer value.
Check out the API documentation for the Character class. This will contain methods for determining whether a character is a digit.
Consider using a StringBuilder to store the intermediate numerical result as you read in each digit of the number.
Check the Integer class API for methods to help with parsing the String value (stored within your StringBuilder) and turning it into an int.
Finally, consider using a List (e.g. LinkedList) to store the int value.

For a quick and dirty soluiton I would use StringTokenizer and try { Integer.parseInt() } catch (NumberFormatException){}

Check out this post on exactly the same topic:
Java Programming - Evaluate String math expression
It looks like BeanShell has the cleanest method to do what you need. You could also try the JavaScript Engine method (although BeanShell looks much cleaner to me).

Easiest solution would be to use java.util.Scanner. You can set Scanner.useDelimeter("\\D+") which will mean skip any non-digit characters, and then call Scanner.nextInt() to get next Integer from the String.
If you want to work with characters, then use Character.isDigit(char c).

Related

How to split string if splitting character is dynamic or unknown?

I want to make a Java program in which I want to take a String as a input. The string will have two integer numbers and operation to be performed.
eg. 25+85
or 15*78
The output will the solution of the string.
But I don't know how to split the string because operator sign is not known before execution.
You would want to check what operation it is using by using String.contains("+"); and checking all the other operators you want to support. Then split wherever that operator is, String.split("+"). From there parse the output of String.split("+") by using Integer.parseInt(String s) and then return the sum. Pretty simple, good luck.
You can use the split() method of the String class to split the input at non-digit characters:
input.split("\\D");
This will give you an array containing only the numbers.
I guess you also want to get the operator somehow? Although it's not the most elegant way, you might want to start with input.replaceAll("[^\\*\\+\\-\\/]", "") to remove everything that's not an operator, but you will still have to do some careful input filtering. What if i type 5+4*6 oder 2+hello ?

How do I change the value in "Xxxxx" format even if user inputs like this "xXXxX"? Java

I have a thought:(might too completed)
I wonder if I can substring(1) then use .toUpperCase() for the
string
and after that use getLength() to have the length then
.toLowerCase() the rest in the string
Is there a easier way to do this? So I can have the value stored in "Xxxx" format.
Thank you.
For example:
No matter user input the value as "hELlo" or "HEllO" , the system always store the value as "Hello". That's may explain my question.
I wrote a small utility for my self. It might help you. I was sure that my string would always have alphabets in it so did not care about any other characters. you might want to modify as per your needs.
int length = "yourstring".length();/// get the length of the string
String camelCase = removeCharacters.substring(0, 1).toUpperCase();// upper case the first alphabet
camelCase = camelCase + "yourstring".substring(1, length).toLowerCase();// lowercase all other alphabets
Substring has a one-arg version and a two-arg version. The second argument is non-inclusive. So, the first bit of the string you want is
myString.subString(0, 1)
and the second bit is
myString.subString(1)
You can then call the toUpper and toLower methods on the results, and concatenate them.
Yes, the Apache Commons-Lang project has a WordUtils class with a capitalize method, as in the following example:
WordUtils.capitalize("i am FINE") yields: "I Am FINE"

Can a special character can be taken in a character variable with an alphabet?

I want to take input in a character variable which is A#.
Is it possible to do that?
Example:
char E[]={'E','F#','G#','A','B','C#','D#'};
To solve this I have taken array type as string. Which is giving me problem to get it's ascii to calculate hash value and also with respect to sorting as well.
No, you should use String to store these since there are more than 1 character. A char can only have 1 character. You can use the default hashCode() implementation of String for hash value and the default compareTo() for sorting.
You can write any character, include specials characters... For example 'Ñ'. But you can't write 'F#' in a char.
You can't store two characters in one char variable. Remember that 'F#' isn't a char, it's a String ! You should use String to store them. Then you can use compareTo() method for check if two strings are equal or not. Check This out

Using Java, how do I accept and categorize a phrase that has numbers and letters in user input?

I'm writing an application that I'd like to be able to accept and hold the user's input of a combination of numbers and letters (06712A1, for instance), and then input that info into an array. I assume I can't use Integer since there are letters inside it. How do I do this?
Use a String and then validate it by examining all the letters it contains.
What do you mean by "input that info into an array"? Put each character into a separate position in an array?
You need to use the String datatype to represent a sequence of characters.
This tutorial on Strings in java might be useful.
Your question is ambiguous.
I assume you mean that you want to use that String as an index to another object. In that case, search for the java.util.Map subclass that suits you.

Help converting input string into Unicode integers in Java

I've been set an assignment which requires me to capture the input of 5 strings, convert them to uppercase, output them as a string, convert them to their Unicode integers (using the getNumericValue method) and then manipulate the integers using some basic operators.
I get the first part but I am having trouble with the following:
Using the getNumericValue to convert
my single character literal strings
into their Unicode integer
counterparts.
Being able to assign these ints to
variables so I can further process s
them with operators, all the
examples I have seen have been
simple printing out the number and
not assigning it to a variable,
since I am a Java noob the syntax is
still a little confusing for me.
My code is here
If there is a cleaner way of doing what I want please suggest so but without the use of arrays or loops.
I don't understand why you don't want to do this without arrays or loops.
You can get the unicode values (as ints) making up the string via String.codePointAt(), or get the characters via charAt() followed by a getNumericValue() for each character. But regardless, you're going to have to iterate over the set of characters in the string via a loop, or perhaps recursion.
Yes, sounds like op needs to continue researching avenues of learning.
// difficult to code without interfering with
// instructor's prerogative
char ( array ) = String.getchars();
// Now what, unroll the loop?
...
As noted by Brian, loops are fundamental. I cannot imagine getChars being assigned before simple array techniques.

Categories