0

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";
    }  
    }
}
}
3
  • Its not clear exactly what you're trying to do, but I think that it may become clearer to you if you separate the chunk of code that handles the user's input from the main input loop. Make it a separate method. Then the loop will be simple: get input, process, repeat. Commented Jul 27, 2014 at 20:20
  • 2
    Do not say new Scanner(System.in) inside a loop. Do that only once. Commented Jul 27, 2014 at 20:26
  • place the for-loop line after this line Scanner scan = new Scanner (System.in);?! Commented Jul 27, 2014 at 20:30

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

@codeacode you're welcome. One more small hint: while (true) {...} is perhaps more readable code than for ( ; ; ) {...}, but both loops do exactly the same thing. Also, you might eventually look into GUI creation to make your application more user-friendly and for ease of input validation: docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.