Need to split character numbers with comma and space using Java - java

Hi I am relatively new to Java. I have to compare amount value AED 555,439,972 /yr is lesser to another amount. so I tried to split using the code first
public static void main(String[] args) {
String value= "AED 555,439,972 /yr";
String[] tokens = value.split("\b");
int[] numbers = new int[tokens.length];
for (int i = 0; i < tokens.length; i++) {
numbers[i] = Integer.parseInt(tokens[i]);
}
System.out.println(numbers);
}
but I am getting Exception in thread "main" java.lang.NumberFormatException: For input string: "AED 555,439,972 /yr".
Appreciate if someone can help me to solve the problem.

Hope that you need to get the numeric value from the string.
First, use the following to remove all non-digit characters.
value.replaceAll("\\D", "")
\\D stands for non-digit character. Once every such character is replaced with empty string (which means those are removed), use Integer.parseInt on it. (Use Long.parseLong if the values can be out of Integer's range.)
In your code, you are trying to split the string by word character ends (which too is not done correctly; you need to escape it as \\b). That would give you an array having the result of the string split at each word end (after the AED, after the space following AED, after the first 3 digits, after the first comma and so on..), after which you are converting each of the resulting array components into integers, which would fail at the AED.
In short, the following is what you want:
Integer.parseInt(value.replaceAll("\\D", ""));

There are a few of things wrong with your code:
String[] tokens = value.split("\b");
The "\" needs to be escape, like this:
String[] tokens = value.split("\\b");
This will split your input on word boundaries. Only some of the elements in the tokens array will be valid numbers, the others will result in a NumberFormatException. More specifically, at index 2 you'll have "555", at index 4 you'll have 439, and at index 6 you'll have 972. These can be parsed to integers, the others cannot.

I found a solution from stack overflow itself
public static void main(String[] args) {
String line = "AED 555,439,972 /yr";
String digits = line.replaceAll("[^0-9]", "");
System.out.println(digits);
}
the output is 555439972

You are going about it the wrong way. It's a single formatted number, so treat it that way.
Remove all non-digit characters, then parse as an integer:
int amount = Integer.parseInt(value.replaceAll("\\D", ""));
Then you'll have the number of dirhams per year, which you can compare to other values.

Related

Efficiently split large strings in Java

I have a large string that should be split at a certain character, if it is not preceded by another certain character.
Would is the most efficient way to do this?
An example: Split this string at ':', but not at "?:":
part1:part2:https?:example.com:anotherstring
What I have tried so far:
Regex (?<!\?):. Very slow.
First getting the indices where to split the string and then split it. Only efficient if there are not many split characters in the string.
Iterating over the string character by character. Efficient if there are not many protect characters (e.g. '?').
I fear you would have to go through the string and check if a ":" is preceded by a "?"
int lastIndex=0;
for(int index=string.indexOf(":"); index >= 0; index=string.indexOf(":", lastIndex)){
if(index == 0 || string.charAt(index-1) != '?'){
String splitString = string.subString(lastIndex, index);
// add splitString to list or array
lastIndex = index+1;
}
}
// add string.subString(lastIndex) to list or array
You will have to test this very carefully (since I didn't do that), but using a regular expression in the split() might produce the results you want:
public static void main(String[] args) {
String s = "Start.Teststring.Teststring1?.Teststring2.?Teststring3.?.End";
String[] result = s.split("(?<!\\?)\\.(?!\\.)");
System.out.println(String.join("|", result));
}
Output:
Start|Teststring|Teststring1?.Teststring2|?Teststring3|?.End
Note:
This only considers your example about splitting by dot if the dot is not preceded by an interrogation mark.
I don't think you will get a much more performant solution than the regex...

How to return only first n number of words in a sentence Java

Say i have a simple sentence as below.
For example, this is what have:
A simple sentence consists of only one clause. A compound sentence
consists of two or more independent clauses. A complex sentence has at
least one independent clause plus at least one dependent clause. A set
of words with no independent clause may be an incomplete sentence,
also called a sentence fragment.
I want only first 10 words in the sentence above.
I'm trying to produce the following string:
A simple sentence consists of only one clause. A compound
I tried this:
bigString.split(" " ,10).toString()
But it returns the same bigString wrapped with [] array.
Thanks in advance.
Assume bigString : String equals your text. First thing you want to do is split the string in single words.
String[] words = bigString.split(" ");
How many words do you like to extract?
int n = 10;
Put words together
String newString = "";
for (int i = 0; i < n; i++) { newString = newString + " " + words[i];}
System.out.println(newString);
Hope this is what you needed.
If you want to know more about regular expressions (i.e. to tell java where to split), see here: How to split a string in Java
If you use the split-Method with a limiter (yours is 10) it won't just give you the first 10 parts and stop but give you the first 9 parts and the 10th place of the array contains the rest of the input String. ToString concatenates all Strings from the array resulting in the whole input String. What you can do to achieve what you initially wanted is:
String[] myArray = bigString.split(" " ,11);
myArray[10] = ""; //setting the rest to an empty String
myArray.toString(); //This should give you now what you wanted but surrouned with array so just cut that off iterating the array instead of toString or something.
This will help you
String[] strings = Arrays.stream(bigstring.split(" "))
.limit(10)
.toArray(String[]::new);
Here is exactly what you want:
String[] result = new String[10];
// regex \s matches a whitespace character: [ \t\n\x0B\f\r]
String[] raw = bigString.split("\\s", 11);
// the last entry of raw array is the whole sentence, need to be trimmed.
System.arraycopy(raw, 0, result , 0, 10);
System.out.println(Arrays.toString(result));

Searching for an int inside a string user input

I am currently doing an assignment for class and I would like to know how to find an integer inside a string input. So far in the code I have created a way to exit the loop. Please don't give me code just give me some ideas. Keep in mind that I am quite new to java so please bear with me. Thanks.
EDIT: I forgot to mention that the string input should be like "woah123" and it should find only the "123" portion. Sorry
import java.util.Scanner;
public class DoubleTheInt
{
public static void main(String[] args)
{
int EXIT = 0;
while(EXIT == 0)
{
Scanner kbReader = new Scanner(System.in);
System.out.println("What is your sentence?");
String sentence = kbReader.next();
if(sentence.equalsIgnoreCase("exit"))
{
break;
}
}
}
}
For learning purpose what you can do is traverse the whole string and check only for the digits. In this case you will also learn how to check char-by-char in a string if in future you may require this also you will get the digits of that string. Hope that solves your problem.
Here's what you do...
Replace all non numeric characters with empty string using \\D and String.replaceAll function
Parse your string (after replacing) as integer using Integer.parseInt()
Edited after Christian's comment :
replaceAll() function replaces occurances of particular String (Regex is first argument) with that of the second argument String..
\\D is used to select everything except the numbers in the String. So, the above 2 lines combined will give "1234" if your String is "asas1234" .
Now , Integer.parseInt is used to convert a String to integer.. It takes a String as argument and returns an Integer.
Since you are not asking for code , I am giving you some suggestions.
Use regex in string method to find the numbers and to remove all the
non numbers.
Parse the string to integer.
Unless your assignment "is" a regex assignment, I suggest you do it the non-regex way. i.e, by reading character by character and checking for integers or reading the string and converting to a char array and process.
I'm not sure what your teacher intends you to do, but there are two ways-
Read character by character and filter the numbers by their ASCII code. Use BuffferedReader to read from standard input. And use read() method. Figure out the ASCII code range for numbers by experimenting.
Read the entire String at once(using either Scanner or BufferedReader) and see what you can do from the String API(as in the methods available for String).
Use Regular Expression : \d+
String value = "abc123";
Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher(value);
int i = Integer.valueOf(m.group(1));
System.out.println(i);
output
123

About splits in Java

I'm a Java beginner, so please bear with me if this is an extremely easy answer.
Say I have code that looks like this:
String str;
String [] splits;
str = "The words never line up in such a way ";
splits = str.split(" ");
for (int i = 0; i < splits.length; i++)
System.out.println(splits[i]);
What does Java do at the end of the string? After "way" there is a space; since there is no value after the space does Java decide not to split again?
Thanks so much!
According to the Java documentation for split(), http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String),
The split(String r) is equivalent to the split(String r, 0) method, which will ignore and not include any blank trailing empty strings. Specifically from the docs:
"Trailing empty strings are therefore not included in the resulting
array."
So the last element in the array after the split will be "way"
You can confirm this by executing the code you mentioned.
You will not get any trailing space after delimiter if you use split method. Example
class Main
{
public static void main (String[] args)
{
String str;
String [] splits;
str = "The words never line up in such a way "; // some empty string after delimiter at end
splits = str.split(" ");
for (int i = 0; i < splits.length; i++)
System.out.println(splits[i]);
System.out.println("END");
}
}
OUTPUT
The
words
never
line
up
in
such
a
way
END
see no splitted string for end delimiters.
Now
class Main
{
public static void main (String[] args)
{
String str;
String [] splits;
str = "The words never line up in such a way yeah";
splits = str.split(" ");
for (int i = 0; i < splits.length; i++)
System.out.println(splits[i]);
System.out.println("END");
}
}
OUTPUT
The
words
never
line
up
in
such
a
way
yeah
END
see an extra string after delimiter which is also a empty string but not the trailing, so it will be in the array.
I´ve been looking at javadoc and here what it says about String.split:
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
It seems that this method calls .split with two arguments:
The limit parameter controls the number of times the pattern is
applied and therefore affects the length of the resulting array. If
the limit n is greater than zero then the pattern will be applied at
most n - 1 times, the array's length will be no greater than n, and
the array's last entry will contain all input beyond the last matched
delimiter. If n is non-positive then the pattern will be applied as
many times as possible and the array can have any length. If n is zero
then the pattern will be applied as many times as possible, the array
can have any length, and trailing empty strings will be discarded.
thanks

How to detect if a string input has more than one consecutive space?

For a class I have to make a morse code program using a binary tree. The user is suppose to enter morse code and the program will decode it and print out the result. The binary tree only holds A-Z. And I only need to read dashes, dots, and spaces. If there is one space that is the end of the letter. If there is 2 or more spaces in a row that is the end of the word.
How do you detect if the string input has consecutive spaces? Right now I have it programmed where it detects if there is 2 (which will then print out a space), but i dont know how to have it where it knows there is 3+ spaces.
This is how I'm reading the input btw:
String input = showInputDialog( "Enter Code", null);
character = input.charAt(i);
And this is how I have it detecting a space: if (character == ' ').
Can anyone help?
Well, you could do something like this which if you had more than one item in the resulting array would tell you that you had at least one instance of 2+ spaces.
String[] foo = "a b c d".split(" +");
This splits into "a b", "c", and "d".
You'd probably need regex checks than just that though if you need to detect how many of each count of spaces (e.g. how many 2 spaces, how many 3 spaces, etc).
Note I have made an assumption that you are retrieving the full morse code message in one go and not one character at a time
Focusing on this point:
"If there is one space that is the end of the letter. If there is 2 or more spaces in a row that is the end of the word."
Personally, I'd use the split() method on the String class. This will split up a String into a String[] and then you can do some checks on the individual Strings in the array. Splitting on a space character like this will give you a couple of behavioural advantages:
Any strings that represent characters will have no trailing or leading spaces on them
Any sequences of multiple spaces will result in empty strings in the returned String[].
For example, calling split(" ") on the string "A B C" would give you a String[] containing {"A", "B", "", "C"}
Using this, I would first check if the empty string appeared at all. If this was the case, it implies that there were at least 2 space characters next to each other in the input morse code message. Then you can just ignore any empty strings that occur after the first one and it will cater for any number of sequential empty strings.
Without wanting to complete your assignment for you, here is some sample code:
public String decode(final String morseCode) {
final StringBuilder decodedMessage = new StringBuilder();
final String[] splitMorseCode = morseCode.split(" ");
for (final String morseCharacter : splitMorseCode) {
if( "".equals(morseCharacter) ) {
/* We now know we had at least 2 spaces in sequence
* So we check to see if we already added a space to spearate the
* resulting decoded words. If not, then we add one. */
if ( !decodedMessage.toString().endsWith(" ") ) {
decodedMessage.append(" ");
}
continue;
}
//Some code that decodes your morse code character.
}
return decodedMessage.toString();
}
I also wrote a quick test. In my example I made "--" convert to "M". Splitting the decodedMessage on the space character was a way of counting the individual words that had been decoded.
#Test
public void thatDecoderCanDecodeMultipleWordsSeparatedByMultipleSpaces() {
final String decodedMessage = this.decoder.decode("-- -- -- -- -- -- -- -- -- -- -- -- -- --");
assertThat(decodedMessage.split(" ").length, is(7));
assertThat(decodedMessage, is("MM MM MM MM MM MM MM"));
}
Of course, if this is still not making sense, then reading the APIs always helps
To detect if a String has more than one space:
if (str.matches(".* .*"))
This will help.,
public class StringTester {
public static void main(String args[]){
String s="Hello ";
int count=0;
char chr[]= s.toCharArray();
for (char chr1:chr){
if(chr1==' ')
count++;
}
if(count>=2)
System.out.println(" I got more than 2 spaces") ;
}

Categories