如何拆分索引包含整数和字母的字符串数组 Java
How to split an array of strings whose indexes contain integers and letters Java
大家好! =)
我是 Java 的新手,目前正在学习数组和循环。我有一个有趣的家庭作业任务,我很困惑。
我不知道如何处理这个。所以我需要你的建议。
在其中创建一个 public String getCheapStocks(String[] stocks) 方法。它需要一个字符串数组作为输入。每行包含产品名称及其价格,由 space.
分隔
方法returns 一个字符串 - 价格低于 200 的产品名称列表。
getCheapStocks(new String[] {"gun 500", "firebow 70", "pixboom 200"}) returns "firebow".
只有for循环可以使用
我找到了一个可以拆分字符串的方法:
字符串文本 = "123 456"
字符串[]部分=text.split(" ")
int number1 = Integer.parseInt(parts[0]) //123
int number2 = Integer.parseInt(parts[1]) //456
但是当我有字符串“gun 500”时,我只能将它分成两个字符串。而且我无法将它与 200 进行比较。我的代码一团糟,什么也没做。
我非常感谢任何提示或建议,提前致谢!
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"}));
}
}
由于您的输入格式为"<stock> <price>"
,将其拆分为两部分后,您只需将第二部分转换为整数,否则会出现异常。
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();
}
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);
}
大家好! =) 我是 Java 的新手,目前正在学习数组和循环。我有一个有趣的家庭作业任务,我很困惑。 我不知道如何处理这个。所以我需要你的建议。
在其中创建一个 public String getCheapStocks(String[] stocks) 方法。它需要一个字符串数组作为输入。每行包含产品名称及其价格,由 space.
分隔方法returns 一个字符串 - 价格低于 200 的产品名称列表。 getCheapStocks(new String[] {"gun 500", "firebow 70", "pixboom 200"}) returns "firebow".
只有for循环可以使用
我找到了一个可以拆分字符串的方法:
字符串文本 = "123 456"
字符串[]部分=text.split(" ")
int number1 = Integer.parseInt(parts[0]) //123
int number2 = Integer.parseInt(parts[1]) //456
但是当我有字符串“gun 500”时,我只能将它分成两个字符串。而且我无法将它与 200 进行比较。我的代码一团糟,什么也没做。
我非常感谢任何提示或建议,提前致谢!
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"}));
}
}
由于您的输入格式为"<stock> <price>"
,将其拆分为两部分后,您只需将第二部分转换为整数,否则会出现异常。
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();
}
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);
}