-1

I'm using multiple + in the code. I want to know if there is any other way to do so without using + many times.

package java;
import java.util.Scanner;
public class Calculator
{
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in); 
        System.out.println("\n                  ------CALCULATOR------");
        System.out.println
        ("------------------------------------------------------------\n"
        +"                       1 - ADDITION\n"
        +"                       2 - SUBTRACTION\n"
        +"                       3 - MULTIPLICATON\n"
        +"                       4 - DIVISION\n"
        +"                       5 - MODULUS\n"
        +"                       6 - RATING\n"
        +"                       7 - EXIT\n"
        +"-------------------------------------------------------------\n");
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Piyush Pandita
  • 181
  • 1
  • 1
  • 12

1 Answers1

0

You could make a method that you provide a list of what you want to print and it will format it that way.

It could look something like this:

ArrayList<String> data = new ArrayList<String>();
data.add("1 - ADDITION");
data.add("2 - SUBTRACTION");

public void printMenu(List<String> data){
    System.out.println("-----------------------------------------------------");
    data.stream.forEach(d->{
        System.out.println(d);
    });
    System.out.println("-----------------------------------------------------");
}

This would allow you to have more flexability and control, and you can call this using printMenu(data);

Depending on how much repetition you want to eliminate, you can do something like

ArrayList<String> data = Arrays.asList(new String[]{"1 - ADDITION", "2 - SUBTRACTION"});

and create a print method

public void print(Object o){
    System.out.println(o.toString());
}

Which instead of using System.out.println you can then just call print. Which means you can do-

public void printMenu(List<String> data){
    print("-----------------------------------------------------");
    data.stream.forEach(d->{
        print(d);
    });
    print("-----------------------------------------------------");
}

TL:DR- depending on what you want and your requirements, you have tons of flexibility.

Cameron Grande
  • 163
  • 1
  • 10