2017-01-11 27 views
0

所以目前我正在下面的代碼從一個名爲Races.txt如何讓我的程序跳過java中的一段文本?

import java.util.Scanner; 
import java.io.*; 

public class FileReader { 
    public FileReader(){ 
    } 

    public static void readRaceFile(String filename) throws FileNotFoundException{ 
     Scanner reader = new Scanner(new File(filename)); 
     System.out.println("File found"); 
     int str = 0, dex = 0, con = 0, intl = 0, wis = 0, cha = 0, maxAge = 0, baseSpeed = 0; 
     String name = "", size = "", description = ""; 
     while(reader.hasNext()){ 
      if(reader.hasNext("Race:")){ 
       reader.skip("Race:"); 
       reader.useDelimiter("\\{"); 
       name = reader.next(); 
       reader.skip("\nstr:"); 
       reader.useDelimiter(";"); 
       str = reader.nextInt(); 
       reader.skip(";\ndex:"); 
       dex = reader.nextInt(); 
       reader.skip(";\ncon:"); 
       con = reader.nextInt(); 
       reader.skip(";\nintl:"); 
       intl = reader.nextInt(); 
       reader.skip(";\nwis:"); 
       wis = reader.nextInt(); 
       System.out.println(str + wis + dex + con + intl + cha + maxAge + baseSpeed + name + size + description); 
      }else if(reader.hasNext("Subrace:")){ 

      } 
     } 
     reader.close(); 
    } 

    public static void main(String args[]){ 
     try{ 
      readRaceFile("Races.txt"); 
     }catch(IOException e){ 
      System.out.println("file not found"); 
     } 
    } 
} 

文件的代碼是從一個格式相同的方式,下面的文本文件閱讀,閱讀如下所示:

Race:name{ 
str:0; 
dex:0; 
con:0; 
intl:0; 
wis:0; 
cha:0; 
max age:100; 
base speed:35; 
size:medium; 
description:This is a race; 
abilities:Darkvision - You can see in dim light up to 60 feet as if it were bright light, Idiosy - You are stupid; 
number of possible tool proficiency choices:0; 
possible tool proficiencies:brewer's tools, masonry tools; 
number of possible skill proficiency choices:0; 
possible skill proficiencies:perception; 
number of possible language choices:0; 
possible language choices:elvish, dwarf; 
given tool proficiencies:martial weapons; 
given skill proficiencies:; 
given languages:english; 
cantrips: 
1, fireball 
3, plant growth; 
} 

當我運行程序時,我可以成功跳過「Race:」並導入「name」並打印它,如果我想,但是當我試圖通過使用「\ nstr:」,「str:」跳過「str: 「或」{\ nstr:「我收到以下錯誤:

Exception in thread "main" java.util.NoSuchElementException 
    at java.util.Scanner.skip(Unknown Source) 
    at java.util.Scanner.skip(Unknown Source) 
    at FileReader.readRaceFile(FileReader.java:18) 
    at FileReader.main(FileReader.java:39) 

這是我的第一個Java編碼項目之一,我意識到這可能是一個簡單的修復,但更多的幫助,將不勝感激

附:我意識到其他代碼在這一點上是不完整的,但是我正在測試它以確保掃描儀能夠工作,以便我可以創建另一個我一直在研究的對象。謝謝!

+2

這只是我的意見,但考慮到數據的(合理的)複雜性,我建議你在看看XML或JSON數據結構,而不是,它具有定義良好且經過測試的API可用,它將使您的生活從長遠來看變得如此簡單 – MadProgrammer

+0

是的,如果您可以更改數據格式,則應該使用XML或JSON,並且我會推薦使用JAXB等映射技術MOXy作爲實現)用於XML或用於JSON的Jackson – kwisatz

回答

0

這不是讀取此文件的完美方式。任何請檢查以下代碼是否適合您。

public static void readRaceFile(String filename) throws FileNotFoundException{ 
     Scanner reader = new Scanner(new File(filename)); 
     System.out.println("File found"); 
     int str = 0, dex = 0, con = 0, intl = 0, wis = 0, cha = 0, maxAge = 0, baseSpeed = 0; 
     String name = "", size = "", description = ""; 
     while(reader.hasNext()){ 
      if(reader.hasNext("Race:")){ 
       reader.skip("Race:"); 
       reader.useDelimiter("\\{"); 
       name = reader.next(); 
       reader.nextLine(); 
       reader.skip("str:"); 
       reader.useDelimiter(";"); 
       str = reader.nextInt(); 
       reader.nextLine(); 
       reader.skip("dex:"); 
       dex = reader.nextInt(); 
       reader.nextLine(); 
       reader.skip("con:"); 
       con = reader.nextInt(); 
       reader.nextLine(); 
       reader.skip("intl:"); 
       intl = reader.nextInt(); 
       reader.nextLine(); 
       reader.skip("wis:"); 
       wis = reader.nextInt(); 
       System.out.println(str + wis + dex + con + intl + cha + maxAge + baseSpeed + name + size + description); 
      }else if(reader.hasNext("Subrace:")){ 

      } 
     } 
     reader.close(); 
    } 

既然你是一個新手,請嘗試閱讀關於正則表達式模式匹配,以避免這種掃描器的跳過(模式)。

您可以將文件讀入字符串中,也可以逐行讀取或使用分隔符。然後運用你的邏輯。而不是如何獲得邏輯。這是超級僵硬的,一個空白或一個換行符會把事情搞砸。

例如,您可以一行一行地閱讀並對手頭的字符串進行操作,而不是使用掃描儀本身進行播放。

public static void readRaceFile(String filename) throws FileNotFoundException{ 

      Scanner reader = new Scanner(new File(filename)); 
      System.out.println("File found"); 
      while(reader.hasNext()){ 
       String line = reader.nextLine(); 

       //Do you work with the 'line' string as you wish.. Split the string 
       //or applying regex anything you want to get the details in the string. 

       //System.out.println(reader.nextLine()); 
      } 
} 
0

你應該真的使用一個完整的解析器,以便它可以處理更復雜的情況。最好是,如果您可以更改文件格式,則可以使用現有解析器(例如JSON)。但是,如果您真的想爲此使用掃描儀,我已概述了適用於您的示例數據集的方法。請注意,當您需要在數據值中包含分隔符時,這種方法會失效。

private static final String DELIMITER = "(" 
+ "\r\n|[\n\r\u2028\u2029\u0085]" // end of line 
+ "|;" // semi-colons 
+ "|," // commas 
+ "|(?<=:)" // match after colon 
+ "|(?=\\{|\\})|(?<=\\{|\\})" // match before+after brace 
+ ")+"; // one or more 

public static void readRaceFile(String filename) throws FileNotFoundException{ 
    try(Scanner reader = new Scanner(new File(filename))) { 
     reader.useDelimiter(DELIMITER); 
     while(reader.hasNext()){ 
      String s = reader.next(); 
      if(s.equals("{")) { 
       System.out.println("{start block}"); 
      } else if(s.equals("}")) { 
       System.out.println("{end block}"); 
      } else if(s.endsWith(":")) { 
       s = s.substring(0, s.length()-1); 
       System.out.println("attribute: " + s); 
      } else { 
       s = s.trim(); 
       System.out.println("\tvalue: " + s); 
      } 
     } 
    } 
} 

輸出:

attribute: Race 
    value: name 
{start block} 
attribute: str 
    value: 0 
attribute: dex 
    value: 0 
attribute: con 
    value: 0 
attribute: intl 
    value: 0 
attribute: wis 
    value: 0 
attribute: cha 
    value: 0 
attribute: max age 
    value: 100 
attribute: base speed 
    value: 35 
attribute: size 
    value: medium 
attribute: description 
    value: This is a race 
attribute: abilities 
    value: Darkvision - You can see in dim light up to 60 feet as if it were bright light 
    value: Idiosy - You are stupid 
attribute: number of possible tool proficiency choices 
    value: 0 
attribute: possible tool proficiencies 
    value: brewer's tools 
    value: masonry tools 
attribute: number of possible skill proficiency choices 
    value: 0 
attribute: possible skill proficiencies 
    value: perception 
attribute: number of possible language choices 
    value: 0 
attribute: possible language choices 
    value: elvish 
    value: dwarf 
attribute: given tool proficiencies 
    value: martial weapons 
attribute: given skill proficiencies 
attribute: given languages 
    value: english 
attribute: cantrips 
    value: 1 
    value: fireball 
    value: 3 
    value: plant growth 
{end block} 
相關問題