Dylan Schroder
Dylan Schroder

Reputation: 41

changing a string of numbers into an int from a part of a parsed string

So if I were given a string "Cylinder/soda200/0.0054/5/10" and were to parse it, how would I actually take the parts with just numbers and turn them into an int or double?

Currently I am parsing it by doing this:

    String delims="[/]+";
    String[] drinkParse=lineToParse.split(delims);

Upvotes: 1

Views: 86

Answers (4)

msmolcic
msmolcic

Reputation: 6557

I'm not sure if you're familiar with StringBuilder in java, but you can learn more about it here and you can also take a look at String API here. In the program example below I'm reading character by character from the string you posted until program runs into '/' or unless it's the end of the string. All characters are saved to the StringBuilder and converted into a String after to check if it's a number, either double or integer. If any character in the string is not a number or period isNumber flag value is set to false and we don't print it. In case it's a period we set isDouble flag value to true because we need that information for further conversion. At last we print the numbers if all conditions are true and create new StringBuilder in order to start building a fresh new String.

public class myProject {

    public static void main(String[] args) {

        String str = "Cylinder/soda200/0.0054/5/10";
        StringBuilder sb = new StringBuilder();
        boolean isDouble;
        boolean isNumber;

        System.out.println("List of numbers:");
        for (int i = 0; i < str.length(); i++) {

            if (str.charAt(i) != '/') {
                sb.append(str.charAt(i));
            }

            if (str.charAt(i) == '/' || i+1 == str.length()) {

                String strConvert = sb.toString();
                isDouble = false;
                isNumber = true;

                for (int j = 0; j < strConvert.length(); j++) {
                    if (!Character.isDigit(strConvert.charAt(j))) {
                        if (strConvert.charAt(j) == '.') {
                            isDouble = true;
                        } else {
                            isNumber = false;
                        }                        
                    }
                }

                if (isDouble && isNumber) {
                    System.out.println(Double.parseDouble(strConvert));
                } else if (isNumber) {
                    System.out.println(Integer.parseInt(strConvert));
                }

                sb = new StringBuilder();

            }
        }
    }
}

Hopefully this will help you or anyone else with similar problem!

Upvotes: 0

vbazaga86
vbazaga86

Reputation: 156

You should use split function and parse like this

Object[] values = (Object[]) var.split("/");
values[2]=Double.parseDouble(values[2]);
values[3]=Integer.parseInt(values[3]);
values[4]=Integer.parseInt(values[4]);
System.out.println(values);

You can also iterate array and check if string validates against rege/p of double or int and then do corresponding conversion if you dont know positions.

Upvotes: 0

John3136
John3136

Reputation: 29265

Use Integer.parseInt(), Double.parseDouble() etc, passing your string as an argument.

e.g. int i = Integer.parseInt(drinkParse[3]);

Note that these can throw a NumberFormatException if the argument can't be parsed as requested. (e.g. "soda200" can't parse straight to an int)

Upvotes: 2

Carlos Bribiescas
Carlos Bribiescas

Reputation: 4407

You can use new Integer("22") and save it to an int in order to convert it. Same for Double. There are other ways too though, I suggest simple googling.

Upvotes: 2

Related Questions