Creating dialog windows - java

I'm trying to create a dialog window where I ask for a persons name with the format: Lastname, Surname
I'm then trying to show just the surname name in a new dialog window with the format: Hello! SURNAME!
This is my code so far:
import javax.swing.*;
public class Surname {
public static void main(String[] arg) {
String a = JOptionPane.showInputDialog(null, "Write your name: Lastname, surname ");
int i, j;
i = a.lastIndexOf(???);
j = a.indexOf(',' + 1);
a = a.substring(i, j);
JOptionPane.showMessageDialog(null, "Hello! " + a.toUpperCase()); }}

Your substring is not correct, for the start you'll need the index of the comma, for the end simply the length of the string:
int i, j;
i = a.indexOf(',') + 2;
j = a.length();
a = a.substring(i, j);

You can extract the surname by splitting the string by ", ".
For example
String surname = "Novovic, Felix".split(", ")[0];
Since we are accessing an array here which size is fully determined by the input of the user, i.e. the user inputs "Novovic, Felix, Hello, World" you should reassure that the input is in the correct format before you access the array.
For example, by checking that the array length = 2

Using split() this will do:
public static void main(String[] args) {
String a = JOptionPane.showInputDialog(null, "Write your name: Lastname, surname ");
String[] nameParts = a.split(",");
JOptionPane.showMessageDialog(null, "Hello! " + nameParts[1].trim().toUpperCase());
}
... but you would probably want to add some more error handling. So this is only a bare bone example

Related

How to check user input is part of the array in Java

I wanna ask if this is possible. So I have this program will enter a for loop to get the user input on number of subjects. Then after he will enter the subjects listed in the array as his guide. My goal is that I want to check his subjects if it is really inside the array that I made. I made a program but I don't know where to put the part that the program will check the contents.
My goal:
Enter the corresponding code for the subjects you have chosen: user will input 8
Enter the number of subjects you wish to enroll: be able to type the whole subject name like (MATH6100) Calculus 1
then the program will check if the subjects entered are part of the elements inside the array
UPDATE:
I have made another but the problem is that I don't know where to put the code fragment wherein it will check the contents of the user input for list of subjects he wish to enroll.
Here is the code:
private static void check(String[] arr, String toCheckValue){
boolean test=Arrays.asList(arr).contains(toCheckValue);
System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
}
public static void main(String[] args){
String arr[]={"(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1", "(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1", "(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1"};
Scanner input1=new Scanner(System.in);
System.out.print("\nEnter the number of subjects you wish to enroll: ");
int number_subjects1=input1.nextInt();
String []subjects1=new String[number_subjects1];
//else statement when user exceeds the number of possible number of subjects
if(number_subjects1<=8){
for(int counter=0; counter<number_subjects1; counter++){
System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): " + (counter+1));
subjects1[counter]=input1.next();
}
String toCheckValue=subjects1[0];
System.out.println("Array: " +Arrays.toString(arr));
check(arr, toCheckValue);
System.out.println("\nPlease check if these are your preferred subjects:");
for(int counter=0; counter<number_subjects1; counter++){
System.out.println(subjects1[counter]);
}System.out.println("********************************** \n" + "\tNothing Follows");
System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
Scanner character=new Scanner(System.in);
String answer_1subjectserrors=character.nextLine();
System.out.println(answer_1subjectserrors + "Based on your answer, you need to refresh thae page and try again.");
}
}
}
I believe the issue is that you are checking your class course codes against an array which contains both the class code AND the class description.
You ask the user to enter the class code but then you use that code to check for its existence in an array containing both the code & description. The contains in List (collections) is not the same as the contains in String.
I have slightly modified your code so you may get the desired result.
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SOQuestion {
private static void check(String[] arr, String toCheckValue){
List courses = Arrays.asList(arr);
boolean test=courses.contains(toCheckValue);;
System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
}
public static void main(String[] args) {
String class_codes_and_descriptions[] = { "(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1",
"(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1",
"(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1" };
String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };
Scanner input1 = new Scanner(System.in);
System.out.print("\nEnter the number of subjects you wish to enroll: ");
int number_subjects1 = input1.nextInt();
String[] subjects1 = new String[number_subjects1];
// else statement when user exceeds the number of possible number of subjects
if (number_subjects1 <= 8) {
for (int counter = 0; counter < number_subjects1; counter++) {
System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
+ (counter + 1));
subjects1[counter] = input1.next();
}
String toCheckValue = subjects1[0];
System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
check(class_codes, toCheckValue);
System.out.println("\nPlease check if these are your preferred subjects:");
for (int counter = 0; counter < number_subjects1; counter++) {
System.out.println(subjects1[counter]);
}
System.out.println("********************************** \n" + "\tNothing Follows");
System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
Scanner character = new Scanner(System.in);
String answer_1subjectserrors = character.nextLine();
System.out.println(
answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
}
}
}
When you are debugging always try to break down the statements into steps so you know where the error is. For example instead of boolean test=Arrays.asList(arr).contains(toCheckValue);
break it down to two steps like this :
List courses = Arrays.asList(arr);
boolean test=courses.contains(toCheckValue);
That way you will have an easier time checking for issues.
Second request is to always look at the API. Skim over the API to look at the method that you are using to understand it better.
For example if you are using contains method of List then look up the API here:
https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/List.html#contains(java.lang.Object)
Of course since this is Oracle's Java the explanation is imprecise & not straightforward but it is usually helpful.
I would recommend using a different data structure than plain arrays. Since you are already using List why not use another collections data structure like HashMap?
The original poster may want to look at a slightly refactored and cleaned up version of the code & try to figure out how to check for all courses since that is his next question. I believe that should become obvious with a more refactored code:
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SOQuestion {
public static String class_codes_and_descriptions[] = { "(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1",
"(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1",
"(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1" };
public static String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int desired_number_of_subjects = input_desired_number_of_subjects(input);
String[] desired_subjects = enter_subjects(desired_number_of_subjects, input);
String toCheckValue = desired_subjects[0];
System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
check(class_codes, toCheckValue);
pls_confirm_desired_subjects(desired_number_of_subjects, desired_subjects);
System.out.println("********************************** \n" + "\tNothing Follows");
System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
Scanner character = new Scanner(System.in);
String answer_1subjectserrors = character.nextLine();
System.out.println(
answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
}
private static int input_desired_number_of_subjects(Scanner input) {
System.out.print("\nEnter the number of subjects you wish to enroll: ");
int take_number_of_subjects = input.nextInt();
// TODO: else statement when user exceeds the number of possible number of subjects
return take_number_of_subjects;
}
private static String[] enter_subjects(int desired_subjects_count , Scanner input_desired_subjects) {
String[] subjects_totake = new String[desired_subjects_count];
if (desired_subjects_count <= 8) {
for (int counter = 0; counter < desired_subjects_count; counter++) {
System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
+ (counter + 1));
subjects_totake[counter] = input_desired_subjects.next();
}
}
return subjects_totake;
}
private static void check(String[] arr, String toCheckValue){
List courses = Arrays.asList(arr);
boolean test=courses.contains(toCheckValue);
System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
}
private static void pls_confirm_desired_subjects(int take_number_of_subjects, String[] take_subjects) {
System.out.println("\nPlease check if these are your preferred subjects:");
for (int counter = 0; counter < take_number_of_subjects; counter++) {
System.out.println(take_subjects[counter]);
}
}
}
I will shortly edit the above but a hint is : you can use a for loop to go over the entered desired_subjects array and do a check on each one of the subjects, perhaps?
The following checks for all the courses (though this is not how I would check the courses)
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SOQuestion {
public static String class_codes_and_descriptions[] = { "(MATH6100) Calculus 1", "(ITE6101) Computer Fundamentals", "(ITE6102) Computer Programming 1",
"(GE6100) Understanding the Self", "(GE6106) Purposive Comunication 1", "(ETHNS6101) Euthenics 1",
"(PHYED6101) Physical Fitness", "(NSTP6101) National Service Training Program 1" };
public static String class_codes[] = { "MATH6100", "ITE6101", "ITE6102","GE6100", "GE6106", "ETHNS6101","PHYED6101", "NSTP6101" };
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int desired_number_of_subjects = input_desired_number_of_subjects(input);
String[] desired_subjects = enter_subjects(desired_number_of_subjects, input);
check_all_desired_subjects(desired_subjects);
pls_confirm_desired_subjects(desired_number_of_subjects, desired_subjects);
System.out.println("********************************** \n" + "\tNothing Follows");
System.out.print("\nIf you have enter some errors please press Y and refresh the form (Y/N): ");
Scanner character = new Scanner(System.in);
String answer_1subjectserrors = character.nextLine();
System.out.println(
answer_1subjectserrors + "Based on your answer, you need to refresh the page and try again.");
}
private static int input_desired_number_of_subjects(Scanner input) {
System.out.print("\nEnter the number of subjects you wish to enroll: ");
int take_number_of_subjects = input.nextInt();
// TODO: else statement when user exceeds the number of possible number of subjects
return take_number_of_subjects;
}
private static String[] enter_subjects(int desired_subjects_count , Scanner input_desired_subjects) {
String[] subjects_totake = new String[desired_subjects_count];
if (desired_subjects_count <= 8) {
for (int counter = 0; counter < desired_subjects_count; counter++) {
System.out.println("Enter the corresponding code for the subjects you have chosen (EX. MATH6100): "
+ (counter + 1));
subjects_totake[counter] = input_desired_subjects.next();
}
}
return subjects_totake;
}
private static void check_all_desired_subjects(String[] desired_subjects) {
System.out.println("Array: " + Arrays.toString(class_codes_and_descriptions));
for (String subject_code_to_check:desired_subjects ) {
check(class_codes, subject_code_to_check);
}
}
private static void check(String[] arr, String toCheckValue){
List courses = Arrays.asList(arr);
boolean test=courses.contains(toCheckValue);
System.out.println("Is/Are " + toCheckValue + " present in the array: " + test);
}
private static void pls_confirm_desired_subjects(int take_number_of_subjects, String[] take_subjects) {
System.out.println("\nPlease check if these are your preferred subjects:");
for (int counter = 0; counter < take_number_of_subjects; counter++) {
System.out.println(take_subjects[counter]);
}
}
}

2D String search in Java

This is my code:
import java.util.*;
import java.util.Arrays;
public class PhoneNumbers
{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
String phoneList[][] =
{
{"Harrison, Rose: ", "James, Jean: ", "Smith, William: ", "Smith, Brad: "},
{"415-555-2234", "415-555-9098", "415-555-1785", "415-555-9224"}
};
System.out.println(Arrays.deepToString(phoneList)); //this line is to make sure the 2D arrays work
String input;
System.out.print("Enter the first few letters of a last name to search for: ");
input = scan.nextLine();
int match = -1;
for(int i = 0; i < phoneList.length; i++)
{
if(phoneList[i].indexOf(input))
{
System.out.println(phoneList[i]);
match = i;
break;
}
else
System.out.println("There is no match.");
}
}
}
My goal is:
I have a 2D array, i got name in one and the phone number in another.
I am trying to allow user to enter first few letters of a last name and do a search that and display the matching search along with the phone number(this would be my next challenge).
Thank you,
Here is your Answer :
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int refNumber=-1;
String phoneList[][] =
{
{"Harrison, Rose: ", "James, Jean: ", "Smith, William: ", "Smith, Brad: "},
{"415-555-2234", "415-555-9098", "415-555-1785", "415-555-9224"}
};
Scanner scanner = new Scanner(System.in);
System.out.print("Enter String to be searched ->");
String input = scanner.nextLine();
//System.out.println(input);
for (int i=0; i<phoneList[0].length; i++) {
if (phoneList[0][i].contains(input)) {
refNumber = i;
}
}
System.out.println("Name ->"+phoneList[0][refNumber]);
System.out.println("Phone No ->"+phoneList[1][refNumber]);
}
}
Create a dictionary (e.g. trie tree) for all the names you have. While creating/updating your search dictionary, at each node, keep an integer array which gives you the list of index of all the matching names. As the user provides input, you can keep on refining the result at runtime, and when he selects any one of the provided suggestions, you'll have the index to it.
Rest is simple, using that index, display the phone number of the selected person.
I did some refactoring:
using a Map: name => phone
public static void main(String[] args)
{
String phoneList[][] =
{
{"Harrison, Rose: ", "James, Jean: ", "Smith, William: ", "Smith, Brad: "},
{"415-555-2234", "415-555-9098", "415-555-1785", "415-555-9224"}
};
// Make a Map
Map<String,String> mss=new HashMap<String,String>();
for(int i = 0; i < phoneList.length; i++)
mss.put(phoneList[0][i], phoneList[1][i]) ;
String input;
System.out.print("Enter the first few letters of a last name to search for: ");
Scanner scan = new Scanner(System.in);
input = scan.nextLine();
String match="";
for (String name: mss.keySet())
if (name.startsWith(input))
{
System.out.println("NAME:"+name+ " PHONE:"+mss.get(name));
match=name;
break;
}
if (match.equals(""))
System.out.println("There is no match.");
}
One simple approach is to match the beginning of the input String:
for(int x=0; x<phoneList[1].length; x++)
if(phoneList[0][x].toLowerCase().startsWith(input.toLowerCase()))
System.out.println("Numbers which matched: " + phoneList[1][x]);
Program output:
//Using "smith" as input:
Numbers which matched: 415-555-1785
Numbers which matched: 415-555-9224

how to add a value of array for a specified index without replacing it?

THIS IS ONE METHOD CLASS ONLY .
System.out.print("1. Add Customer..............................................P500.00\n");
case 1://Add Customer
for (int x = 0; x < CustomerInfo.length; x++){// Start For Records
x++;
System.out.print("\nConfimation Number: ");
CustomerInfo[x][0][0][0][0][0] = br.readLine();
System.out.print("\nFirst Name: ");
CustomerInfo[x][1][0][0][0][0] = br.readLine();
System.out.print("Last Name: ");
CustomerInfo[x][0][1][0][0][0] = br.readLine();
System.out.print("Guest: ");
CustomerInfo[x][0][0][1][0][0] = br.readLine();
System.out.print("Night: ");
CustomerInfo[x][0][0][0][1][0] = br.readLine();
System.out.print("Accommodation: ");
CustomerInfo[x][0][0][0][0][1] = br.readLine();
System.out.println();
break;
}
break;
for (int x = 0; x < CustomerInfo.length; x++){
if (CustomerInfo[x][0][0][0][0][0] != null){
if (CustomerInfo[x][1][0][0][0][0] != null){
if (CustomerInfo[x][0][1][0][0][0] != null){
if (CustomerInfo[x][0][0][1][0][0] != null){
if (CustomerInfo[x][0][0][0][1][0] != null){
if (CustomerInfo[x][0][0][0][0][1] != null){
System.out.print("\n|---------------------------------------------------------------------|\n");
System.out.print("\n\nConfirmation Number: |---------------> " + CustomerInfo[x][0][0][0][0][1]);
System.out.print("\nGuest Name: |---------------> " + CustomerInfo[x][1][0][0][0][0] + " " + CustomerInfo[x][0][1][0][0][0]);
System.out.print("\nNumber Of Guest: |---------------> " + CustomerInfo[x][0][0][1][0][0]);
System.out.print("\nNumber Of Nights: |---------------> " + CustomerInfo[x][0][0][0][1][0]);
System.out.println("\nAccommodations: |---------------> " + CustomerInfo[x][0][0][0][0][1]);
i add customer for first time then if i add customer for the second time the first customer wasn't recorded.
i noticed that the problem is the increment.
for (int x = 0; x < CustomerInfo.length; x++)
because i use break after that.
break;
but i want to accomplish is to add customer and when i add another one the previous should display along with the new.
what i asked is i want to add more array values one after one . or like after i add value to my '[x]' i want to add again, but not at the same time
any suggestions ?
i really need your help.
You probably meant to declare CustomerInfo as follows:
String[] customerInfo = new String[6];
and have each array - hold the information of one customer, where first-name will be stored on the first index, last-name on the second and etc.
By doing something like CustomerInfo[][][][][][] - the code tries to declare/access a multidimensional (6-dimensional) array - which is probably not what you wanted.
Now Java is an object oriented programming language, so instead of multidimensional arrays you should try to design your code to use objects - it will make your code easier to read and maintain.
So first, create a CustomerInfo class:
class CustomerInfo {
String confirmationNumber;
String firstName;
String lastName;
String guest;
String night;
String accommodation;
public CustomerInfo(String confirmationNumber,
String firstName,
String lastName,
String guest,
String night,
String accommodation) {
this.confirmationNumber = confirmationNumber;
this.firstName = firstName;
this.lastName = lastName;
this.guest = guest;
this.night = night;
this.accommodation = accommodation;
}
}
and now that you have such a "holder" for the data, you can read the user input and save it into a list of CustomerInfo:
public static void main(String[] args) {
...
List<CustomerInfo> customerInfos = new ArrayList<CustomerInfo>();
for (int i = 0; i < customerInfos.length; i++){// Start For Records
System.out.print("\Confirmation Number: ");
String confirmationNumber = br.readLine();
System.out.print("\nFirst Name: ");
String firstName = br.readLine();
System.out.print("Last Name: ");
String lastName = br.readLine();
System.out.print("Guest: ");
String guest = br.readLine();
System.out.print("Night: ");
String night = br.readLine();
System.out.print("Accommodation: ");
String accommodation = br.readLine();
CustomerInfo customer = new CustomerInfo(confirmationNumber, firstName, lastName, guest, night, accommodation);
customerInfos.add(customer);
System.out.println();
}
}
and if you want to do even better, add some validations on top, for example, if guest is a number, you can read it as int or try to parse it as an int - and if the user entered something weird - display a proper error message. And etc.
case 1://Add Customer
for (int x = 0; x < CustomerInfo.length; x++){// Start For Records
**x++;**
You have problem with your loop. You increase your incremental variable when you start loop and when you finish loop two times.
That means you start not with 0 but with 1. But you want to check that if there is a customer at 0.

Searching an arraylist of anonymous objects in Java for a specfic variable in those objects

I have an assignment that creates an array list that holds contact info objects. Then I have to ask which field to search from and the information to search. I am having a hard time figuring the search part out. I am trying to get into each object and specify which variable to search and find a match. Here is the sample output I have to create. My teacher wants to use this code structure for the main and DuEdAddressBook.
This is the area I am having hard time figuring it out. I want to learn but when you get stuck and all the examples in the book and online do not have this type of scenario it becomes frustrating. Thanks for any input.
search method:
Receive ArrayList as argument
Output Search Menu (see example at bottom)
Utilize a switch and search ArrayList for field specified.
Return index number if entry found or -1 if not found
Please Enter First Name: Nick
Please Enter Last Name: Dewey
Please Enter Street Address: 3232 Longridge Rd.
Please Enter City, State: Del City, OK
Please Enter Zip Code: 73115
Please Enter Field to Search: 1
Please enter value to search for: Nick
First Name: Nick
Last Name: Dewey
Street Address: 3232 Longridge Rd.
City, State: Del City, OK
Zip Code: 73115
Here is my code so far.
import java.util.ArrayList;
import java.util.Scanner;
public class DurrieEdwardChapter10
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String a,b,c,d,e;
ArrayList<DuEdAddressBook> aBook = new ArrayList<DuEdAddressBook>();
for (int count = 0; count < 1; count++)
{
DuEdAddressBook entry = new DuEdAddressBook();
System.out.print("Please Enter First Name: ");
a = stdIn.nextLine();
System.out.print("Please Enter Last Name: ");
b = stdIn.nextLine();
System.out.print("Please Enter Street Address:");
c = stdIn.nextLine();
System.out.print("Please Enter City, State:");
d = stdIn.nextLine();
System.out.print("Please Enter Zip Code:");
e = stdIn.nextLine();
entry.addEntry(a,b,c,d,e);
aBook.add(count,entry);
}
int foundIndex = DuEdAddressBook.search(aBook);
System.out.println();
if (foundIndex > -1)
{
aBook.get(foundIndex).display();
}
else
{
System.out.println("No Entry Found");
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class DuEdAddressBook
{
String first;
String last;
String street;
String state;
String zip;
public DuEdAddressBook()
{
}
public DuEdAddressBook(String a, String b, String c, String d, String e)
{
first = a;
last = b;
street = c;
state = d;
zip = e;
}
public void addEntry(String a,String b,String c,String d, String e)
{
first = a;
last = b;
street = c;
state = d;
zip = e;
}
public static int search(ArrayList<DuEdAddressBook> a)
{
Scanner stdIn = new Scanner(System.in);
String searchNum;
String searchValue;
System.out.print("Please Enter Field to Search:");
searchNum = stdIn.nextLine();
System.out.print("Please Enter Value to Search For:");
searchValue = stdIn.nextLine();
int n = 0; // index of search result
return n;
}
public void display()
{
System.out.println("First Name:" + first);
System.out.println("Last Name:" + last);
System.out.println("Street Address:" + street);
System.out.println("City,State:" + state);
System.out.println("Zip Code:" + zip);
}
}
Easiest way IMHO is to find the objects via reflection.
Iterate over the List of objects and analyze its field values.
something like:
Field[] fields = DuEdAddressBook.class.getFields(); //or getDeclaredFields()
for (Field f : fields)
{
String fieldName = f.getName();
Object fieldValue = f.getValue(theDuEdObjectToAnalyze);
//here you can compare the fieldvalue with the value the user entered
}

How can I pause a loop until I get a users input?

I am a beginner at Java and I am making a fun project for myself to learn more about java, I plan on randomizing videos from a preset list and displaying it to the user.
I am having trouble stopping the loop. Once you type in the kind of video you want to watch the program automatically re-loops, but i want it to ask you if you want to watch another video before relooping. Here is what I have so far:
import java.util.Scanner;
import java.util.Random;
public class YoutubeGenerator {
public static void main(String[] args) {
int randomstring = 0;
for ( ; ; ) {
System.out.println("\n ---------Youtube Video Generator 0.001 BETA------------------ \n");
System.out.println("\n ********* DISCLAIMER: WARNING - This program may direct you to violent, disturbing content, and/or vulgar language and is intended for a MATURE person only. ********* \n \n");
Scanner scan = new Scanner (System.in);
System.out.println("What kind of video from the list would you like to watch? \n");
System.out.println("Cute \n" + "Funny \n" + "WTF \n" + "Interesting \n" + "Documentary \n");
System.out.print("I want to watch: ");
String userString = scan.next();
Random rand = new Random();
if(userString.equalsIgnoreCase("cute")){
String cute1 = "https://www.youtube.com/watch?v=EdCVijVT7Wk";
String cute2 = "http://youtu.be/-XCvPptsfhI?t=7s";
String cute3 = "https://www.youtube.com/watch?v=-nkEPsSsH68";
String cute4= "https://www.youtube.com/watch?v=FZ-bJFVJ2P0";
String cute5 = "https://www.youtube.com/watch?v=argCvDpk_KQ";
System.out.println("Here's a cute video you can watch: " +cute5) ;
}
if(userString.equalsIgnoreCase("funny")){
System.out.println("Here's a funny you can watch:");
String funny1 = "https://www.youtube.com/watch?v=I59MgGlh2Mg";
String funny2 = "http://www.youtube.com/watch?v=HKMNKS-9ugY";
String funny3 = "https://www.youtube.com/watch?v=_qKmWfED8mA";
String funny4= "https://www.youtube.com/watch?v=QDFQYKPsVOQ";
String funny5 = "https://www.youtube.com/watch?v=ebv51QNm2Bk";
}
if(userString.equalsIgnoreCase("wtf")){
System.out.println("Here's a WTF video you can watch:");
String wtf1 = "https://www.youtube.com/watch?v=UfKIoSv2YEg";
String wtf2 = "https://www.youtube.com/watch?v=hcGvN0iBA5s";
String wtf3 = "http://www.youtube.com/watch?v=vxnyqvejPjI&feature=youtu.be&t=1m37s";
String wtf4= "https://www.youtube.com/watch?v=10NJnT6-sSE";
String wtf5 = "https://www.youtube.com/watch?v=DQeyjgSUlrk";
}
if(userString.equalsIgnoreCase("interesting")){
System.out.println("Here's an interesting video you can watch:");
String int1 = "https://www.youtube.com/watch?v=fYwRMEomJMM";
String int2 = "https://www.youtube.com/watch?v=1PmYItnlY5M&feature=youtu.be&t=32s";
String int3 = "https://www.youtube.com/watch?v=HgmnIJF07kg";
String int4= "https://www.youtube.com/watch?v=cUcoiJgEyag";
String int5 = "https://www.youtube.com/watch?v=BePoF4PrwHs";
}
if(userString.equalsIgnoreCase("documentary")){
System.out.println("Here's a space video you can watch: ");
String doc1 = "https://www.youtube.com/watch?v=wS_WlzdOc_A";
String doc22 = "https://www.youtube.com/watch?v=8n0SkIGARuo";
String doc33 = "https://www.youtube.com/watch?v=6LaSD8oFBZE";
String doc4= "https://www.youtube.com/watch?v=zvfLdg2DN18";
String doc5 = "https://www.youtube.com/watch?v=8af0QPhJ22s&hd=1";
}
}
}
}
Insert the following code right before the closing brace of your loop:
System.out.println("Do you want to watch another video? Enter yes or no");
String decision = scan.next();
if (decision.equalsIgnoreCase("no"))
break;

Categories

Resources