Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 看-否则如果声明:我把它放在哪里_Java_Eclipse_If Statement_Task_Eclipse Kepler - Fatal编程技术网

Java 看-否则如果声明:我把它放在哪里

Java 看-否则如果声明:我把它放在哪里,java,eclipse,if-statement,task,eclipse-kepler,Java,Eclipse,If Statement,Task,Eclipse Kepler,我不知道把ELSE语句放在哪里,如果我把它放在for循环中,在if语句之后,它会在ELSE语句中重复多次。但是如果我在ELSE语句中输入我想要输出的内容,例如“对不起,这个车牌找不到”在FOR循环之外,那么它会在我不想要的时候输出 else // or { // this code reads a file in String full=""; try { FileR

我不知道把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
            }
else//或
{
//这段代码在中读取一个文件
字符串满=”;
尝试{FileReader=newfilereader(“address.txt”);//在名为address的文件中读取新文件读取器
BufferedReader OwnerDetails=new BufferedReader(reader);//高效读取文件
字符串行;//分配字符串
而((line=OwnerDetails.readLine())!=null){//读取所有文件
满=满+行;
}
reader.close();//如果读取器关闭,它将在
}捕获(IOE异常){
e、 printStackTrace();
}
//System.out.println(full);//读入整个文件并输出,用于检查文件是否已写入
//一旦程序完成,它将被注释掉,但在测试时使用
String[]details=full.split(,“”;//将信息拆分为数组
String searchword=registration;//searchword设置为输入的车牌

对于(int i=0;i如果我收集正确,代码的目的是查找文本文件中是否存在特定的注册号。为此,您将整个文件内容读入一个字符串,并在
上拆分它。然后使用
For循环
遍历整个数组并查找特定的注册号您拥有的号码,即,
searchword

为了记录在案,我并不完全相信您实施它的方式(本可以更好,我将不涉及)但是,按照您的实现方式,您的方法存在的问题是,您正在执行在
for循环
中搜索
的操作,并尝试在循环本身中同时执行
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");
    }
搜索
duh
时,将生成

Word found
Bar
abc
def
Word found
Bar1
abc
def
Word not found

因此,我要做的是,如果找到了详细信息,我将添加一个布尔检查

从一开始就将其设置为
false
。 那么什么时候

if(searchword.equals(details[i]))
使布尔值
为真
。 在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")
    }
}
布尔值yes=false;

对于(int i=0;我建议先看一下:@Rakesh-我还是不明白,我读了另一个问题,他们说把else语句放在for循环之外,但这使得它每次都输出,我需要布尔值吗?很难理解应用程序需要做什么,因为这里没有人知道上下文。我不确定w当你说在for循环之外使用else时,你的意思是什么?
else
条件如果没有
if
,就永远不可能存在,它必须总是在
if
语句之后。否则,它将无法编译。我不知道当你将else“置于
for
循环之外”时,你的代码是如何编译的。可能有一个预先存在的
if
条件而没有其他条件?。是否要查看我的代码?我真的需要帮助,我不知道我做错了什么。重点是你的问题没有真正意义。请参阅此链接,了解如何问一个最小、完整且可验证的问题: