I need help with my Java class, i saw another response to this lab in Chegg, but
ID: 3783824 • Letter: I
Question
I need help with my Java class, i saw another response to this lab in Chegg, but is waay bad.
I'm stuck, make sure that the output prints the name of the cities in upper case with the "to" in lower case between the city string. This is what I have so far
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Itinerary
{
public static void main(String[] args)
{
List<String> itinerary = new ArrayList<>();
String travelRoute;
StringBuilder sb;
Scanner input = new Scanner(System.in);
String city;
boolean flag = true;
while(flag)
{
System.out.println("Destination: ");
//
city = input.next();
if(city.equalsIgnoreCase("done"))
{
flag= false;
//
}
itinerary.add(city);
}
// starts at index 0| until the list's size minus 1| one at a time
for (int i = 0; i < itinerary.size()-1; i++) {
String s = itinerary.get(i).toUpperCase();
sb = new StringBuilder(s);
Explanation / Answer
solution:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Itinerary
{
public static void main(String[] args)
{
// declaration and assignment of varibales
List<String> itinerary = new ArrayList<>();
String travelRoute;
StringBuilder sb;
Scanner input = new Scanner(System.in);
String city;
int i=0;
boolean flag = true;
// while loop which runs untill input entered by user is Done,donE..etc.
while(flag)
{
System.out.println("Destination: ");
city = input.next();
if(city.equalsIgnoreCase("done"))
{
flag= false;
break;
}
itinerary.add(city);
}
//StringBuilder object is created
sb = new StringBuilder();
//while loop which takes one by one value from itinerary and adds to
//StringBuilder object with uppercase
while(i < itinerary.size())
{
String s = itinerary.get(i).toUpperCase();
sb.append(s);
i++;
if (i != itinerary.size())
{
sb.append(" to ");
}
}
System.out.println("travel route: " + " " + sb);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.