I am trying to make a phone validation program in java, why am I getting a 'else without if' error?

huangapple go评论70阅读模式
英文:

I am trying to make a phone validation program in java, why am I getting a 'else without if' error?

问题

import java.util.*;
import javax.lang.model.util.ElementScanner6;
import java.io.*;
import static java.lang.System.*;

public class Main
{
    public static void main(String[] args) throws IOException
    {       
        Scanner input = new Scanner(new File("ValidPhoneNumber.dat"));

        int lines = Integer.parseInt(input.nextLine());

        input.nextLine();

        String phoneNumber = "";

        for (int i = 0; i < lines; i++)
        {
            phoneNumber = input.nextLine();

            if (phoneNumber.length() == 14) 
            {
                out.println("Valid");
            }
            else if (phoneNumber.length() == 12) 
            {
                out.println("Valid");
            }
            else
            {
                out.println("Invalid");
            }
        }

        input.close();
    }
}

The error on lines 63 and 72 has been corrected.

英文:

I am trying to do an extra credit assignment with phone number validation from a file. Most of the structuring was provided by the teacher and I had to fill in or add certain parts. I keep getting an 'else without if' error and from what I can see I didn't include any ';' after if/else/else if statements so I don't know why I am getting this error.

import java.util.*;
import javax.lang.model.util.ElementScanner6;
import java.io.*;   
import static java.lang.System.*; 


public class Main
{
	public static void main(String[] args) throws IOException
	{		
		Scanner input = new Scanner(new File(&quot;ValidPhoneNumber.dat&quot;)); 	
			
    int lines = input.readLine();
		
		input.nextLine();
		
		String phoneNumber = &#39; &#39;;
		 
		for (int i=0; i&lt;lines; i++)
		{
		 phoneNumber = input.nextLine;
			
			if (phoneNumber.length() == 14) 
			{
			    out.println(&quot;Valid&quot;);}
				
			   else{
         out.println(&quot;Invalid&quot;); 
			   
			}
			else if (phoneNumber.length() == 12) //error here
			{
			    out.println(&quot;Valid&quot;);}

			    else
            {out.println(&quot;Invalid&quot;);

            }
            else //error here
            {
              	out.print(&quot;Invalid&quot;);
           
			}
		
		}
		
		input.close();

	} 

} 

The error is on line 63 and line 72

else if (phoneNumber.length() == 12) 

and

 else 
            {
              	out.print(&quot;Invalid&quot;);
			}

答案1

得分: 1

在第一个错误中,您需要删除else这个词。
在第二个错误中,您需要做相同的操作,或者将第一个错误下面的else的条件更改为使用它。
在两个错误中,您连续两次调用了else
总之:您在没有if的情况下调用了else(错误显示了这一点)。

if(){
}else{
}

if(){
} else if(){
}else{
}

这是使用ifelse ifelse的正确方式。在您的代码中,它适用。

您可以在这个链接中看到解释:
如何使用if和else

英文:

In the first error you need to remove else word.
In the second error you need to do the same, or change the condition of the else under the first error to use it.
You're calling else two times sequentially in both errors.
To resume: You're calling else without a if (the error shows it).

if(){
}else{
 }

if(){
} else if(){
  }else{
   }

That's the correct way to use if, else if and else. In your code, it applies.

In this link you will see the explanation:
How to use if and else

答案2

得分: 0

Sure, here's the translation of the provided text:

有效选项

  1. 仅当
  2. 如果...否则
  3. 如果...否则如果...否则

你不能写 if..else,然后跟着 else if。

纠正这一点,它就会生效。

英文:

Valid options

 1. Only if
 2. if...else
 3. if...elseif...else

You cant write if..else followed by else if.

Correct that and it will work

答案3

得分: 0

你将

 else{

放在了 if 的前面的大括号内,因此在 else 语句之前的 那个块中 没有 if,因此出现了没有 else 的 if。
将其从那里移出,并放在关闭 if 后的大括号后面的大括号之后。

英文:

You misplaced your

 else{

you put it inside the braces in front of if, and therefore there is no if in that block preceding the else statement, hence if without else.
pull it out of there and put it after the brace that closes the open brace after if.

答案4

得分: 0

如果您需要指定多个条件,那么您应该使用 if -> 多个 else if -> else,这意味着 else 块是最后一个。

如上所述,新代码必须如下所示:

for (int i = 0; i < lines; i++) {
    phoneNumber = input.nextLine;
    if (phoneNumber.length() == 14) {
        out.println("有效");
    } else if (phoneNumber.length() == 12) {
        out.println("有效");
    } else {
        out.println("无效");
    }
}
英文:

If you need to specify multiple conditions, then you should use if -> multiple else if -> else, what means that else block is the last one.
As explained above, the new code must look like the following:

for (int i = 0; i &lt; lines; i++) {
    phoneNumber = input.nextLine;
    if (phoneNumber.length() == 14) {
        out.println(&quot;Valid&quot;);
    } else if (phoneNumber.length() == 12) {
        out.println(&quot;Valid&quot;);
    } else {
        out.println(&quot;Invalid&quot;);
    }
}

答案5

得分: -1

for (int i = 0; i < lines; i++) {
    phoneNumber = input.nextLine;
    if (phoneNumber.length() == 14 || phoneNumber.length() == 12 ) {
        out.println("Valid");
    } else {
        out.println("Invalid");
    }
}
英文:
for (int i = 0; i &lt; lines; i++) {
    phoneNumber = input.nextLine;
    if (phoneNumber.length() == 14 || phoneNumber.length() == 12 ) {
        out.println(&quot;Valid&quot;);
    } else {
        out.println(&quot;Invalid&quot;);
    }
}

huangapple
  • 本文由 发表于 2020年8月29日 02:43:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63639329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定