简体   繁体   English

如何使用数字元素和空格分割字符串并将其存储到Java中的可索引数组中?

[英]How do split string with number elements and white space and store it into an indexable array in Java?

I am having some difficulty getting my input and getting it stored into an array. 我很难获得输入并将其存储到数组中。 I am making a stock exchange type program where a user will enter if they want to buy, sell, or calculate capital gain of their shares. 我正在进行证券交易所类型计划,如果用户想要买入,卖出或计算其股票的资本收益,他们将进入该计划。 So basically the input would be taken in using a scanner and storing it into a variable. 所以基本上输入将使用扫描仪并将其存储到变量中。 Then it would call a method, using the variable in the parameter and parsing it so that it can be split into 3 parts. 然后它将调用一个方法,使用参数中的变量并对其进行解析,以便将其拆分为3个部分。

My input will consist of a string element followed by integer type values. 我的输入将包含一个字符串元素,后跟整数类型值。 So this is what my input would look like when the program starts: 所以这是我的输入在程序启动时的样子:

B 20 300 B 20 300

This means that I will be buying 20 shares at $300. 这意味着我将以300美元的价格购买20股。 Is there a way for me to store all of this as first a string into an array and then converting the values at index 1 and 2 so that it will be an int? 有没有办法让我将所有这些作为第一个字符串存储到数组中然后转换索引1和2的值,以便它将是一个int? I would really appreciate some help so that I can get my input to start working. 我真的很感激一些帮助,以便我可以开始工作。 This is what I've been trying to do: 这就是我一直在努力做的事情:

import java.util.Scanner;
import java.util.StringTokenizer;

public class StockTran {
static String command = "";
String[] stockParts = null;
CircleArrayQueue Q = null;

public StockTran(String inputCommand) {
    try {
        this.stockParts = this.parseInput(command);
        System.out.println(stockParts[0]);
        System.out.println(stockParts[1]);
        System.out.println(stockParts[2]);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


private String[] parseInput(String inputLine) throws Exception {
    String[] temp = inputLine.split("\\s");
    return temp;
}

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);

    System.out.println("Enter 'B' to purchase share, 'S' to sell share, 'C' for capital gain, or 'Q' to quit: ");
    command = reader.next();

    StockTran tran = new StockTran(command);
}

} }

You need to read a complete line to pass to your constructor for splitting into elements: 您需要读取完整的行以传递给构造函数以分割为元素:

command = reader.nextLine();

as

command = reader.next(); 

will only read the first token. 只会读取第一个令牌。

import java.util.Scanner;
public class tcs {

/**
 * @param args
 */
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String customerDetails = scanner.nextLine();
    String string[]=customerDetails.split("\\s");
    for (int i = 0; i < string.length; i++) {
        System.out.println(string[i]);
    }
    //System.out.println(string[0]);
    //System.out.println(string[1]);
    //System.out.println(string[2]);
    int total=Integer.parseInt(string[1])*(Integer.parseInt(string[2]));
    System.out.println(total);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM