Hi guys. I am having issues with the output of this program. Can somebody tell me what I have to do to make this work correctly? Here is the code I have so far:

import java.util.Scanner; //text scanner to parse primitive types and strings using regular expressions.

public class Operations {

    private static Object total;

    public static void main(String[] args) {
        Scanner input=new Scanner (System.in); 
        int num1=0, num2=0;
        System.out.printf("Enter First Number:\t ");
        System.out.printf("Enter Second Number:\t ");

        String fmt = "%-28s %-7d \n";
        String solution1= Integer.toString(num1) + "+" + Integer.toString(num2);
        System.out.printf(fmt, solution1, total);  

        String format1 = "%-28s %-7d \n";
        String solution2= Integer.toString(num1) + "-" + Integer.toString(num2);
        System.out.printf(format1, solution2, total);

        String format2 = "%-28s %-7d \n";
        String solution3= Integer.toString(num1) + "*" + Integer.toString(num2);
        System.out.printf(fmt, solution3, total);

        String format3 = "%-28s %-7d \n";
        String solution4= Integer.toString(num1) + "/" + Integer.toString(num2);
        System.out.printf(fmt, solution4, total);



it gives off a couple of warnings but no errors and when I run the programI get this output:

Operation Result
5+6 null
5-6 null
5*6 null
5/6 null

which is exactly how I want it but it needs to give the answers not a null value.

I will be so grateful for your help. Thank you in advance.

beckhazkayz commented: programing of java +0

Recommended Answers

All 6 Replies

Where does the code assign any value to the variable: total? Its default value is null.
Why is total defined as an Object? Shouldn't it be a numeric type?

I really don't know. I am extremely new to java programming but that makes sense. Any thoughts on how to fix what I have messed up?

Just a few things:

  • You're trying to make it look neater by making the formatting a string variable. I would say this is not going to help. Unless this is part of some other plan, maybe just leave the formatting alone e.g. printf("%d",x);

  • Some of the variables are not actually used in your program. format1, format2 & format3.

  • I would guess that you are using an IDE of some description (like Eclipse) and it is giving you some advice through red or yellow flags. Be cautious about just picking the first default quick fix the IDE offers you. I think this is where you've used the Object declaration for total.

  • Finally, your maths operators (like plus signs) are strings, In this form, they won't just 'work' like they do for normal formulas.

Thanks a lot for your thoughts. Being new to programming and all, it really is a major learning tool to be enlightened by more experienced coders. I spent a few more hours on this code and was able to put a little somethin' somethin' together. Here look at this and if you or anybody else has more insight they can offer I'll be more than glad to hear it. Inline Code Example Here

 import java.util.Scanner;

 public class ArithmeticOperations {

     public static void main(String[] args) {
         Scanner input=new Scanner (System.in); 
         int num1=0, num2=0;
         System.out.printf("NUMBER CALCULATIONS\n\n");
         System.out.printf("Enter First Number:\t ");
         System.out.printf("Enter Second Number:\t ");

         int additionResult = num1 + num2;

         int multiplicationResult = num1 * num2;

         int subtractionResult = num1 - num2;

         int divisionResult = num1 / num2;



Again I appreciate your input a lot. Thank you.

Seems to work now :)

I would have used System.out.printf, instead of format since it does the same thing in this case.

In line 9, there are two variables intialized on the same line. It will work like that, but most people will tell you that it's clearer to use separate lines. It also allows a comment to be added next to it. (http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html#18761)

Cool, alrighty then Thank you much and I am sure I will be back very shortly if not later today sometime.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.