I was assigned to make a program that converts Binary to decimal. I got that part done with no problems but I have to validate what the user inputs to make sure they put in binary or else it should say "try again" I also have to keep the program repeating using letter "y/Y" to continue or else it exists without using an infinite loop. I have no idea how to do it without an infinite loop. I've tried many different ways but i cant seem to properly input them into my code.
import java.util.Scanner;
class Quiz4 {
public static void main(String args[]){
Scanner input = new Scanner( System.in );
System.out.print("Enter a binary number: ");
String binaryString =input.nextLine();
System.out.println("Output: "+Integer.parseInt(binaryString,2));
}
}
► Run code snippet