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

时间:2015-10-28 17:09:41

标签: java eclipse if-statement task eclipse-kepler

我不知道在哪里放置我的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.

2 个答案:

答案 0 :(得分:1)

所以我这样做是因为我会添加一个布尔检查是否找到了详细信息。

从一开始就制作它并将其设置为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")
    }
}

答案 1 :(得分:0)

如果我正确地收集了它,代码的目的是找出文本文件中是否存在特定的注册号。为此,您将整个文件内容读入字符串并将其拆分为,。然后使用for loop遍历整个数组并查找您拥有的特定注册号,即searchword

为了记录,我并不完全相信你实施它的方式(可能会更好,我不会涉及)。但是,按照您实施的方式,您的方法的问题是您正在执行key中搜索for loop的操作,并尝试同时执行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
相关问题