2013-05-19 50 views
1

我正在編寫代碼來搜索文檔並查找數字並將它們放入數組中。這是一個文件:掃描儀跳過數字Java

username Sam movies id 1 Citizen Kane id 2 Vertigo id 3 Rules of the Game... 

當我運行該程序nextword跳過id後的數字,並使用下一個單詞。我需要它讀取數字,以便將數字放入數組中。

請幫忙。謝謝!

package ArrayInversions; 
import java.io.*; 
public class Main{ 
public static void main (String[]args) 
    throws FileNotFoundException{ 
    TextReader read = new TextReader("/Users/name/Desktop/movie-data.json"); 

    int[] ArraySam = new int[1000]; 
    int[] ArrayTerry = new int [1000]; 
    int[] ArrayDana = new int [1000]; 
    int temp; 
    int i=0; 

    String nextword; 
    String name=null; 
    String id="id"; 

    nextword=read.GetWord(); 
    while (nextword!=null){ 
     if (nextword.compareTo("username")==0){ 
      nextword=read.GetWord(); 
      name=nextword; 
      System.out.println("name: "+name); 
      nextword=read.GetWord(); 
      i=0; 
     } 
     System.out.println("* "+nextword+"="+id); 

     if(nextword.compareTo(id)==0){ 
      nextword=read.GetWord(); 
      System.out.println(nextword); 
      temp=Integer.valueOf(nextword); 

      if (name.compareTo("Sam")==0){ 
       System.out.println("Sam"); 
       ArraySam[i]=temp; 
       i++; 
      } 
      else if (name.compareTo("Terry")==0){ 
       System.out.println("Terry"); 
       ArrayTerry[i]=temp; 
       i++; 
      } 
      else{ 
       System.out.println("Dana"); 
       ArrayDana[i]=temp; 
       i++; 
      } 
     } 
     nextword=read.GetWord(); 
      } 
     } 
     } 

package ArrayInversions; 
import java.util.*; 
import java.io.*; 

public class TextReader { 

private Scanner read; 
private String currline; 

public TextReader(String filename){ 
try{ 
    currline = ""; 
    read = new Scanner(new File(filename)); 
} 
catch (Exception ex){    
    System.out.println("File does not exist error: "+ex.toString()); 
} 
} 
private static boolean isLetter(char ch) { 
    return ((ch >= 'A')&&(ch <= 'Z') || 
      (ch >= 'a')&&(ch <= 'z') || 
       // (ch <= '1')&&(ch <= '9') || //allows numbers? 
      (ch == '-') || (ch == '\'') 
      ); } 
private String removeNextWord(String s) { 
    //Returns the string with the first 'word' removed 
    //First, pull all non-letters off front 
    while ((s.length()>0) && (isLetter(s.charAt(0))== false)) 
     s = s.substring(1); 
    //Now, pull all letters off front 
    while ((s.length()>0) && (isLetter(s.charAt(0))== true)) 
     s = s.substring(1); 
    //Finally remove all non-letters off front 
    while ((s.length()>0) && (isLetter(s.charAt(0))== false)) 
     s = s.substring(1); 

    return s;        //Return the resulting string 
} 
private String getNextWord(String s) { 
    //Returns first 'word' of the string 
    //First, pull all non-letters off front 
    String word=""; 
    while ((s.length()>0) && (isLetter(s.charAt(0))== false)) 
     s = s.substring(1); 
    //Now, keep all letters as we pull them off the front 
    while ((s.length()>0) && (isLetter(s.charAt(0))== true)) 
    { word = word + s.charAt(0); //build up the word 
     s = s.substring(1);   //remove letters from string input 
    } 
    return word;     //Return the resulting word string 
} 

public String GetWord(){ 
// throws FileNotFoundException //required throw line 
{ String nextword; 

    while ((currline != null)&&(currline.length()== 0)){ 
     if (read.hasNext()) 
      currline = read.nextLine().trim(); 
     else 
      currline = null; 
    } 
    if (currline != null) 
    { nextword = getNextWord(currline); //get word from front of line 
     currline = removeNextWord(currline).trim(); //update the line w/o word 
    } 
    else 
    { 
     nextword = null; 
    } 
    return nextword; 
} 
} 
} 

回答

0

removeNextWord()方法是貪婪的,即它取出數爲好。

在您的實現,首先拆除的一切,這不是單詞前的一封信,那是不信,然後刪除ID號以及在字後面的詞本身,最後所有的字符。

也許,您應該取消註釋此行並將isLetter()重命名爲isToken()

(ch >= '0') && (ch <= '9') || // allow numbers 

編輯:請解決您的數字條件檢查。 ch <= '1'不正確。

+0

當我取消註釋行(ch <='1')&&(ch <='9')|| //允許數字不會給我一個字。我得到所有的話。 – user2399625

+0

提示:使用'Character.isLetterOrDigit(ch)'實現新的'isToken()'方法。 –

+0

當我取消註釋該行在我的主要nextword成爲:「用戶名山姆電影ID 1公民凱恩ID」。 – user2399625

0

當您檢查ch> = 0時& & ch < = 9它只會允許單個數字的數字。使用正則表達式是一個好主意

String regex="^[0-9]$"; 

if(token.matches(regex)) 
// add the token to the array!