简体   繁体   中英

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. 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 // 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.

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 . then when

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

make the boolean 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

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 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.

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

Word found
Bar
abc
def
Word found
Bar1
abc
def

When searching for duh , this will produce

Word not found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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