Need help with Java while loops

Guys, I need help with my java code, I have been working on this problem for hours. The problem is that when my inner while loop turns out true, it returns to the outer while loop, and the inner while loop doesn’t run anymore. Is there another alternative for this method because I want the inner while loop to run again after it turns out true and repeats the outer loop and inner loop again. Please help.

here is the code:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Main {

	public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
		Scanner console = new Scanner(System.in);
		
		 File file1 = new File("Cumbia City - An Jone.wav");
		 File file2 = new File("This City.wav");
		 File file3 = new File("Binibini.wav");
		 
		 AudioInputStream audioStream1 = AudioSystem.getAudioInputStream(file1);
		 Clip clip1 = AudioSystem.getClip();
		 clip1.open(audioStream1);
		 
		 AudioInputStream audioStream2 = AudioSystem.getAudioInputStream(file2);
		 Clip clip2 = AudioSystem.getClip();
		 clip2.open(audioStream2);
		 
		 AudioInputStream audioStream3 = AudioSystem.getAudioInputStream(file3);
		 Clip clip3 = AudioSystem.getClip();
		 clip3.open(audioStream3);
		 
		 char response_char = 'l';
		 String output = "";
		 
		 while(response_char != 'o') {
			 System.out.println("Select a Music: A- Cumbia City, B- This City, C- Binibini");
			 System.out.print("Choice: ");
			 String response = console.next();
			 response_char = response.charAt(0);
		while(!output.equals("Q")) {
		 if(response_char == 'A' || response_char == 'a') {
			 System.out.println("P- Play, S- Stop, R- Reset, Q- Quit");
			 output = console.next();
			 output = output.toUpperCase();
			 switch(output) {
			 case ("P"): clip1.start();
			 break;
			 case ("S"): clip1.stop();
			 break;
			 case ("R"): clip1.setMicrosecondPosition(0);
			 break;
			 case ("Q"): clip1.close();
			 break;
			 default: System.out.println("Invalid output");
			 
			 }
		 } else if(response_char == 'B'|| response_char == 'b') {
			 System.out.println("P- Play, S- Stop, R- Reset, Q- Quit");
			 output = console.next();
			 output = output.toUpperCase();
			 switch(output) {
			 case ("P"): clip2.start();
			 break;
			 case ("S"): clip2.stop();
			 break;
			 case ("R"): clip2.setMicrosecondPosition(0);
			 break;
			 case ("Q"): clip2.close();
			 break;
			 default: System.out.println("Invalid output");
			 
		  }  
		 } else if(response_char == 'C' || response_char == 'c') {
			 System.out.println("P- Play, S- Stop, R- Reset, Q- Quit");
			 output = console.next();
			 output = output.toUpperCase();
			 switch(output) {
			 case ("P"): clip3.start();
			 break;
			 case ("S"): clip3.stop();
			 break;
			 case ("R"): clip3.setMicrosecondPosition(0);
			 break;
			 case ("Q"): clip3.close();
			 break;
			 default: System.out.println("Invalid output");
			 
		 }
		 
		 } else {
			 System.out.println("Invalid song");
		 
		 }
		} //Inner while loop
		 
	
	} //Outer while loop
	}
}

Hello @DavidLacsao,

It looks like you need to assign the output after the inner while.

output = "";

I’m assuming when the inner while loop is true then output = “Q”

so next time it goes to the inner while loop

while(!output.equals("Q"))

it is Q so will not run it

Hope that helps out

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

backticks are also evil when it comes to internet explorer - sorry had to put that in :slight_smile:
but only for javascript

Hi @jaket, this actually worked for me. I really appreciate the help.

@JeremyLT thanks for the advice, ill keep that in mind next time.

That is good - glad I could help.
Just get in the habit of re-assigning the variable/object being tested on at the end of the loop before closing.
Oh but in your example just after it closes.
Happy coding.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.