superior
superior

Reputation: 39

Calculator, can't get it to print out as I want it

This is a homework of mine. However, I can't get the result to work. I want it to print out as:

> 2*7*6
   2
*  7
----
  14 
*  6
----
  84

and so on. I want the code to work regardless of how many numbers I type in. This is my code so far;

public static int add(int a, int b) {
    return a + b;
}

public static int sub(int a, int b) {
    return a - b;
}

public static int multiply(int a, int b) {
    return a * b;
}

public static void main(String[] args) 
{

    Scanner in = new Scanner(System.in);

    System.out.print("(ex. 8*2*6): ");
    String amount = in.nextLine(); 

    if ( amount.contains("+") ) {
        String[] parts = amount.split("\\+");
    } else if ( amount.contains("-") ) {
        String[] parts = amount.split("\\-");
    } else if ( amount.contains("*") ) {
        String[] parts = amount.split("\\*");
        int[] results = new int[parts.length];

        // Convert from string to integer
        for (int i = 0; i < parts.length; i++) {
            try {
                results[i] = Integer.parseInt(parts[i]);
            } catch (NumberFormatException nfe) {};
        }

        // Print result
        int counter = 1;
        for (int i = 0; i <= results.length; i++) {
            if ( i == 0) {
            System.out.println("   " + results[i]);
            System.out.println("*  " + results[counter]);
            System.out.println("----");
            int total = multiply(results[i], results[counter]);
            System.out.println("  " +  total);
            } else if ( i > 1 ) {
                System.out.println("*  " + results[i]);
                System.out.println("----");
                System.out.println("  " + multiply(results[i], results[counter]) );
            }
            }

    } else {
        System.out.println("Error");
    }

What am I doing wrong?

Upvotes: 0

Views: 59

Answers (1)

MirlvsMaximvs
MirlvsMaximvs

Reputation: 1483

Is not c# isn it? I'm not sure if i understand you.

In c#, have you tried something like that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args) { 

            //   Scanner in = new Scanner(System.in);
          //  String amount = Console.ReadLine();
            String amount = "2*7*6*5*3*2";

            if (amount.Contains('+')) {
                String[] parts = amount.Split('+');

            }
            else
                if (amount.Contains('-')) {
                    String[] parts = amount.Split('-');
                }
                else if (amount.Contains("*")) {
                    String[] parts = amount.Split('*');
                    int[] results = new int[parts.Length];

                    // Convert from string to integer
                    for (int i = 0; i < parts.Length; i++) {
                        try {
                            results[i] = int.Parse(parts[i]);
                        }
                        catch (FormatException nfe) { };
                    }

                    // Print result      
                    int total = results[0];

                    for (int i = 1; i < results.Length; i++) {

                        if (i == 1)
                            Console.WriteLine("   " + results[i - 1]);

                        Console.WriteLine("*  " + results[i]);
                        Console.WriteLine("----");

                        total = multiply(results[i], total);
                        Console.WriteLine("  " + total);
                    }

                }
                else {
                    Console.WriteLine("Error");
                }

            Console.ReadKey();
        }

        public static int add(int a, int b) {
            return a + b;
        }

        public static int sub(int a, int b) {
            return a - b;
        }

        public static int multiply(int a, int b) {
            return a * b;
        }

        public static void main(String[] args) {


        }
    }

}

Upvotes: 1

Related Questions