小编bil*_*118的帖子

正确地在while循环中使用BufferedReader.readLine()

所以我在将一个文本文件读入我的程序时遇到了问题.这是代码:

     try{
        InputStream fis=new FileInputStream(targetsFile);
        BufferedReader br=new BufferedReader(new InputStreamReader(fis));

        //while(br.readLine()!=null){
        for(int i=0;i<100;i++){
            String[] words=br.readLine().split(" ");
            int targetX=Integer.parseInt(words[0]);
            int targetY=Integer.parseInt(words[1]);
            int targetW=Integer.parseInt(words[2]);
            int targetH=Integer.parseInt(words[3]);
            int targetHits=Integer.parseInt(words[4]);
            Target a=new Target(targetX, targetY, targetW, targetH, targetHits);
            targets.add(a);
        }
        br.close();
    }
    catch(Exception e){
        System.err.println("Error: Target File Cannot Be Read");
    }
Run Code Online (Sandbox Code Playgroud)

我正在读取的文件是100行参数.如果我使用for循环它完美地工作.如果我使用while语句(for循环上面注释掉的那个),它会在50处停止.用户可能会运行带有任意行数的文件的程序,因此我当前的for循环实现赢了工作.

为什么线路while(br.readLine()!=null)停在50?我检查了文本文件,没有任何东西可以挂起来.

当我使用while循环时,我没有从try-catch中得到任何错误,所以我很难过.有人有主意吗?

java bufferedreader

19
推荐指数
5
解决办法
13万
查看次数

Jython - 使用PyObject的类转换异常

我试图在Jython中创建并转换对象,我收到以下错误:

Exception in thread "MainThread" java.lang.ClassCastException: org.python.core.PySingleton cannot be cast to resources.ixia.IxNetType
at resources.ixia.IxNetFactory.create(IxNetFactory.java:34)
at resources.ixia.IxiaTest.run(IxiaTest.java:34)
at resources.ixia.IxiaTest.<init>(IxiaTest.java:14)
at resources.ixia.IxiaTest.main(IxiaTest.java:42)
Run Code Online (Sandbox Code Playgroud)

这是代码:

import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

public class IxNetFactory {

    private PyObject ixNetClass;
    private PythonInterpreter interpreter;

    public IxNetFactory(String script_dir) {
        script_dir=script_dir.replace("\\", "/");

        interpreter = new PythonInterpreter();

        interpreter.exec("import sys");             
        interpreter.exec("sys.path.append('"+script_dir+"')");
        interpreter.exec("import time");
        interpreter.exec("import os");
        interpreter.exec("from ixnetworks import IxNet");
        //interpreter.exec("from utils import sm");
        //interpreter.exec("from utils import cpf");

        ixNetClass = interpreter.get("IxNet");
    }

    /*
     * Create an IxNet object
     * 
     * Usage: ixNet.create();
     */
    public …
Run Code Online (Sandbox Code Playgroud)

python java jython

5
推荐指数
1
解决办法
634
查看次数

C - 以十六进制打印出一个字符指针给了我奇怪的结果

所以我正在编写一个非常小而简单的程序,它将一个数字作为输入,将其转换为十六进制,然后一次打印出两个字符。

对于某些数字,它会在输出前打印出 ffffff。

这是我的代码:

    //Convert the input to an unsigned int
unsigned int a = strtoul (argv[1], NULL, 0);
//Convert the unsigned int to a char pointer
char* c = (char*) &a;

//Print out the char two at a time
for(int i = 0; i < 4; i++){
    printf("%02x ", c[i]);
}
Run Code Online (Sandbox Code Playgroud)

大多数输出​​都很好,看起来像这样:

./hex_int 1

01 00 00 00
Run Code Online (Sandbox Code Playgroud)

但是对于某些数字,输出如下所示:

./hex_int 100000

ffffffa0 ffffff86 01 00
Run Code Online (Sandbox Code Playgroud)

如果您删除所有 f,则转换是正确的,但我无法弄清楚为什么它仅在某些输入上执行此操作。

谁有想法?

c

3
推荐指数
1
解决办法
4623
查看次数

标签 统计

java ×2

bufferedreader ×1

c ×1

jython ×1

python ×1