简体   繁体   English

看 - else if 语句:我把它放在哪里

[英]LOOK - else if statement: where do i put it

I don't know where to put my ELSE statement, if I put it in the for loop, after the IF statement, it repeats the thing inside the else statement loads of times.我不知道把我的 ELSE 语句放在哪里,如果我把它放在 for 循环中,在 IF 语句之后,它会多次重复 else 语句中的内容。 But if I put whats in ELSE statement that I want to be outputted, eg 'sorry this registration plate couldn't be found' outside the FOR loop then it outputs when i dont want it to.但是,如果我在 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 task is to search for a registration plate in a file, and if its not in the file, then it should output that its not in the file.任务是在文件中搜索车牌,如果它不在文件中,那么它应该输出它不在文件中。

the output of the code above is: 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.上面代码的输出是: 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.

So the way I would do it is I would add a boolean checking if the details have been found.所以我要做的方法是添加一个布尔值检查是否找到了详细信息。

start off with making it at the very beginning and set it to false .从一开始就制作它并将其设置为false then when那么当

if(searchword.equals(details[i]))

make the boolean true .使布尔值true after the for loop, check if the boolean is false , and if it is false , print your sorry this registration plate couldn't be found在 for 循环之后,检查布尔值是否为false ,如果为false ,请打印您抱歉找不到这个登记牌

a simple example following looks like this:下面的一个简单示例如下所示:

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")
    }
}

If I gathered it correctly, the intent of the code is to find whether a particular registration number exists in the text file.如果我正确收集了它,代码的目的是查找文本文件中是否存在特定的注册号。 For this, you are reading the entire file content into a string and splitting it on , .为此,您将整个文件内容读入一个字符串并将其拆分为, And then using a for loop to iterate through the entire array and look for a particular registration number that you have, ie, searchword .然后使用for loop遍历整个数组并查找您拥有的特定注册号,即searchword

For the record, I'm not entirely convinced with the way in which you have implemented it (could have been way better, which I won't touch upon).为了记录,我并不完全相信您实施它的方式(本来可以更好,我不会涉及)。 However, going with the way that you have implemented, the problem with your approach is that you are performing an operation of searching for a key in the for loop , and try to do both the if-else part within the loop itself.但是,按照您实施的方式,您的方法的问题在于您正在执行在for loop中搜索key的操作,并尝试在循环本身中执行if-else部分。

This is how you could do what you need.这就是你如何做你需要的。

    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");
    }

When searching for abc , this will produce搜索abc ,这将产生

Word found
Bar
abc
def
Word found
Bar1
abc
def

When searching for duh , this will produce搜索duh ,这将产生

Word not found

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM