2
import javax.swing.JOptionPane;
public class HW {
public static void main(String[] args)
{
    String x1 = JOptionPane.showInputDialog(null, "X: ");
    String y1 = JOptionPane.showInputDialog(null, "Y: ");
    double x = Double.parseDouble(x1);
    double y = Double.parseDouble(y1);
    System.out.println("Sum: " + (x+y));
    System.out.println("Difference: " + (x-y));
    System.out.println("Product: " + (x*y));
    System.out.println("Average: " + (x+y)/2);
    System.out.println("Distance: " + Math.abs(x-y));
    System.out.println("Maximum Value: " + Math.max(x,y));
    System.out.println("Minimum Value: " + Math.min(x,y));
    }
}

Basically, I'm trying to output the values to be like:

Sum:        5
Difference: 10
Product:    7
etc.

I've found how to do this with strings, but I'm unsure how to accomplish this with variables.

Rohit Jain
  • 195,192
  • 43
  • 369
  • 489
Modify You
  • 143
  • 3
  • 14
  • Calculate the maximum length of all strings. It will be easier if they are in array. And then PAD when printing. Look at [this SO post](http://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java) for ideas how to PAD in Java. – PM 77-1 Sep 30 '13 at 20:55

2 Answers2

1

Simplest solution, hardcode the number of tabs necessary to line up the text. Fine for small programs like this.

public static void main(String[] args)
{
    String x1 = JOptionPane.showInputDialog(null, "X: ");
    String y1 = JOptionPane.showInputDialog(null, "Y: ");
    double x = Double.parseDouble(x1);
    double y = Double.parseDouble(y1);
    System.out.println("Sum: \t\t" + (x+y));
    System.out.println("Difference: \t" + (x-y));
    System.out.println("Product: \t" + (x*y));
    System.out.println("Average: \t" + (x+y)/2);
    System.out.println("Distance: \t" + Math.abs(x-y));
    System.out.println("Maximum Value: \t" + Math.max(x,y));
    System.out.println("Minimum Value: \t" + Math.min(x,y));
}

You may have to add additional '\t' (tab) characters to make it line up. The reason I left a space before the '\t' is so that you guarantee there will be at least 1 whitespace between label and value. (Tab can have no effect if cursor is at a certain position already)

Ron
  • 1,338
  • 11
  • 24
0

I would use StringBuffer instead to calculate prefix length:

public  class HelloWorld  {

 public static void main(String args[]){

 String x = JOptionPane.showInputDialog(null, "X: ");
 String y = JOptionPane.showInputDialog(null, "Y: ");       


System.out.println(print("Sum:", (x+y)+""));
System.out.println(print("Difference:", (x-y)+""));
System.out.println(print("Product:", (x*y)+""));
System.out.println(print("Average:", (x+y)/2+""));
System.out.println(print("Distance:", Math.abs(x-y)+""));
System.out.println(print("Maximum Value:", Math.max(x,y)+""));
System.out.println(print("Minimum Value:", Math.min(x,y)+""));
}   

private static String print(String prefix, String value){
    StringBuilder buff = new StringBuilder();

    int length = prefix.length();

    int mLength = 15; // this is your gap between title and value

    buff.append(prefix);

    while(length <= mLength){
        buff.append(" ");
        length++;
    }

    buff.append(value);

    return buff.toString();
}

 }

Output:

Sum:            7.0
Difference:     -3.0
Product:        10.0
Average:        3.5
Distance:       3.0
Maximum Value:  5.0
Minimum Value:  2.0
Maxim Shoustin
  • 76,444
  • 28
  • 192
  • 219