生成用于输入扫描仪的字符序列

时间:2014-12-14 17:52:18

标签: java

我对Bruce Eckels在Java 4E第332页思考的练习感到困惑。

问题要求创建一个产生一系列字符的类。调整此类以使其可以作为Scanner对象的输入(即实现可读)。

我的问题是我编写的代码与Bruce在他的书中给出的示例非常相似。但他的榜样有效,而我的例子不然。我做错了什么?

注意:这不是作业(这只是对本书的个人研究,而不是课堂作业)。下面是布鲁斯的代码,它起作用,然后是我对基于他的练习题的回答,这不起作用。当然我看到有一个缓冲区溢出异常,但我不明白为什么会发生这种情况而不是Bruce(缓冲区似乎是由Scanner类提供的,因为它没有在任何地方声明..但是我在这一点上迷路了。)

布鲁斯的代码(WORKS):

import java.nio.*;
import java.util.*;

//based on example in Bruce Eckels Thinking in Java 4th edition
//see page 332: "adapting to an interface".

public class RandomWords implements Readable {

  //random number generator
  private static Random rand = new Random(47);

  //an array of lowercase letters
  private static final char[] lowers =
    "abcdefghijklmnopqrstuvwxyz".toCharArray();

  //number of words to generate
  private int count;

  //constructor
  public RandomWords(int count) { this.count = count; }

  //read method (required to implement Readable)
  public int read(CharBuffer cb) {

    if(count-- == 0)
      return -1; // Indicate end of input

    //ten letter random word
    for(int i = 0; i < 9; i++) {
      cb.append(lowers[rand.nextInt(lowers.length)]);
    }
    cb.append(" "); //separate tokens with a space ?
    return 10; // Number of characters appended
  }

  public static void main(String[] args) {
    Scanner s = new Scanner(new RandomWords(8));
    while(s.hasNext())
      System.out.println(s.next());
  }

}

现在这是我的代码(DOESN&#39; T WORK)

import java.nio.*;
import java.util.*;

public class RandomWord implements Readable {

    //length of random word to be generated
    private int count;

    //Random number generator
    private static Random rand = new Random(47);

    //Array of letters to choose from
    private  static final char[] letters =
                "abcdefghijklmnopqrstuvwxyz".toCharArray();

    //Constructor
    RandomWord(int count) {
        this.count = count;
    }

    //read method (required to implement Readable)
    public int read(CharBuffer cb){

        //no input
        if(count == 0)
          return -1;

        //generate letters
        for(int i = 0; i < count; ++count)
            cb.append(letters[rand.nextInt(letters.length)]);

        //separate tokens with a space ?
        cb.append(" ");

        //return number of letters generated
        return count;
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(new RandomWord(8));
            System.out.println(s.next());
            //also no joy:
            //if (s.hasNext())
            //  System.out.println(s.next());
    }

}

输出:

Exception in thread "main" java.nio.BufferOverflowException
    at java.nio.Buffer.nextPutIndex(Buffer.java:513)
    at java.nio.HeapCharBuffer.put(HeapCharBuffer.java:163)
    at java.nio.CharBuffer.append(CharBuffer.java:1375)
    at RandomWord.read(RandomWord.java:30)
    at java.util.Scanner.readInput(Scanner.java:849)
    at java.util.Scanner.next(Scanner.java:1414)
    at RandomWord.main(RandomWord.java:41)


------------------
(program exited with code: 1)

1 个答案:

答案 0 :(得分:1)

这个循环没有结束......

for(int i = 0; i < count; ++count)

改变

for(int i = 0; i < count; i++)