here is the code I have: //page 277 #49 import java.util.Scanner; public class E
ID: 3654148 • Letter: H
Question
here is the code I have: //page 277 #49 import java.util.Scanner; public class Email { public static void main(String[] args) { String myWeb; Scanner scan = new Scanner(System.in); System.out.print("Enter the web address "); myEmail = scan.nextLine(); if (myEmail.indexOf("gov")) System.out.println("This is an gov address!"); else System.out.println(myEmail + " is not a valid address!"); } } I need to write a program that sees if the webl is .gov or .edu or .com or .org of none of the above I know that there is something wrong with the indexOf partExplanation / Answer
Your Error: indexOf("com") givesthe index of first occurence of com in the string, it's an integer value. it cannot be used for if Statement. If "com" is not found then it return negative value . so if statement should be if(myEmail.indexOf("gov")>=0) //page 277 #49 import java.util.Scanner; public class Email { public static void main(String[] args) { String myEmail; Scanner scan = new Scanner(System.in); System.out.print("Enter the web address "); myEmail = scan.nextLine(); if (myEmail.indexOf(".gov") >= 0) System.out.println("This is an gov address!"); else if (myEmail.indexOf(".com") >= 0) System.out.println("This is an com address!"); else if (myEmail.indexOf(".org") >= 0) System.out.println("This is an org address!"); else if (myEmail.indexOf(".edu") >= 0) System.out.println("This is an edu address!"); else System.out.println(myEmail + " is not a valid address!"); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.