# Need help with Java while loops

**URL:** <https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446>\
**Category:** General\
**Created:** [June 11, 2021, 10:52am UTC](https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446 "2021-06-11T10:52:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidLacsao](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@DavidLacsao](https://forum.freecodecamp.org/u/DavidLacsao)\
**Post date:** [June 11, 2021, 10:52am UTC](https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446/1 "2021-06-11T10:52:35Z")

</div>

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:

```auto
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
	}
}

```

---

<div class="post-metadata">

**Author:** ![jaket](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jaket/32/134089_2.png) [@jaket](https://forum.freecodecamp.org/u/jaket)\
**Post date:** [June 11, 2021, 11:04am UTC](https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446/2 "2021-06-11T11:04:40Z")

</div>

Hello @DavidLacsao,

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

```auto
output = "";

```

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

so next time it goes to the inner while loop

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

```

it is Q so will not run it

Hope that helps out

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 11, 2021, 11:31am UTC](https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446/3 "2021-06-11T11:31:42Z")

</div>

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.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![jaket](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jaket/32/134089_2.png) [@jaket](https://forum.freecodecamp.org/u/jaket)\
**Post date:** [June 11, 2021, 11:51am UTC](https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446/4 "2021-06-11T11:51:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DavidLacsao](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@DavidLacsao](https://forum.freecodecamp.org/u/DavidLacsao)\
**Post date:** [June 11, 2021, 12:21pm UTC](https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446/5 "2021-06-11T12:21:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DavidLacsao](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@DavidLacsao](https://forum.freecodecamp.org/u/DavidLacsao)\
**Post date:** [June 11, 2021, 12:23pm UTC](https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446/6 "2021-06-11T12:23:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jaket](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jaket/32/134089_2.png) [@jaket](https://forum.freecodecamp.org/u/jaket)\
**Post date:** [June 11, 2021, 12:25pm UTC](https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446/7 "2021-06-11T12:25:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [December 11, 2021, 12:26am UTC](https://forum.freecodecamp.org/t/need-help-with-java-while-loops/464446/8 "2021-12-11T00:26:20Z")

</div>

Closed on December 11, 2021, 12:26am UTC

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