0

import java.util.Scanner;
 class Quiz4 {
    public static void main(String args[]){
  char repeat = userInput.charAt(0);
  do{

       Scanner input = new Scanner( System.in );
       System.out.println("Enter a binary number ");
       String binaryString =input.nextLine();

       if (binaryString.matches("[10]+")) {
     System.out.println ("You entered " +binaryString );
          System.out.println("Its base of 10 equivalent is "+Integer.parseInt(binaryString,2));
       } else {
          System.out.println ("Try again ");
          }while (repeat == 'Y' || repeat == 'y');
       }
    }
}

Im writing a code that converts binary to decimal, I've gotten pretty much everything done except I have to make sure the program continues working until user prompts it to stop. Im not sure how to apply it to my code. I know I have use a do/while loop but im not sure how to apply it.

 import java.util.Scanner;
 class Quiz4 {
    public static void main(String args[]){
       Scanner input = new Scanner( System.in );
       System.out.println("Enter a binary number ");
       String binaryString =input.nextLine();
       if (binaryString.matches("[10]+")) {
           System.out.println ("You entered " +binaryString );
          System.out.println("Its base of 10 equivalent is "+Integer.parseInt(binaryString,2));
       } else {
          System.out.println ("Try again ");
       }
    }
}
Brandon.O
  • 27
  • 5
  • 2
    I'll give you a hint: use a while(true) and an if statement that only contains a break in it – Natecat Apr 02 '16 at 05:28
  • im not allowed to use them in my assignments or infinite loops – Brandon.O Apr 02 '16 at 05:40
  • The loop isn't truely infinite because you are breaking out of the loop with a break statement. Loops such as these are used fairly often, and it is silly to simply not use them. – Natecat Apr 02 '16 at 05:41
  • Yeah, I understand but I was told to use a do/while loop but I cant figure it out. – Brandon.O Apr 02 '16 at 05:42
  • You can put the condition you would put in the if statement in the while condition if it is a do/while loop – Natecat Apr 02 '16 at 05:47
  • im not sure how to apply it without getting massive errors – Brandon.O Apr 02 '16 at 05:48
  • Show us your attempt and we can help you resolve the errors – Natecat Apr 02 '16 at 05:48
  • @Natecat Your code is wrong. It will take the user input only once before the loop. – FallAndLearn Apr 02 '16 at 06:00
  • @FallAndLearn I never gave any code??? – Natecat Apr 02 '16 at 06:34
  • 1
    @Brandon.O, If you solve your problem, please select an answer or add your own so other users can see what solution worked for you! – Matt C Apr 21 '16 at 02:23
  • @Brandon.O Please remove your answer from the question, post it as a separate answer below, and remove the "[Solved]" addition to your question. I understand that what you've done is the convention of some less formal forums, but SO has an entire system built up around having them separate. In a couple days, you'll be able to accept your answer, and this will show that it is resolved. ...Also, the solution you added has no loop; quite confusing. – jpmc26 Apr 21 '16 at 04:24

2 Answers2

0

There are 2 problems in your code. 1) repeat doesn't get changed in the loop, so the loop will either loop forever or only run once. To fix this, at some point in your loop you will need to change repeat to be the last response the user gave when asked if they wanted to keep going. This brings me to 2) You never ask the user if they want to convert again. Ask the user if they want to keep going, specify they must respond with a Y or y, and then store their response in repeat

Natecat
  • 2,175
  • 1
  • 17
  • 20
  • This may sound stupid but how do I do this, im not very experienced so everything is new to me. – Brandon.O Apr 02 '16 at 05:58
  • You have already demonstrated you can print out things to output, and get input, so what is stopping you from applying this knowledge to what I'm asking you to do? – Natecat Apr 02 '16 at 05:59
  • Im not good with the vocabulary so alot of stuff your saying doesnt make sense to me – Brandon.O Apr 02 '16 at 06:00
-1

Using do while loop

import java.util.Scanner;
 class Main {
    public static void main(String args[]){
       Scanner input = new Scanner( System.in );
       String str;
       do{
           System.out.println("Enter a binary number ");
           String binaryString =input.nextLine();
           if (binaryString.matches("[10]+")) {
               System.out.println ("You entered " +binaryString );
              System.out.println("Its base of 10 equivalent is "+Integer.parseInt(binaryString,2));
           } else {
              System.out.println ("Try again ");
           }
           System.out.println("Do you want to continue ? Press Y or y");
           str = input.nextLine();
       }while(str.charAt(0) == 'Y'||str.charAt(0) == 'y');

       System.out.println("Exiting");
    }
}

Output on console.

Enter a binary number 
100
You entered 100
Its base of 10 equivalent is 4
Do you want to continue ? Press Y or y
y
Enter a binary number 
102
Try again 
Do you want to continue ? Press Y or y
y
Enter a binary number 
111
You entered 111
Its base of 10 equivalent is 7
Do you want to continue ? Press Y or y
Y
Enter a binary number 
110
You entered 110
Its base of 10 equivalent is 6
Do you want to continue ? Press Y or y
n
Exiting
FallAndLearn
  • 4,035
  • 1
  • 18
  • 24