2012-02-26 6 views
-1

私は現在ゲーム(コード3500行以上の巨大なゲーム)を作っています。ゲームの保存機能をプログラミングするときにerror.itがうまくいきましたが、試してみると負荷私はそれにゲームを保存したファイルのいずれかがエラーを返します:java game saving program wake work

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at com.torstein.Mars_Settlement.States.Building.Parse(Building.java:56) 
    at com.torstein.Mars_Settlement.Managers.SavingManager.Load(SavingManager.java:102) 
    at com.torstein.Mars_Settlement.SLGUI$2.actionPerformed(SLGUI.java:155) 
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 

のように...

basicly「」私は、ファイルをスキャンするために使用するスキャナが戻っていることを言っています。私は何度もソースコードをチェックしていますが、私は仕事をすることができません。ここSavingManagerクラスです:解析しようとイム

package com.torstein.Mars_Settlement.Managers; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Formatter; 
import java.util.Scanner; 

import com.torstein.Mars_Settlement.Bases; 
import com.torstein.Mars_Settlement.Connection; 
import com.torstein.Mars_Settlement.Main; 
import com.torstein.Mars_Settlement.Eneties.CarEnety; 
import com.torstein.Mars_Settlement.Eneties.LightGunnerEntity; 
import com.torstein.Mars_Settlement.Eneties.Pepole; 
import com.torstein.Mars_Settlement.Eneties.Plant; 
import com.torstein.Mars_Settlement.Eneties.TankEntity; 
import com.torstein.Mars_Settlement.States.Building; 
import com.torstein.Mars_Settlement.States.GroundState; 

public class SavingManager { 

    public static void Save(String Path){ 
     Main m = Main.UseThis; 
     File txt = new File(Path); 
     txt.delete(); 
     Formatter f = null; 
     try { 
      f = new Formatter(Path); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     f.format("%d%n",m.pepoleOut); 
     for(Bases b : m.bases) 
      f.format(b.toString()+"%n"); 
     f.format(":%n"); 
     f.format(m.Fusion+"%n"); 
     f.format(m.Compact+"%n"); 
     f.format(m.Money+"%n"); 
     f.format(m.Water+"%n"); 
     f.format(m.Energy+"%n"); 
     f.format(m.Food+"%n"); 
     f.format(m.Venus+"%n"); 
     for(int x = 0;x<20;x++) 
      for(int y = 0;y<20;y++) 
       f.format(m.stateG[x][y].T+"%n"); 
     for(int x = 0;x<20;x++) 
      for(int y = 0;y<20;y++) 
       f.format(m.stateB[x][y].toString()+"%n"); 
     f.format(m.Terraformed+"%n"); 

     for(CarEnety b : m.cars) 
      f.format(b.toString()+"%n"); 
     f.format(":%n"); 

     for(Pepole b : m.Pepole) 
      f.format(b.toString()+"%n"); 
     f.format(":%n"); 

     for(Plant b : m.plants) 
      f.format(b.toString()+"%n"); 
     f.format(":%n"); 

     for(Connection b : m.Connections) 
      f.format(b.toString()+"%n"); 
     f.format(":%n"); 
     for(TankEntity b : m.Tanks) 
      f.format(b.toString()+"%n"); 
     f.format(":%n"); 
     for(LightGunnerEntity b : m.LightGunners) 
      f.format(b.toString()+"%n"); 
     f.format(":%n"); 

     f.close(); 
    } 
    public static void Load(String Path){ 
     final Main m = Main.UseThis; 
     m.Contiuecapable=true; 
     m.Treset(); 
     Scanner scan = null; 
     try { 
      scan = new Scanner(new File(Path)); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     String cln; 
     m.pepoleOut = scan.nextInt(); 
     while(!(cln = scan.nextLine()).startsWith(":")) 
      if(Bases.parseBase(cln)!=null)m.bases.add(Bases.parseBase(cln)); 
     m.Fusion = scan.nextBoolean(); 
     m.Compact = scan.nextBoolean(); 
     m.Money = scan.nextInt(); 
     m.Water = scan.nextInt(); 
     m.Energy = scan.nextInt(); 
     m.Food = scan.nextInt(); 
     m.Venus = scan.nextBoolean(); 
     for(int x = 0;x<20;x++) 
      for(int y = 0;y<20;y++) 
       m.stateG[x][y]=GroundState.Parse(scan.nextInt()); 


     for(int x = 0;x<20;x++) 
      for(int y = 0;y<20;y++) 
      { 

       m.stateB[x][y]= Building.Parse(scan.nextLine()); 
       if(m.stateB[x][y].index==6) 
        m.addHouseSpirit(x,y); 
      } 

     m.Terraformed = scan.nextBoolean(); 
     while(!(cln = scan.nextLine()).startsWith(":")) 
      if(CarEnety.Parse(cln)!=null)m.cars.add(CarEnety.Parse(cln)); 
     while(!(cln = scan.nextLine()).startsWith(":")) 
      if(Pepole.Parse(cln)!=null)m.Pepole.add(Pepole.Parse(cln)); 
     while(!(cln = scan.nextLine()).startsWith(":")) 
      if(Plant.Parse(cln)!=null)m.plants.add(Plant.Parse(cln)); 
     while(!(cln = scan.nextLine()).startsWith(":")) 
      if(Connection.Parse(cln)!=null)m.Connections.add(Connection.Parse(cln)); 
     while(!(cln = scan.nextLine()).startsWith(":")) 
      if(TankEntity.Parse(cln)!=null)m.Tanks.add(TankEntity.Parse(cln)); 
     while(!(cln = scan.nextLine()).startsWith(":")) 
      if(LightGunnerEntity.Parse(cln)!=null)m.LightGunners.add(LightGunnerEntity.Parse(cln)); 

     scan.close(); 
    } 

} 

ファイル内容:

0 
R:4:4:0:false 
: 
false 
false 
1000 
1000 
500 
1000 
false 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
2 
0 
0 
0 
0 
1 
0 
0 
2 
0 
3 
0 
0 
0 
0 
0 
0 
1 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 
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0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
0:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
-1:100:0:1 
false 
: 
: 
: 
: 
: 
: 

Building.Parse:

public static Building Parse(String parsef){ 

     int id = Integer.parseInt(parsef.split(":")[0]); 
     Building b =id == -1 ? No : 
     id == 0 ? Base : 
     id == 1 ? UranCollectorBase : 
     id == 2 ? IceCollectorBase : 
     id == 3 ? NuclearReactor : 
     id == 4 ? House : 
     id == 5 ? IceMeltor : 
     id == 6 ? GunHouse : 
     id == 7 ? ResearchCenter : 
     id == 8 ? GBase : 
     id == 9 ? SBase : 
     id == 10 ? MBase : 
     id == 11 ? FlowerCollector : 
     id == 12 ? WindMill : 
     id == 13 ? VechleProduser : 
     id == 14 ? OBase : 
     No; 
     b.health = Integer.parseInt(parsef.split(":")[1]); 
     b.Tanks = Integer.parseInt(parsef.split(":")[2]); 
     b.lookfor=carStates.Parse(Integer.parseInt(parsef.split(":")[3])); 
     return b; 
    } 

あなたが必要としなければならない何かがある場合このエラーを修正してください。御時間ありがとうございます。 Torstein。

+2

デバッガでこれを実行したときに何を学びましたか? –

+0

エラーが発生する直前にスキャナに入力が残っているので、ループのfisrt要素で読み込みが中止されました。それは基本的にすべて私が感知した重要なものです –

+4

Ok。ここで役に立つヘルプをしたいのであれば、これを問題を示す最小のテストケースに減らす必要があります(http://sscce.orgを参照)。 –

答えて

0

forループ{GroundState.Parse(scan.nextInt()}}のステートメントscan.nextInt()が前回実行されたとき、ファイル読み取りポインターが行末にあると思います。 nextLine()を呼び出すと、 ""(空文字列)が返されます。だからあなたがする必要があるのは、この特定の空の文字列を処理からスキップすることだけです。

3

独自のオブジェクトパーシスタンスコードの作成には気をつけないでください。既存のオブジェクトパーシスタンスコードがあります。あなたのデータには、ゲームの状態をオブジェクトとして保存することをお勧めします。 JAXBを使用してオブジェクトをマーシャリングし(すなわちXMLとしてディスクに書き込む)、オブジェクトを非マーシャルする(すなわち、XMLファイルを読み取ることによってオブジェクトを作成する)。

JAXBはあまり使用するのが難しくありません。あなたはJAXB reference implementationページでそれを読むことができます。私はEclipseLinkのJAXBプロバイダ、MOXyのファンです。彼らは非常に守りやすいgetting started tutorialを持っています。

ボーナスとして、保存ゲームファイルをXMLに保存すると、任意のテキストエディタで開くことができ、ゲームの状態を簡単に読み取ることができます。これにより、ゲーム状態を人間が読めるようにすることで、ゲームのデバッグがはるかに容易になります。あなたの現在のフォーマットでは、値を調べるために行を数えなければなりません。

XMLが気に入らない場合は、Javaプロパティファイルを使用してください。これは、生の値よりも優れています。