Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is any one like to fix my while loop. Its not working right. when I called my me

ID: 3801062 • Letter: I

Question

Is any one like to fix my while loop. Its not working right. when I called my method name decimalToBinary(input)) in the while loop its giving me error like I need to change the int type to String. but I want the user to put an integer and get converted to binary. And the loop is not working properly or what I intended to do so please help!

import java.util.Scanner;

import structures.ArrayStack;

/**

* an interactive program which accepts a positive integer value

* from a user and then displays the binary equivalent of that value. The program should allow the user to

* repeat this process until the user chooses to quit.

* @author

*

*/

public class StackApps {

  

   private static Scanner userInput;

public static void main(String[] args) {

   boolean flag = false;

   while (!flag) {

   System.out.print("Please enter the Integer number to convert:(Enter x to exit): ");

   Scanner userInput = new Scanner(System.in);

   final String input = userInput.next();

while(!(input.equals("x"))){

int num2 = 0;

userInput = new Scanner(System.in);

try {

input = userInput.next();

  

num2 = Integer.parseInt(input);

} catch (NumberFormatException ex) {

System.out.println("Please enter only an integer number.");

}

  

if(num2 < 0) {

System.out.println("Please enter a postive number > 0." );

} else {

System.out.println(decimalToBinary(input));

}

  

System.out.println("Whould you like to continue? 'Y' or 'N'");

   Scanner yesOrNo = new Scanner(System.in);

   final String prompt = yesOrNo.next();

   if ("Y".equals(prompt) || "y".equals(prompt)) {

flag = false;

} else if ("N".equals(prompt) || "n".equals(prompt)) {

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

flag = true;

} else {

System.out.println("Looks like you done "

+ " close the program.");

}

}

   }

  

   }

  

   /**

*

* @param BinaryRep.

* @return string Binary Representation.

*/

public static String decimalToBinary(int num) {

String resultString = "";

  

final ArrayStack stack = new ArrayStack<>();

//int n = num;

int i = 0;

while (num > 0) {

stack.push(num % 2);

num = num / 2;

i++;

}

System.out.print(num + " in binary = ");

while (i > 0) {

//System.out.print(stack.pop());

resultString +=stack.pop();

i--;

}

return resultString;

}

  

  

}

Explanation / Answer

public class StackApps {

  

   private static Scanner userInput;

public static void main(String[] args) {

   boolean flag = false;

   while (!flag) {

   System.out.print("Please enter the Integer number to convert:(Enter x to exit): ");

   Scanner userInput = new Scanner(System.in);

   final String input = userInput.next();

while(!(input.equals("x"))){

int num2 = 0;

userInput = new Scanner(System.in);

try {

input = userInput.next();

  

num2 = Integer.parseInt(input);

} catch (NumberFormatException ex) {

System.out.println("Please enter only an integer number.");

}

  

if(num2 < 0) {

System.out.println("Please enter a postive number > 0." );

} else {

System.out.println(decimalToBinary(input));

}

  

System.out.println("Whould you like to continue? 'Y' or 'N'");

   Scanner yesOrNo = new Scanner(System.in);

   final String prompt = yesOrNo.next();

   if ("Y".equals(prompt) || "y".equals(prompt)) {

flag = false;

} else if ("N".equals(prompt) || "n".equals(prompt)) {

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

flag = true;

} else {

System.out.println("Looks like you done "

+ " close the program.");

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote