Is any one like to fix my while loop Its not working right w

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;

}

  

  

}

Solution

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.\");

}

}

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 n
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 n
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 n
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 n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site