Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split an array of strings whose indexes contain integers and letters Java

Hi guys! =)
I’m new to Java, currently, I’m learning arrays and loops. I have an interesting homework task that I am very confused about.
And I don’t know what to do with this one. So I need your advice.

Create a public String getCheapStocks(String[] stocks) method in it. It takes an array of strings as input. Each line consists of the name of the product and its price, separated by a single space.

The method returns a string – a list of product names whose price is less than 200.
And the getCheapStocks(new String[] {"gun 500", "firebow 70", "pixboom 200"}) returns "firebow".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There is only for loop can be used.

I found a method that can split a string:

String text = "123 456"

String[] parts = text.split(" ")

int number1 = Integer.parseInt(parts[0]) //123

int number2 = Integer.parseInt(parts[1]) //456

But when I have String "gun 500" I can only split it in two String. And I can’t compare it to 200. My code is a mess and it does nothing.

I would really appreciate any tips or advice, thanks in advance!

public static String getCheapStocks(String[] stocks) {
    
    //MESS!
    int max = 200;
    
    for(int i = 0; i < stocks.length; i++) {
        String txt = stocks[i];
        String[] parts = txt.split(" ");
        int number1 = Integer.parseInt(parts[0]);
        int number2 = Integer.parseInt(parts[1]);
        
            if(number1 < max) { 
            
        }
    }
}

public static void main(String[] args) {

    //returns "firebow"
    System.out.println(getCheapStocks(new String[] {"gun 500", "firebow 70", "pixboom 200"}));
    
}

}

>Solution :

Since your input is in format of "<stock> <price>", after splitting this into 2 parts, you have to convert only the second part to an integer, otherwise you will get an exception.

public static String getCheapStocks(String[] stocks) {
    // Use a StringBuilder to hold the final result
    StringBuilder result = new StringBuilder();
    for (String stock : stocks) {
        String[] parts = stock.split(" ");
        // If the price is lower than 200, append part[0] (stock name) to the result
        if (Integer.parseInt(parts[1]) < 200) {
            result.append(parts[0]).append(" "); // Append also a space character for dividing the stocks
        }
    }
    // Strip the space from the end
    return result.toString().trim();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading