That's when the fun started, of course. I missed the instructions for sending Java code, ans, as such, got a few CEs. I went there, read the docs, and tried again, only to get a WA. I have no idea what's wrong in this code... Several test cases obtained here in the forum work exactly the same. I even got some ISBNs from bookstores to try on my program, and it was fine.
Without further ado, here is the code. Can someone please give me a small hint?
Code: Select all
/* @JUDGE_ID: XXXXXXX 333 Java "Recognizing Good ISBNs" */
/*package e333;*/
import java.io.IOException;
class Main {
public static void main(String[] args)
{
String data;
while ((data = ReadLn(80)) != null)
{
data = data.trim();
if (calcularSoma(data) % 11 != 0)
{
System.out.println(data + " is incorrect. ");
}
else
{
System.out.println(data + " is correct. ");
}
}
}
static int calcularSoma(String data)
{
int resultado = 0;
int resultado2 = 0;
int counter = 0;
for (int i = 0; i < data.length(); i++)
{
String caractere = String.valueOf(data.charAt(i));
if (data.charAt(i) >= '0' && data.charAt(i) <= '9')
{
resultado += Integer.parseInt(caractere);
resultado2 += resultado;
counter++;
}
else if (data.charAt(i) == 'X' && counter == 9)
{
resultado += 10;
resultado2 += resultado;
counter++;
}
else if (data.charAt(i) == '-')
{
}
else
{
return -1;
}
}
if (counter == 10)
return resultado2;
else
return -1;
}
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) || (car == '\n') && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
}