0

It is attempt to solve second task from Code Jam 2014. Unfortunately, I have an InputMisMatchException? I think problem is that first number is not double. How I should to read such information? Link to task.

Input:    
    4
    30.0 1.0 2.0
    30.0 2.0 100.0
    30.50000 3.14159 1999.19990
    500.0 4.0 2000.0

My Java code:

public class CookieClickerAlpha {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);

        int n = (int) sc.nextDouble();

        double priceForFarm, plusCookies, goal;
        double currentTime = 0.0, timeWithFarm, 
                timeWithoutFarm, timeForFarm, resultTime;
        double currCookies = 0.0, cookiesPerSec = 2.0;

        for (int i = 0; i < n; i++) {

            priceForFarm = sc.nextDouble();
            plusCookies = sc.nextDouble();
            goal = sc.nextDouble();

            while (currCookies < goal) {

                timeForFarm = (priceForFarm - currCookies) / cookiesPerSec;

                timeWithFarm = timeForFarm + 
                        goal / (cookiesPerSec + plusCookies);
                timeWithoutFarm = (goal - currCookies) / cookiesPerSec;

                if (timeWithFarm < timeWithoutFarm) {
                    currCookies = 0.0;
                    cookiesPerSec += plusCookies;
                    currentTime += timeForFarm;
                }
                else {
                    currentTime += goal / cookiesPerSec;
                    currCookies = goal;
                }

            }

            pw.print("Case #" + (i + 1) + ": ");
            pw.println(currentTime);

        }

        sc.close();
        pw.close();
    }
}

1 Answers1

0

use sc.hasNextDouble() / hasNextInt() to check whether the nxt number is a double / int and then use nextInt(), nextDouble() based on what hasNextXXX returns.

EDIT :

To read integer first , do :

if(sc.hasNextInt()){
n=sc.nextInt();
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • I know this method but how it can helps me? I know that first number always is integer. I would like read integer first and than only doubles. –  Jun 12 '14 at 11:15