Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

anyone know what i did wrong?

–2 votes
asked 3 days ago by 3 molina william (180 points)
import java.util.Scanner;

public class Main

{

private String firstNum;

private String secondNum;

private String sum;

  public static void main(String[ ] args) {

Main lab = new Main( );

lab.input(); // Read two binary numbers

lab.output( ); // Display output

}

public void input() {

Scanner reader = new Scanner(System.in);

System.out.print("Enter the first binary number: ");

firstNum = reader.next();

System.out.print("Enter the second binary number: ");

secondNum = reader.next();

}

public String sum(String first, String second) {

  sum(firstNum, secondNum));

  public static String sum(String n1, String n2) {

int a = Integer.parseInt(n1, 2);

int b = Integer.parseInt(n2, 2);

return Integer.toBinaryString(a + b);

    

  }

}

public void output() {

System.out.println(firstNum + " + " + secondNum + " = " + sum(firstNum, secondNum));

}

}

1 Answer

0 votes
answered 3 days ago by Peter Minarik (22,320 points)

Your code seems to be copied (incorrectly) from somewhere as basic concepts, such as

  • only one function can exists with the same signature (the function sum was duplicated)
  • opening braces/parenthesis must be closes

are violated.

If you're new to programming, please, review some tutorials to familiarize yourself with the language basics.

I fixed these issues, tested the program: it runs, it worked for a random input (100 + 10 = 110), but I didn't go into details.

Here's the fixed code:

import java.util.Scanner;

public class Main
{
    private String firstNum;
    private String secondNum;
    private String sum;
    
    public static void main(String[ ] args)
    {
        Main lab = new Main();
        lab.input(); // Read two binary numbers
        lab.output( ); // Display output
    }

    public void input()
    {
        Scanner reader = new Scanner(System.in);
        System.out.print("Enter the first binary number: ");
        firstNum = reader.next();
        System.out.print("Enter the second binary number: ");
        secondNum = reader.next();
    }

    public static String sum(String n1, String n2)
    {
        int a = Integer.parseInt(n1, 2);
        int b = Integer.parseInt(n2, 2);
        return Integer.toBinaryString(a + b);
    }

    public void output()
    {
        System.out.println(firstNum + " + " + secondNum + " = " + sum(firstNum, secondNum));
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...