小编hom*_*ome的帖子

Java奇怪的输出:[Ljava.lang.String;@1aa8c488(string.split("\\s"))

我有一个非常简单的解析器拆分字符串:

import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class Parser {

public static void main(String args[]){

    String newLog = new String();
    try {
        BufferedReader reader = new BufferedReader(new FileReader("/Users/John/IdeaProjects/tomcatParser/src/log.log"));
        try {
            while ((newLog = reader.readLine()) != null){
                System.out.println(newLog.split("\\s"));
                //System.out.println(newLog.split(" "));
                break;
            }
            //reader.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | …
Run Code Online (Sandbox Code Playgroud)

java regex string

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

无限循环...?

我已经构建了这个程序来从文件中读取用户名.它会检查用户输入的用户名是否在配置文件中.现在,如果它不在文件中,它会要求您创建新用户吗?我正在尝试做的是来自用户的一些输入验证 - 意思是,我希望他能够回答只有Y表示是,N表示否,并且只有5次尝试.

我的问题是,在"标记为"循环中,某些内容无法正常工作.它假设只询问用户他的用户名5次,但它永远要求,就像一个无限循环.另外,我希望它只向用户写一次我找不到他的个人资料,所以我把它放在for循环之外,但它会在每次迭代中显示出来.

任何帮助都会被暗示.

else {
    System.out.println("Sorry couldn't find your user profile " + userName + ".");
    // If profile wasn't found, ask to create a new one.
    search:
    for(int i=0; i<5; i++) {    
        System.out.println("Would you like to create a new user profile now? (Enter Y for yes), (Enter N for no and exit).");
        try{
            BufferedReader answer = new BufferedReader(new InputStreamReader(System.in));
            String addNewUser = answer.readLine();
            // If user pressed Y than write the new user name to myFile.txt
            if …
Run Code Online (Sandbox Code Playgroud)

java for-loop

0
推荐指数
1
解决办法
342
查看次数

指令分配一个值,但不会在任何后续指令中读取或使用该值

我有一段代码

for(int i = 0; i < num_of_random; i++){

    String str = in.readLine();
    if(str != null){
        String[] randoms = new String[4];
        randoms = str.split(",");

        dateRanges[i] = Integer.parseInt(randoms[0]);
        id[i] = Integer.parseInt(randoms[1]);
        flag[i] = Integer.parseInt(randoms[2]);
        system[i] = Integer.parseInt(randoms[3]);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我针对findBugs运行此代码时,我得到了一个建议

"String [] randoms = new String [4];"

此指令为局部变量赋值,但不会在任何后续指令中读取或使用该值.通常,这表示错误,因为从未使用计算的值.

为什么我这样做?

非常感谢

java variables findbugs

0
推荐指数
1
解决办法
676
查看次数

为什么这会导致空指针异常?

我有这个代码:

public class Sprite {
    protected float x;
    protected float y;
    protected Image image;
    protected Rectangle boundingBox;

    Sprite(float x, float y, Image image) {
        this.x = x;
        this.y = y;
        this.image = image;
        boundingBox = new Rectangle(x, y, 
            this.image.getWidth(), this.image.getHeight());
    }
Run Code Online (Sandbox Code Playgroud)

...

public Rectangle getBoundingBox() {
    return boundingBox;
}
Run Code Online (Sandbox Code Playgroud)

但是,一旦定义并初始化了sprite对象,我在另一个类中调用此函数时:

public static boolean collides(Sprite object1, Sprite object2) {
    return object1.getBoundingBox().intersects(object2.getBoundingBox());
}
Run Code Online (Sandbox Code Playgroud)

我得到一个空指针异常,指向包含这个的行:

this.image.getWidth(), this.image.getHeight());
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

java initialization

0
推荐指数
1
解决办法
752
查看次数

使用jar库包编译和执行java程序

我正在尝试编译并执行一个使用JTidy的java程序.我已经设法使用以下命令编译程序:

javac -classpath jtidy-r938.jar @sourcefile
Run Code Online (Sandbox Code Playgroud)

这似乎工作得很好.但是,当我尝试使用以下命令运行程序时(Top是包含程序主要部分的类的名称):

java -classpath jtidy-r938.jar Top
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: Top
Caused by: java.lang.ClassNotFoundException: Top
...
Could not find the main class: Top. Program will exit.
Run Code Online (Sandbox Code Playgroud)

对于一个简单的解决方案来说,这很可能是一个非常愚蠢的问题,但这让我感到疯狂.请帮忙!!

java javac

0
推荐指数
1
解决办法
1362
查看次数

MySQL 提交和回滚失败

我有个问题。

try {                   
    jdbcConnect( ); //get mysql connect
    conn.setAutoCommit( false );

    pstmt = conn.prepareStatement ( 
                    "INSERT INTO member ( member_name, member_introduce ) VALUES ( ?, ? )", Statement.RETURN_GENERATED_KEYS );

    pstmt.setString( 1, "something" );
    pstmt.setString( 2, "something" );
    pstmt.executeUpdate( );
    rs = pstmt.getGeneratedKeys( );
    rs.next( );
    String no = Integer.toString( rs.getInt( 1 );

    pstmt = conn.prepareStatement ( "UPDATE account SET account_name = ? WHERE account_no = ?" );
    pstmt.setString( 1, "something");
    pstmt.setString( 2, no );
    pstmt.executeUpdate( );

    conn.commit( );         
    conn.setAutoCommit( true …
Run Code Online (Sandbox Code Playgroud)

java mysql commit jdbc rollback

0
推荐指数
1
解决办法
1327
查看次数

java在列表中查找具有唯一属性的对象

我有以下表格中的对象列表

Person object with attributes and values
name=x, state=va
name=x, state=nj
name=x, state=va
name=x, state=va
name=x, state=md
name=x, state=va
Run Code Online (Sandbox Code Playgroud)

我想找到唯一的状态值作为数组或列表.Google集合或Java是否提供此功能?或者我是否必须遍历所有对象并找到唯一的状态值?

java

0
推荐指数
1
解决办法
3669
查看次数

将整数转换为十六进制字符串

我有一个整数值,我想在十六进制转换它.

我这样做:

private short getCouleur(Integer couleur, HSSFWorkbook classeur) {
if (null == couleur) {
    return WHITE.index;
} else {
    HSSFPalette palette = classeur.getCustomPalette();
    String hexa = Integer.toHexString(couleur);

    byte r = Integer.valueOf(hexa.substring(0, 2), 16).byteValue();
    byte g = Integer.valueOf(hexa.substring(2, 4), 16).byteValue();
    byte b = Integer.valueOf(hexa.substring(4, 6), 16).byteValue();

    palette.setColorAtIndex((short) 65, r, g, b);

    return (short) 65;
}
}
Run Code Online (Sandbox Code Playgroud)

在输出中我有这个:

couleur:65331

:FF33

hexa.substring(0,2):FF

hexa.substring(2,4):33

hexa.substring(4,6):

r:-1

g:51

b:错误消息

错误消息:字符串索引超出范围:6

谢谢.

java

0
推荐指数
1
解决办法
6318
查看次数

将文件转换成字典

我有一个包含三列的文件,即, a 1 4 b 2 5 c 3 6 我希望将此文件读入字典,以便第一列是键,第二列和第三列是值,即

dict = {'a': (1,4), 'b': (2,5), 'c': (3,6)}

有人对此有命令吗?

python dictionary file

0
推荐指数
1
解决办法
45
查看次数