Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Can Anyone Help With Repairing The Bug In This Code?

Quasatron

New Coder
I have set myself the task of trying to effectively diagnose a Bug in this piece of code, which has been written in JavaScript. the problem. How does a coder who has just started to learn coding, use 'variable watching', 'compiler warnings' and a 'unit test' for this objective? Here is the piece of code I am referring to:

JavaScript:
import java.util.Scanner;

public class StudentVersion {

 public static void main(String[] args) {

 boolean run = true;

 String choice;

 

 do {

 choice = displayMenu();

 if (choice.equals("x")){

 run = false;

 }

 else if (choice.equals("1")){

 double x = getNumber(1);

 double y = getNumber(2);

 int total = sumMethod(x,y);

 display(totl);

 run = checkFinish();

 }

 else if (choice.equals("2")){

 double x = getNumber(1);

 double y = getNumber(2);

 double total = subtractMethod(x,y);

 display(total);

 run = checkFinish();

 }

 else if (choice.equals("3")){

 double x = getNumber(1);

 double y = getNumber(2);

 double total = multiplymethod(x,y);

 display(total);

 run = checkFinish();

 }

 else (choice.equals("4")){

 double x = getNumber(1);

 double y = getNumber(2);

 double total = divideMethod(x,y);

 display(total);

 run = checkFinish();

 }

 else {

 System.out.println("Entry not recognised, please try again...");

 }

 

 } while (run);

 }

 

 public static double getNumber(int count){

 boolean numberWrong = true;

 while (numberWrong) {

 if (count == 1) {

 System.out.print("Enter 1st number: ");

 } else {

 System.out.println("Enter 2nd number: ");

 }

 Scanner reader = new Scanner(System.in);

 try {

 double x = reader.nextLine();

 return x;

 } catch (Exception e){

 System.out.println("Number not recognised, please try again.");

 }

 }

 return 0;

 }

 

 public static String displayMenu(){

 System.out.println(" Calculator Menu");

 System.out.println("1. Add numbers");

 System.out.println("2. Subtract numbers");

 System.out.println("3. Multiply numbers");

 System.out.println("4. Divide numbers");

 System.out.println("x. Exit program");

 System.out.println();

 Scanner reader = new Scanner(System.in);

 System.out.print("Enter option (1,2,3,4,x): ");

 return reader.nextLine();

 }

 

 public static boolean checkFinish(){

 Scanner reader = new Scanner(System.in);

 boolean check = true;

 System.out.print("Have you finished (y/n): ");

 String ans = reader.nextLine().trim().toLowerCase();

 if (ans.equals("y")){

 return false;

 }

 else{

 return true;

 }

 }

 

 public static double sumMethod(double n, double m){

 System.out.println("When adding the numbers");

 return (n * m);

 }

 

 public static double subtractMethod(double n, double m){

 System.out.println("When subtracting the numbers");

 return (n - m);

 }

 

 public static double multiplyMethod(double n, double m){

 System.out.println("When multiplying the numbers");

 return (n * m);

 }

 

 public static double divideMethod(double n, double m){

 System.out.println("When dividing the numbers");

 return ((double)n / m);

 }

 

 public static void display(double sum){

 System.out.println("The answer is: " + sum)

 }
 
Last edited by a moderator:
Well, here is the correct code reference try it once:

Code:
import java.util.Scanner;


public class Calculator {


    public static void main(String[] args) {


        boolean run = true;
        String choice;


        do {
            choice = displayMenu();


            if (choice.equals("x")) {
                run = false;
            } else if (choice.equals("1")) {
                double x = getNumber(1);
                double y = getNumber(2);
                double total = sumMethod(x, y);
                display(total);
                run = checkFinish();
            } else if (choice.equals("2")) {
                double x = getNumber(1);
                double y = getNumber(2);
                double total = subtractMethod(x, y);
                display(total);
                run = checkFinish();
            } else if (choice.equals("3")) {
                double x = getNumber(1);
                double y = getNumber(2);
                double total = multiplyMethod(x, y);
                display(total);
                run = checkFinish();
            } else if (choice.equals("4")) {
                double x = getNumber(1);
                double y = getNumber(2);
                double total = divideMethod(x, y);
                display(total);
                run = checkFinish();
            } else {
                System.out.println("Entry not recognized, please try again...");
            }
        } while (run);
    }


    public static double getNumber(int count) {
        boolean numberWrong = true;
        double x = 0.0; // Initialize to a default value
        Scanner reader = new Scanner(System.in);


        while (numberWrong) {
            if (count == 1) {
                System.out.print("Enter 1st number: ");
            } else {
                System.out.print("Enter 2nd number: ");
            }


            try {
                x = Double.parseDouble(reader.nextLine());
                numberWrong = false; // Exit loop on successful input
            } catch (NumberFormatException e) {
                System.out.println("Number not recognized, please try again.");
            }
        }


        return x;
    }


    public static String displayMenu() {
        System.out.println("Calculator Menu");
        System.out.println("1. Add numbers");
        System.out.println("2. Subtract numbers");
        System.out.println("3. Multiply numbers");
        System.out.println("4. Divide numbers");
        System.out.println("x. Exit program");
        System.out.println();


        Scanner reader = new Scanner(System.in);
        System.out.print("Enter option (1, 2, 3, 4, x): ");
        return reader.nextLine();
    }


    public static boolean checkFinish() {
        Scanner reader = new Scanner(System.in);
        System.out.print("Have you finished (y/n): ");
        String ans = reader.nextLine().trim().toLowerCase();


        return ans.equals("n");
    }


    public static double sumMethod(double n, double m) {
        System.out.println("When adding the numbers");
        return (n + m);
    }


    public static double subtractMethod(double n, double m) {
        System.out.println("When subtracting the numbers");
        return (n - m);
    }


    public static double multiplyMethod(double n, double m) {
        System.out.println("When multiplying the numbers");
        return (n * m);
    }


    public static double divideMethod(double n, double m) {
        System.out.println("When dividing the numbers");
        return ((double) n / m);
    }


    public static void display(double sum) {
        System.out.println("The answer is: " + sum);
    }
}


Thanks
 
A quick note re multiple Scanners:
In general it’s a very bad idea to create multiple scanner instances. Scanner buffers its underlying input so when you stop using one Scanner and start using another you lose whatever was left in the first Scanner’s buffer.
In this particular code you get away with it because you only read whole lines - but then why bother to use a Scanner at all?
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom