0
String numOfTermsInput = JOptionPane.showInputDialog(null, "Please enter the number "
                                        + "of terms you would like to use to compute Pi: ");

Scanner numOfTermsScanner = new Scanner(numOfTermsInput);

int term = numOfTermsScanner.nextInt();
numOfTermsScanner.close();      

int num1 = term * 2;
int num2 = num1 + 1;
int num3 = num2 + 1;

int digit1 = num1 * 1000;
int digit2 = num2 * 10;
int digit3 = num3;
int sum = digit1 + digit2 + digit3;

if (digit3 > 9)
{

    sum = ((digit1 + digit2) * 10) + digit3;

}

System.out.println(digit1);
System.out.println(digit2);
System.out.println(digit3);
System.out.println(sum);

I want to try and consider the numbers as characters, so that when I get 8, 9, 10 the value will be 8910, or is there a more efficient way to do this? You can ignore the if statement, that was just an attempt and only works for 8910. Thanks.

Seamus W
  • 95
  • 1
  • 7
  • 1
    So you mean you want `String blub = "" + digit1 + digit2 + digit3`? If yes, then this question is a dupe of this one: http://stackoverflow.com/questions/2674707/how-to-concatenate-int-values-in-java – Tom Oct 25 '16 at 16:25
  • Yes this works but now I want to cast that string back into an int so I can use it for calculations. How do I do that? – Seamus W Oct 25 '16 at 16:32
  • 1
    This isn't hard to google ;). Just give it a try, you'll find it in less than a minute. – Tom Oct 25 '16 at 16:33

1 Answers1

0

Use Integer.parseInt(String) to convert string to integer:

see Integer.valueOf() vs. Integer.parseInt()

Community
  • 1
  • 1
donlys
  • 370
  • 2
  • 13