anyone know how to get input and output on the same line while using the Scanner? if not, is there an alternative where i can use the same input way, #/# , and get them into longs.
mine looks like
PI: 3.141592653589793
Current: 22/7
~ 3.142857142857143
Next: 179/57 ~ 3.1403508771929824
while on the assignment it says it's supposed to be
http://img2.freeimagehosting.net/uploads/c57d1b0dd1.jpg
aka
PI: 3.141592653589793
Current: 22/7 ~ 3.142857142857143
Next: 179/57 ~ 3.1403508771929824
(it has to look EXACTLY like that or i will get points taken away)
I am not using println, just print. we have to input like "355/34" and the only way i know is too use a scanner then use for loops to put like 355 and 34 into StringBuffers and then longs to divide into a double. if theres another way to have the input like "322/31" and still get the output without using a scanner, please enlighten me.
Code:
import java.util.*;
public class NeedHelp_jaysee
{
public static void main(String[] args)
{
int i2 = 0;
long numerator, denominator;
double currentAnswer;
final double EPSILON = Math.abs(Math.PI - 22.0/7.0);
String current, numToString, denomToString;
StringBuffer num = new StringBuffer(), denom = new StringBuffer();
Scanner keyboard = new Scanner(System.in);
System.out.println("PI:\t " + Math.PI);
System.out.print("Current: ");
current = keyboard.next();
for(int i =0; i < current.length(); i++)
{
if(current.charAt(i) == '/')
{
i2 = i + 1;
i = current.length();
}
else
num.append(current.charAt(i));
}
for(; i2 < current.length(); i2++)
{
denom.append(current.charAt(i2));
}
numToString = num.toString();
denomToString = denom.toString();
numerator = Long.parseLong(numToString);
denominator = Long.parseLong(denomToString);
currentAnswer = (double)numerator/denominator;
System.out.print(" ~ " + currentAnswer);
while(Math.abs(Math.PI - currentAnswer) >= EPSILON)
{
if(currentAnswer < Math.PI)
{
numerator++;
}
else
denominator++;
currentAnswer = (double)numerator/denominator;
}
System.out.print("\nNext:\t " + numerator + "/" + denominator + " ~ " + currentAnswer);
}
}