[英]LOOK - else if statement: where do i put it
我不知道把我的 ELSE 語句放在哪里,如果我把它放在 for 循環中,在 IF 語句之后,它會多次重復 else 語句中的內容。 但是,如果我在 ELSE 語句中輸入想要輸出的內容,例如在 FOR 循環外“抱歉,無法找到此登記牌”,那么它會在我不想要的時候輸出。
else // or
{
// this code reads a file in
String full="";
try { FileReader reader = new FileReader("address.txt"); // new file reader to read in a file called address
BufferedReader OwnerDetails = new BufferedReader(reader); // reads in the file efficiently
String line; // assigning a string
while ((line = OwnerDetails.readLine()) != null) { // reads in all file
full=full+line;
}
reader.close(); // the reader close, it finishes reading it in
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(full); // reads in whole file, and outputs it, used to check if the file had been written in
// it is commented out once program is finished, but is used when testing
String[] details = full.split(","); // splits up info into arrays
String searchword=registration; // searchword is set as the registration plate entered
for (int i=0;i<details.length-2;i=i+1)
{
if(searchword.equals(details[i])) // if the search word is in the file
{
System.out.println("Name: " +details[i-1]); // outputs name
System.out.println("Registration: "+details[i]); // outputs reg plate again
System.out.println("Address Line 1: "+details[i+1]); // outputs address
System.out.println("Address Line 2: "+details[i+2]); // outputs address line 2
fw2.write(details[i-1]+","); // separates the details when writing in to the file
fw2.write(details[i]+",");
fw2.write(details[i+1]+",");
fw2.write(details[i+2]+",");
fw2.write(speed+"/n");
fw2.close(); // file writer closes
}
}
System.out.println("The numberplate entered could not be found in the file, sorry."); // outputted if registration is not found
}
任務是在文件中搜索車牌,如果它不在文件中,那么它應該輸出它不在文件中。
上面代碼的輸出是:
the speed the car was driving at was: 39.47 mph Name: Adam Davies Registration: AA12ASD Address Line 1: 1 New Road Address Line 2: Northfleet The numberplate entered could not be found in the file, sorry.
所以我要做的方法是添加一個布爾值檢查是否找到了詳細信息。
從一開始就制作它並將其設置為false
。 那么當
if(searchword.equals(details[i]))
使布爾值true
。 在 for 循環之后,檢查布爾值是否為false
,如果為false
,請打印您抱歉找不到這個登記牌
下面的一個簡單示例如下所示:
boolean yes = false;
for (int i=0;i<details.length-2;i=i+1){
if(searchword.equals(details[i])){
System.out.println("Name: " +details[i-1]);
yes = true;
}
}
if (!yes){
System.out.println("sorry this registration plate couldn't be found")
}
}
如果我正確收集了它,代碼的目的是查找文本文件中是否存在特定的注冊號。 為此,您將整個文件內容讀入一個字符串並將其拆分為,
。 然后使用for loop
遍歷整個數組並查找您擁有的特定注冊號,即searchword
。
為了記錄,我並不完全相信您實施它的方式(本來可以更好,我不會涉及)。 但是,按照您實施的方式,您的方法的問題在於您正在執行在for loop
中搜索key
的操作,並嘗試在循環本身中執行if-else
部分。
這就是你如何做你需要的。
String a = "Hello,World,Foo,Bar,abc,def,ghi,Hello1,World1,Foo1,Bar1,abc,def,ghi";
String[] b = a.split(",");
String searchWord = "abc";
boolean hasSearchWord = false;
for(int i = 0; i < b.length; i++) {
if(b[i].equals(searchWord)) {
hasSearchWord = true;
System.out.println("Word found");
// Print whatever you need
System.out.println(b[i - 1]);
System.out.println(b[i]);
System.out.println(b[i + 1]);
}
}
if(!hasSearchWord) {
System.out.println("Word not found");
}
搜索abc
,這將產生
Word found
Bar
abc
def
Word found
Bar1
abc
def
搜索duh
,這將產生
Word not found
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.