1 / 4
Nov 2019

This code works fine and i tested using many other test cases as well and it worked fine for all of them.
Yet still i seem to be getting NZEC error when i submit it.
Any help would be appreciated.Thank you! :slight_smile:

import java.util.*;
import java.math.BigInteger;
public class Main
{ public static HashMap<Integer,Long> map= new HashMap<Integer,Long>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in4);
String s;
s=sc.nextLine();
while(!s.equals(""))
{
int n =Integer.parseInt(s);

 System.out.println(maxDollars(n));
 
 s=sc.nextLine();

}
}

public static long maxDollars(int n)
{
if(n<12) return n;

if(map.containsKey(n)) return map.get(n);

long max=maxDollars(n/2)+maxDollars(n/3)+maxDollars(n/4);
map.put(n,max);

return max;

}
}

  • created

    Nov '19
  • last reply

    Nov '19
  • 3

    replies

  • 808

    views

  • 2

    users

  • 1

    link

You’re not detecting EOF properly. Use _sc.hasNextLine() instead of testing for an empty string.

I tried using sc.hasNextLine();
Got the same following error

"
Exception in thread “main” java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at Main.main(Main.java:14) "

import java.util.*;

public class Main
{
public static HashMap<Integer,Long> map= new HashMap<Integer,Long>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

while(sc.hasNextLine())
{

String s =sc.nextLine();
int n =Integer.parseInt(s);

System.out.println(maxDollars(n));

}
}

public static long maxDollars(int n)
{
if(n<12) return n;

if(map.containsKey(n)) return map.get(n);

long max=maxDollars(n/2)+maxDollars(n/3)+maxDollars(n/4);
map.put(n,max);

return max;

}
}

This must be with your own test data, since SPOJ would only say NZEC?

So did you check whether your data has an empty line at the end?

Edit: you could always add a check for an empty string, and then not try to convert it to an integer.