【问题标题】:Saving Data + String保存数据+字符串
【发布时间】:2013-11-30 15:37:33
【问题描述】:

我正在尝试保存和加载数据,但我感到很沮丧,需要好好睡一觉。如果没有错误或空值,我似乎无法弄清楚这一点。

我的代码:

public class Offers {

public static List<Offer> offers = new ArrayList<Offer>(2000);

public Offers() {
}

public static void saveOffers() {
        for (Offer offer : offers) {
            if (offer == null) {
                continue;
            }
            int id = offer.id;
            int item = offer.item;
            int amount = offer.amount;
            int price = offer.price;
            int currentAmount = offer.currentAmount;
            int removedAmount = offer.removedAmount;
            int type = offer.type;
            int completed = offer.completed ? 1 : 0;
            int aborted = offer.aborted ? 1 : 0;
            int currentPrice = offer.currentPrice;
            int slot = offer.slot;
            String owner = offer.owner;
            String VALUES(""+ id + "" + item + "" + amount + "" + price + "" + currentAmount + "" + removedAmount + "" + type + "" + completed + "" + aborted + "" + currentPrice + "" + slot + "" + owner + "");
        }
    }
}

public static void load() {
    BufferedReader stream = new BufferedReader(new InputStreamReader(
            new FileInputStream("GEoffers.txt")));
            int id = stream.readShort();
            int item = stream.read();
            int amount = stream.read();
            int price = stream.read();
            int currentAmount = stream.read();
            int removedAmount = stream.read();
            int type = stream.readShort();
            boolean completed = stream.readShort() == 1;
            boolean aborted = stream.readShort() == 1;
            int currentPrice = stream.read();
            int slot = stream.readShort();
            String owner = stream.readString();
            if (id != -1)
                offers.add(new Offer(id, item, amount, currentAmount, removedAmount, price, type, owner, completed, slot));
            for (Offer l : offers) {
                if (l == null) {
                    continue;
                }
                if (l.id == id && l.item == item && l.amount == amount && l.currentAmount == currentAmount && l.price == price && l.type == type && l.owner.equals(owner) && l.slot == slot) {
                    l.currentPrice = currentPrice;
                    l.aborted = aborted;
                    break;
                }
            }
        }
    }

我在同一方法中尝试做的是将所有内容保存到一个字符串中,因此当调用加载方法时,它将加载并将变量设置为保存在该字符串中的内容。 例如另存为""+ id + "" + item + "" + amount + "" + price + "" + currentAmount + "" + removedAmount + "" + type + "" + completed + "" + aborted + "" + currentPrice + "" + slot + "" + owner + "" 并在将变量设置为保存时相同时加载。

我已经完成了这项工作,我即将支付 5 美元之类的费用来为我做这件事。只是寻求一些帮助,或者是否有人可以善意地修复我的垃圾工作。 ++*10 求帮助。

按要求提供课程:

public class Offer {

public int id = -1;
public int item = 0;
public int amount = 0;
public int currentAmount = 0;
public int removedAmount = 0;
public int price = 0;
public int type = 0;
public String owner = "";
public boolean completed = false;
public int currentPrice = 0;
public boolean aborted = false;
public int slot = 0;
public boolean itemsLeft = false;

/**
 * Creates a new global offer.
 * 
 * @param id
 * @param item
 * @param amount
 * @param currentAmount
 * @param removedAmount
 * @param price
 * @param type
 * @param owner
 * @param completed
 * @param slot
 */
public Offer(int id, int item, int amount, int currentAmount, int removedAmount, int price, int type, String owner, boolean completed, int slot) {
    this.id = id;
    this.item = item;
    this.amount = amount;
    this.currentAmount = currentAmount;
    this.removedAmount = removedAmount;
    this.price = price;
    this.type = type;
    this.owner = owner;
    this.completed = completed;
    this.slot = slot;
    this.itemsLeft = false;
}

public void updatePrice(int amount, boolean bool) {
    if (bool) {
        currentPrice += amount;
    } else {
        currentPrice -= amount;
    }
}

public void clear() {
    id = -1;
    item = 0;
    amount = 0;
    currentAmount = 0;
    price = 0;
    type = -1;
    owner = "";
    completed = false;
    currentPrice = 0;
    aborted = false;
    itemsLeft = false;
}

public void delete() {
    Offers.offers.remove(this);
}

}

【问题讨论】:

  • 到底是什么问题?
  • 这不是一个可以帮你做功课的网站。告诉我们您遇到了什么错误。
  • 您尚未将任何Offers 添加到列表中
  • 请同时发布Offer课程
  • 按要求发布报价类

标签: java string inputstream outputstream


【解决方案1】:

也许你想要这样的东西

public class Offers {

    public static List<Offer> offers = new ArrayList<Offer>(2000);

    public Offers() {
    }

    public void saveOffers(int id, int item, int amount, int currentAmount, 
                           int removedAmount, int price, int type, String owner, 
                           boolean completed, int slot) {

        Offer offer = new Offer(id, item, amount, currentAmount, removedAmount, 
                                price, type, owner, completed, slot)

       offers.add(offer);
   }
}

您尚未将任何Offers 添加到offers 列表中。所以你不能改变价值观。这就是为什么你得到空值。 ArrayList&lt;&gt;(2000) 不添加 2000 个 Offer,只是创建 2000 个容量。根据您定义 Offer 类的方式以及它的构造函数和 getter 方法(如果有),您可能想要上面的代码。

运行它

public class TestOffers {
    public static void main(String[] args){
        Offers offers = new Offers();

        int id  = 1;
        int item = 1;
        int amount = 1;
        int currentAmount = 1; 
        int removedAmount = 1;
        int price = 10;
        int type =- 1;
        String owner = "Me";
        boolean completed = true;
        int slot =1;

        offers.saveOffers(id, item, amount, currentAmount, removedAmount, 
                                          price, type, owner, completed, slot);

        for (Offer offer : offers){
            System.out.println(offer);
        }
    }
}

您还需要覆盖 Offer 类中的 toString() 方法,以便在您想要打印 offer 时获得所需的输出

public class Offer {
   ...

   @Override
   public String toString(){
       return id + " " + item + " " + amount + " " + price + " " 
              + currentAmount + " " + removedAmount + " " + type + " " 
              + completed + " " + aborted + " " + currentPrice + " " 
              + slot + " " + owner;
   }
}

【讨论】:

  • 我需要将它保存到一个文件中。当我尝试将其保存到播放器文件(播放器)时,它不会向世界显示。我需要保存到文件中。但是,是的,这与我想要完成的目标非常接近。但我不需要测试报价。这些变量是从不同的类中设置的。
  • 你的文件写入方法在哪里?
  • 您的main 方法在哪里运行程序?您可能需要逐步向我解释您要完成的工作,因为我不知道仅通过查看代码。我只能哪个,这就是我一直在做的,因为你的解释不是那么好。
  • 我实际上只是想将整数和字符串(所有者)保存到一个字符串中,并且可以在文件中执行类似的操作:100、50、10、500000、200、0, 1, 1, 0, 500000, 1, me 100, 50, 10, 500000, 200, 0, 1, 1, 0, 500000, 2, me2
  • 你想把这个字符串保存在哪里,你试图保存到文件的方法是什么?
猜你喜欢
  • 2023-02-07
  • 2016-06-01
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2018-09-21
  • 2012-07-04
  • 2013-02-07
  • 1970-01-01
相关资源
最近更新 更多