扫描仪跳过数字Java

时间:2013-05-19 20:51:47

标签: java string int java.util.scanner skip

我正在编写代码来搜索文档并查找数字并将它们放入数组中。这是文件的一部分:

 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;
}
}
}

2 个答案:

答案 0 :(得分:0)

您的removeNextWord()方法很贪婪,即它也会移除这些数字。

在你的实现中,你首先删除单词之前不是字母的所有内容,然后是单词本身,然后最后删除单词后面的所有不是字母的字符,然后删除id号。

也许,您应取消注释此行并将isLetter()重命名为isToken()

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

编辑 :请修改数字的条件检查。 ch <= '1'不正确。

答案 1 :(得分:0)

检查ch&gt; = 0&amp;&amp; ch&lt; = 9它只允许一位数字。使用正则表达式是个好主意

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

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