Jump to content

I'm trying to make the second loop go back to the first one. I'm not sure why my code didn't work but do you guys have any suggestions?

import java.util.Scanner;
public class HW05P05 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		
	int x = 0;
	int choice;
	
	do{
		System.out.println("Binary Number Converter");
		System.out.println("1. Convert Binary to Decimal");
		System.out.println("2. Convert Decimal to Binary");
		System.out.println("3. Exit the Program");
		System.out.print("Enter your choice (1-3): ");
		choice = input.nextInt();
		if (choice > 3 || choice < 1){
			System.out.println("");
			System.out.println("Sorry that's not an option");
			System.out.println("");
			x--;
		}
		else if (choice == 3){
			System.out.println("Bye!");
			System.exit(0);
		}
		x++;
	}while(x == 0 || choice > 3 || choice < 1);
	
	String empty = input.nextLine();
	
	double sum = 0;
	int i;
	do{
		System.out.println("Enter Binary: ");
		String binary = input.nextLine();
		int length = binary.length();
		int length2 = binary.length();
		for (i = 0; i < length; i++){
			char check = binary.charAt(i);
			if (check == '1'){
				sum = sum + Math.pow(2,length-1-i);
			}
				
}
		System.out.printf("Decimal: %.0f", sum);
		x--;
	}while(x == 1);
	
	
	

	}

}

 

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
https://linustechtips.com/topic/594610-help-with-java-loops/
Share on other sites

Link to post
Share on other sites

3 minutes ago, IvanSnipedYu said:

I'm trying to make the second loop go back to the first one. I'm not sure why my code didn't work but do you guys have any suggestions?

Spoiler


import java.util.Scanner;
public class HW05P05 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		
	int x = 0;
	int choice;
	
	do{
		System.out.println("Binary Number Converter");
		System.out.println("1. Convert Binary to Decimal");
		System.out.println("2. Convert Decimal to Binary");
		System.out.println("3. Exit the Program");
		System.out.print("Enter your choice (1-3): ");
		choice = input.nextInt();
		if (choice > 3 || choice < 1){
			System.out.println("");
			System.out.println("Sorry that's not an option");
			System.out.println("");
			x--;
		}
		else if (choice == 3){
			System.out.println("Bye!");
			System.exit(0);
		}
		x++;
	}while(x == 0 || choice > 3 || choice < 1);
	
	String empty = input.nextLine();
	
	double sum = 0;
	int i;
	do{
		System.out.println("Enter Binary: ");
		String binary = input.nextLine();
		int length = binary.length();
		int length2 = binary.length();
		for (i = 0; i < length; i++){
			char check = binary.charAt(i);
			if (check == '1'){
				sum = sum + Math.pow(2,length-1-i);
			}
				
}
		System.out.printf("Decimal: %.0f", sum);
		x--;
	}while(x == 1);
	
	
	

	}

}

 

 

Just to be clear, you want the second do-while loop to end up at the top of the first?

 

Just put them both inside a while loop. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
https://linustechtips.com/topic/594610-help-with-java-loops/#findComment-7730128
Share on other sites

Link to post
Share on other sites

5 minutes ago, Stardar1 said:

Just to be clear, you want the second do-while loop to end up at the top of the first?

 

Just put them both inside a while loop. 

yes.

I tried something like this
but it didn't work because the second time i try to access the binary -> decimal converter it gives me a 0. Instead of asking for a new binary input.

Quote

import java.util.Scanner;

public class HW05P05 {

 

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

 

 

int x = 0;

int choice;

 

do{

System.out.println("Binary Number Converter");

System.out.println("1. Convert Binary to Decimal");

System.out.println("2. Convert Decimal to Binary");

System.out.println("3. Exit the Program");

System.out.print("Enter your choice (1-3): ");

choice = input.nextInt();

if (choice > 3 || choice < 1){

System.out.println("");

System.out.println("Sorry that's not an option");

System.out.println("");

x--;

}

else if (choice == 3){

System.out.println("Bye!");

System.exit(0);

}

x++;

}while(x == 0 || choice > 3 || choice < 1);

 

String empty = input.nextLine();

 

int i;

do{

double sum = 0;

System.out.println("Enter Binary: ");

String binary = input.nextLine();

int length = binary.length();

for (i = 0; i < length; i++){

char check = binary.charAt(i);

if (check == '1'){

sum = sum + Math.pow(2,length-1-i);

}

 

}

System.out.printf("Decimal: %.0f", sum);

x--;

System.out.println("");

System.out.println("");

do{

System.out.println("Binary Number Converter");

System.out.println("1. Convert Binary to Decimal");

System.out.println("2. Convert Decimal to Binary");

System.out.println("3. Exit the Program");

System.out.print("Enter your choice (1-3): ");

choice = input.nextInt();

if (choice > 3 || choice < 1){

System.out.println("");

System.out.println("Sorry that's not an option");

System.out.println("");

x--;

}

else if (choice == 3){

System.out.println("Bye!");

System.exit(0);

}

x++;

}while(x == 0 || choice > 3 || choice < 1);

}while(choice == 1);

 

 

 

 

}

 

}

 

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
https://linustechtips.com/topic/594610-help-with-java-loops/#findComment-7730170
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×