amber_new 2017-11-26 08:00 采纳率: 0%
浏览 1409

算法第四版二分法代码运行出现错误

感谢各位大神!
代码是直接从官网保存下来的,在终端运行时出现以下错误提示,不知道是该怎么改啊...

MA:src vicky$ javac BinarySearch.java
MA:src vicky$ java BinarySearch tinyW.txt < tinyT.txt
Exception in thread "main" java.lang.NumberFormatException: For input string: "{\rtf1\ansi\ansicpg936\cocoartf1404\cocoasubrtf470"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at In.readAllInts(In.java:528)
at BinarySearch.main(BinarySearch.java:93)

源代码如下
/******************************************************************************

import java.util.Arrays;

/**

  • The {@code BinarySearch} class provides a static method for binary
  • searching for an integer in a sorted array of integers.
  • The indexOf operations takes logarithmic time in the worst case.
  • For additional documentation, see Section 1.1 of
  • Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. *
  • @author Robert Sedgewick
  • @author Kevin Wayne
    */
    public class BinarySearch {

    /**

    • This class should not be instantiated. */ private BinarySearch() { }

    /**

    • Returns the index of the specified key in the specified array. *
    • @param a the array of integers, must be sorted in ascending order
    • @param key the search key
    • @return index of key in array {@code a} if present; {@code -1} otherwise */ public static int indexOf(int[] a, int key) { int lo = 0; int hi = a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. int mid = lo + (hi - lo) / 2; if (key < a[mid]) hi = mid - 1; else if (key > a[mid]) lo = mid + 1; else return mid; } return -1; }

    /**

    • Returns the index of the specified key in the specified array.
    • This function is poorly named because it does not give the rank
    • if the array has duplicate keys or if the key is not in the array. *
    • @param key the search key
    • @param a the array of integers, must be sorted in ascending order
    • @return index of key in array {@code a} if present; {@code -1} otherwise
    • @deprecated Replaced by {@link #indexOf(int[], int)}. */ @Deprecated public static int rank(int key, int[] a) { return indexOf(a, key); }

    /**

    • Reads in a sequence of integers from the whitelist file, specified as
    • a command-line argument; reads in integers from standard input;
    • prints to standard output those integers that do not appear in the file. *
    • @param args the command-line arguments
      */
      public static void main(String[] args) {

      // read the integers from a file
      In in = new In(args[0]);
      int[] whitelist = in.readAllInts();

      // sort the array
      Arrays.sort(whitelist);

      // read integer key from standard input; print if not in whitelist
      while (!StdIn.isEmpty()) {
      int key = StdIn.readInt();
      if (BinarySearch.indexOf(whitelist, key) == -1)
      StdOut.println(key);
      }
      }
      }

  • 写回答

1条回答 默认 最新

  • binglian101 2018-01-11 03:06
    关注

    报错已经说了:java.lang.NumberFormatException: For input string: "{\rtf1\ansi\ansicpg936\cocoartf1404\cocoasubrtf470",可以看一下输入的格式是否有问题

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入