简体   繁体   English

如何在扫描仪中搜索并用空格分隔字符串?

[英]How can I search through a scanner and divide a String by spaces?

I am trying to construct a store by reading items from a given Scanner. 我试图通过从给定的扫描仪中读取项目来构建商店。 The constructor must repeatedly (until item name is *) read items from the given scanner object and add it to its inventory. 构造函数必须重复(直到项目名称为*)从给定的扫描器对象读取项目并将其添加到其清单中。

BreadLoaf 2.75 25 面包2.75 25

I need to divide a string like this into "Breadloaf" "2.75", and "25". 我需要将这样的字符串分为“面包”,“ 2.75”和“ 25”。 And then go to the next line and do the same thing until it reads "*" 然后转到下一行并执行相同的操作,直到显示“ *”

public class Store {
private ArrayList<Item> inventory;

// CONSTRUCTORS

/*
 * Constructs a store without any items in its inventory.
 */
public Store() {

}

/*
 * Constructs a store by reading items from a given Scanner. The constructor
 * must repeatedly (until item name is *) read items from the given scanner
 * object and add it to its inventory. Here is an example of the data (that
 * has three items) that could be entered for reading from the supplied
 * scanner:
 */
public Store(Scanner keyboard) {
    while(keyboard != null){

    }
}

Try below code. 尝试下面的代码。 it works I have recently checked. 我最近检查过的作品。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MyClient {

    public static void main(String args[]) {

        List<Item> inventory = new ArrayList<Item>();

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s1 = sc.nextLine();

            if (s1.equals("*")) {
                break;
            } else {
                Scanner ls = new Scanner(s1);
                while (ls.hasNext()) {
                    Item item = new Item(ls.next(), ls.nextFloat(), ls.nextInt());
                    inventory.add(item);
                }

            }
        }
        System.out.println(inventory);

    }
}

Now you need to create an Item.java below is Item .java 现在您需要创建一个Item.java,下面是Item .java

public class Item {
    private String name;
    private int quanity;
    private float price;

    public Item(String name, float price, int quanity) {
        this.name = name;
        this.price = price;
        this.quanity = quanity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getQuanity() {
        return quanity;
    }

    public void setQuanity(int quanity) {
        this.quanity = quanity;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Item [name=" + name + ", quanity=" + quanity + ", price="
                + price + "]";
    }




}

After typing all the inventory type "*"(star) at the end it will list all the entered Items . 在最后键入所有库存类型“ *”(星号)后,它将列出所有输入的物料。

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

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