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;

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

        System.out.println("");
        System.out.format("%1s%25s","Operation","Result\n");
        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.

Comments
programing of java

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 ");
         num1=input.nextInt(); 
         System.out.printf("Enter Second Number:\t ");
         num2=input.nextInt();

    System.out.format("%1s%25s","Operation","Result\n");
         int additionResult = num1 + num2;
    System.out.format("%1s%26s",num1+"+"+num2,additionResult+"\n");

         int multiplicationResult = num1 * num2;
    System.out.format("%1s%26s",num1+"*"+num2,multiplicationResult+"\n");

         int subtractionResult = num1 - num2;
    System.out.format("%1s%26s",num1+"-"+num2,subtractionResult+"\n");

         int divisionResult = num1 / num2;
    System.out.format("%1s%26s",num1+"/"+num2,divisionResult+"\n");


         }


     }

Again I appreciate your input a lot. Thank you.

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

This question has already been answered. Start a new discussion instead.