Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving Data + String
    primarykey
    data
    text
    <p>I'm trying to save &amp; load data but I'm getting frustrated &amp; need some damn sleep. I can't seem to figure this out without getting bugs or nulls.</p> <p>My code:</p> <pre><code>public class Offers { public static List&lt;Offer&gt; offers = new ArrayList&lt;Offer&gt;(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 &amp;&amp; l.item == item &amp;&amp; l.amount == amount &amp;&amp; l.currentAmount == currentAmount &amp;&amp; l.price == price &amp;&amp; l.type == type &amp;&amp; l.owner.equals(owner) &amp;&amp; l.slot == slot) { l.currentPrice = currentPrice; l.aborted = aborted; break; } } } } </code></pre> <p>What I'm trying to do in the same method is save all that into a string so when the load method is called it will load &amp; set the variables to what was saved in that string. for example saves as <code>""+ id + "" + item + "" + amount + "" + price + "" + currentAmount + "" + removedAmount + "" + type + "" + completed + "" + aborted + "" + currentPrice + "" + slot + "" + owner + ""</code> and loads as the same while setting the variables as they were when saved.</p> <p>I'm so done with this I'm on the edge of paying someone like 5$ to just do it for me. Just asking for some help or if someone could be kind enough to fix my crap work. ++*10 for help.</p> <p>Offer Class as requested:</p> <pre><code>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); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload