我有一个非常简单的解析器拆分字符串:
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) 我已经构建了这个程序来从文件中读取用户名.它会检查用户输入的用户名是否在配置文件中.现在,如果它不在文件中,它会要求您创建新用户吗?我正在尝试做的是来自用户的一些输入验证 - 意思是,我希望他能够回答只有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) 我有一段代码
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];"
此指令为局部变量赋值,但不会在任何后续指令中读取或使用该值.通常,这表示错误,因为从未使用计算的值.
为什么我这样做?
非常感谢
我有这个代码:
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)
为什么是这样?
我正在尝试编译并执行一个使用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)
对于一个简单的解决方案来说,这很可能是一个非常愚蠢的问题,但这让我感到疯狂.请帮忙!!
我有个问题。
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) 我有以下表格中的对象列表
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是否提供此功能?或者我是否必须遍历所有对象并找到唯一的状态值?
我有一个整数值,我想在十六进制转换它.
我这样做:
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
谢谢.
我有一个包含三列的文件,即,
a 1 4
b 2 5
c 3 6
我希望将此文件读入字典,以便第一列是键,第二列和第三列是值,即
dict = {'a': (1,4), 'b': (2,5), 'c': (3,6)}
有人对此有命令吗?