I am trying to solve this “exponentiation” problem.
Here is the code that I have written
import java.util.Scanner;
import java.util.ArrayList;
import java.math.BigInteger;
import java.io.*;
import java.math.*;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
BigDecimal R,f;// = new BigInteger();
ArrayList <String> ans = new ArrayList <String> ();
int n;
///// I dont know how to deal with it
while( R = Input.nextBigDecimal(), n = Input.nextInt() )
////
f = R.pow(n);
f = f.stripTrailingZeros();
String s = f.toPlainString();
String answer = "";
answer = s;
if(s.charAt(0) == '0')
s = s.substring(1);
ans.add(answer);
System.out.println(s);
}
}
Now The question that I want to ask is that how would I take input until the end of file?
(I have solved such problems using c++ using while(cin >> v >> s) etc ) But I don’t know how to solve this.
Scanner class has also hasNextBigDecimal() method, so you would like to do:
Scanner is slow for huge input files. I’m using BufferedReader if the line has well known format (for example two numbers separated by space):
If you want to use java, you should learn also Java naming conventions.
And even better than
splitis to useStringTokenizer, it’s quicker.