0
我有一個名爲StringInput的接口,其代碼如下。無法訪問在接口中聲明的變量,即使它在另一個類中實現
public static ArrayList<String[]> fileInput() throws IOException {
ArrayList<String[]> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
String[] formatted = format(line);
lines.add(formatted);
System.out.println(line);
}
}
return lines;
}
public static String[] format(String s) {
String[] data = new String[9];
String logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+) \"([^\"]+)\" \"([^\"]+)\"";
System.out.println("Using RE Pattern:");
System.out.println(logEntryPattern);
System.out.println("Combined Apache Log Format (VERBOSE)");
System.out.println("Input line is:");
System.out.println(s);
Pattern p = Pattern.compile(logEntryPattern);
Matcher matcher = p.matcher(s);
if (!matcher.matches()
|| NUM_FIELDS != matcher.groupCount()) {
System.err.println("Bad log entry (or problem with RE?):");
System.err.println(s);
return null;
}
System.out.println("IP Address: " + matcher.group(1));
String clientIP = matcher.group(1);
System.out.println("Identd: " + matcher.group(2));
String identd = matcher.group(2);
System.out.println("UserID: " + matcher.group(3));
String userID = matcher.group(3);
System.out.println("Date&Time: " + matcher.group(4));
String dateTime = matcher.group(4);
System.out.println("Request: " + matcher.group(5));
String protocol = matcher.group(5);
System.out.println("Response: " + matcher.group(6));
String respCode = matcher.group(6);
System.out.println("Bytes Sent: " + matcher.group(7));
String respSize = matcher.group(7);
System.out.println("Referer: " + matcher.group(8));
String refer = matcher.group(8);
System.out.println("Browser: " + matcher.group(9));
String userAgent = matcher.group(9);
data[0] = clientIP;
data[1] = identd;
data[2] = userID;
data[3] = dateTime;
data[4] = protocol;
data[5] = respCode;
data[6] = respSize;
data[7] = refer;
data[8] = userAgent;
return data;
}
和另一個名爲ParseUpload的類實現了接口幷包含一個main方法。在main方法裏面,我有一個準備好的語句試圖從接口調用一個變量。但是,我不能。我不知道,因爲我通常不使用接口,沒有實例化它對我來說很奇怪。 有什麼幫助嗎?謝謝。
編輯:這是另一類。
public class ParseUpload implements StringInput {
public static void main(String argv[]) {
try {
StringInput.fileInput();
} catch (Exception e) {
e.printStackTrace();
}
//while (logEntryLine !=null){
// for (int i = 0; i < file.length(); i++){
//System.out.println(fileExists); ignore/For testing purposes
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:5294/mysql", "root", "Iu&#KR489)(");
System.out.println("Database Connection Established!");
String query = " insert into alogs(clientIP, identd, userID, dateTime, protocol, respCode, respSize, refer, userAgent)"
+ " values (?, ?, ?, ? ,? ,? ,?, ?, ?)";
PreparedStatement preparedStmt = con.prepareStatement(query);
preparedStmt.setString(1, data);
preparedStmt.setString(2, identd);
preparedStmt.setString(3, userID);
preparedStmt.setString(4, dateTime);
preparedStmt.setString(5, protocol);
preparedStmt.setString(6, respCode);
preparedStmt.setString(7, respSize);
preparedStmt.setString(8, refer);
preparedStmt.setString(9, userAgent);
preparedStmt.execute();
con.close();
} catch (SQLException ex) {
System.out.println("****************************Can't Connect to the database***********************************");
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} catch (ClassNotFoundException e) {
System.out.println("Class Not Found");
}
// }
}
}
您發佈沒有任何 「變量」,在它的代碼。當然,沒有任何一個可以從另一個班級訪問。 java中的範圍非常重要。 – rmlan
我正在嘗試在其他課程中調用數據[1]的值。我對多級編程還是一種新鮮感。任何建議你可以給修復代碼。 – CorruptedOffset
使用'return'引用給(未發佈的)調用者。 –