Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We are told to use the SWITCH on this program.....thanks for the help earlier bu

ID: 3528335 • Letter: W

Question

We are told to use the SWITCH on this program.....thanks for the help earlier but we are told to EXTRACT the word after the last period. I can't use split or exception......I have not extracted the word on this program yet. if i am doing a switch statement and I inputted an address of a web, my program is compiling but not looking at the last part .... I want it to look at the gov word or edu word but it just kept on giving me wrong website...here is my program public class Webadd { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("enter address"); String address = scan.next(); switch(address) { case "1": address = "gov"; System.out.println("gov site"); break; case "2": address = "edu"; System.out.println("univ site"); break; default: System.out.println("wrong website"); } } }

Explanation / Answer

import java.util.Scanner;

public class Webadd {
public static void main(String[] args) {
int lastNum;
Scanner scan = new Scanner(System.in);
System.out.println("enter address");
String address = scan.next();
int lastIndex = address.lastIndexOf('.');
String last = address.substring(lastIndex + 1);
if (last.equals("gov"))
lastNum = 1;
else
lastNum = 2;

switch (lastNum) {
case 1:
System.out.println("gov site");
break;
case 2:
System.out.println("univ site");
break;
default:
System.out.println("wrong website");
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote