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

Time:04-25

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".

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"}));
    
}

}

CodePudding user response:

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();
}

CodePudding user response:

public static String getCheapStocks(String[] stocks) {
    int maxPrice = 200;
    List<String> results = new ArrayList<>();

    for (String txt : stocks) {
        String[] parts = txt.split(" ");
        String stockName = parts[0];
        int price = Integer.parseInt(parts[1]);

        if (price < maxPrice) {
            results.add(stockName);
        }
    }
    return String.join(" ", results);
}
  • Related