繁体   English   中英

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

[英]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.

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