Amirates
Amirates

Reputation: 63

breaking down any String

Hi guys I am busy with breaking / splitting Strings. However the String is not fixed so when the input changes the program still has to work with any character input.

Till now I got this far but I got lost.

I have made an array of characters and set the size of the array equal to the lenght of any string that is will get as input. I made a for loop to loop through the characters of a string.

how do I insert my string now into the array because I know that my string is not yet in there? Then when its finally looping through the characters of my string is has to printout numbers and operands on different lines. So the ouput would look like in this case like this;

1

+

3

,

432

.

123

etc

I want to do this without using matchers,scanner, etc. I want to use basic Java techniques like you learn in the first 3 chapters of HeadfirstJava.

public class CharAtExample {

public static void main(String[] args) {

    // This is the string we are going to break down
    String inputString = "1+3,432.123*4535-24.4";

    int stringLength = inputString.length();    

    char[] destArray = new char[stringLength];{

        for (int i=0; i<stringLength; i++);
    }

Upvotes: 0

Views: 228

Answers (4)

Bloodeye
Bloodeye

Reputation: 94

It's easier than you might think.

First: to get an array with the chars of your string you just use the toCharArray() method that all strings have.
ex. myString.toCharArray()

Second: When you see that a character is not a number, you want to move to the next line, print the character and then move to the next line again.

The following code does exactly that :

public class JavaApplication255 {


public static void main(String[] args) {

    String inputString = "1+3,432.123*4535-24.4";   

    char[] destArray = inputString.toCharArray();

    for (int i = 0 ; i < destArray.length ; i++){
        char c = destArray[i];

        if (isBreakCharacter(c)){
            System.out.println("\n" + c);
        } else {
            System.out.print(c);
        }

    }
}

public static boolean isBreakCharacter(char c){
    return c == '+' || c == '*' || c == '-' || c == '.' || c == ',' ;
} 

Upvotes: 1

davidxxx
davidxxx

Reputation: 131386

You could use Character.isDigit(char) to distinguish numeric and not numeric chars as actually this is the single criteria to group multiple chars in a same line.
It would give :

public static void main(String[] args) {

    String inputString = "1+3,432.123*4535-24.4";
    String currentSequence = "";

    for (int i = 0; i < inputString.length(); i++) {

        char currentChar = inputString.charAt(i);
        if (Character.isDigit(currentChar)) {
            currentSequence += currentChar;
            continue;
        }
        System.out.println(currentSequence);
        System.out.println(currentChar);
        currentSequence = "";
    }

    // print the current sequence that is a number if not printed yet
    if (!currentSequence.equals("")) {
         System.out.println(currentSequence);
    }

}

Character.isDigit() relies on unicode category.
You could code it yourself such as :

if (Character.getType(currentChar) == Character.DECIMAL_DIGIT_NUMBER) {...}

Or you could code it still at a lower level by checking that the int value of the char is included in the range of ASCII decimal values for numbers:

if(currentChar >= 48 && currentChar <= 57 ) {

It outputs what you want :

1

+

3

,

432

.

123

*

4535

-

24

.

4

Upvotes: 2

RAZ_Muh_Taz
RAZ_Muh_Taz

Reputation: 4099

Here is a possible solution where we go character by character and either add to an existing string which will be our numbers or it adds the string to the array, clears the current number and then adds the special characters. Finally we loop through the array as many times as we find a number or non-number character. I used the ASCII table to identify a character as a digit, the table will come in handy throughout your programming career. Lastly I changed the array to a String array because a character can't hold a number like "432", only '4' or '3' or '2'.

String inputString = "1+3,432.123*4535-24.4";

int stringLength = inputString.length();    

String[] destArray = new String[stringLength];
int destArrayCount = 0;
String currentString = "";
for (int i=0; i<stringLength; i++)
{
    //check it's ascii value if its between 0 (48) and 9 (57)
    if(inputString.charAt(i) >= 48 && inputString.charAt(i) <= 57 )
    {
        currentString += inputString.charAt(i);
    }
    else
    {
        destArray[destArrayCount++] = currentString;
        currentString = "";
        //we know we don't have a number at i so its a non-number character, add it
        destArray[destArrayCount++] = "" + inputString.charAt(i);
    }
}
//add the last remaining number
destArray[destArrayCount++] = currentString;

for(int i = 0; i < destArrayCount; i++)
{
    System.out.println("(" + i + "): " + destArray[i]);
}

IMPORTANT - This algorithm will fail if a certain type of String is used. Can you find a String where this algorithm fails? What can you do to to ensure the count is always correct and not sometimes 1 greater than the actual count?

Upvotes: 1

tony
tony

Reputation: 1651

char[] charArray = inputString.toCharArray();

Upvotes: 1

Related Questions