Page 1 of 1

Compiler error!

Posted: Tue Oct 08, 2002 3:51 pm
by Preben
This code compiles fine under JDK 1.4 on Windows XP,
but give me a compiler error at ACM.
Why?? :o



[java]import java.io.*;
import java.util.*;

class Main {
static String readLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";

try{
while (lg < maxLg){
car = System.in.read();
if(car == '\r') continue; //DOS-friendly
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e){
return (null);
}

if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}

static String[]accountNames;
static int[]transactionAccount;
static int[]transactionAmount;

public static void main(String[] args) throws Exception{
System.setIn(new FileInputStream(new File("p187.in")));
accountNames=new String[1000];
transactionAccount=new int[11];
transactionAmount=new int[11];
String account;
while(!(account=readLn(255)).startsWith("000")){
int accountNumber=Integer.parseInt(account.substring(0,3));
String accountName=account.substring(3,account.length());
accountNames[accountNumber] = accountName;
}
accountNames[999]=new String("Out Of Balance");
String transaction;
int lastTransaction = -1;
int index = 0;
int balance = 0;
while((transaction=readLn(255))!=null){
int transactionNumber=Integer.parseInt(transaction.substring(0,3));
if(transactionNumber==0)break;
int accountNumber=Integer.parseInt(transaction.substring(3,6));
String amount=transaction.substring(6,transaction.length());
amount=amount.trim();
int amountValue = Integer.parseInt(amount);

if(lastTransaction != transactionNumber && lastTransaction != -1){
if(balance != 0){
transactionAccount[index] = 999;
transactionAmount[index] = balance * (-1);
printExceptionReport(lastTransaction);
}
index = 0;
balance = 0;
}

transactionAccount[index]=accountNumber;
transactionAmount[index]=amountValue;
balance+=amountValue;
index++;
lastTransaction = transactionNumber;
}
}

public static void printExceptionReport(int transaction){
System.out.println("*** Transaction " + transaction + " is out of balance ***");
for(int i = 0; i<= transactionAccount.length; i++){
String account = "" + transactionAccount;
String Ljustified = leftJustify(accountNames[transactionAccount], 30);
String Rjustified = rightJustify(transactionAmount, 9);
System.out.println(account + " " + Ljustified + " " + Rjustified);
if(transactionAccount==999){
System.out.println();
break;
}
}
}

public static String rightJustify(int number, int fieldLength){

StringBuffer sb = new StringBuffer();
if(number==0){
sb.append("000");
}else{
sb.append(number);
}
if(number>0){
while(sb.length() < 3){
sb.insert(0,"0");
}
}else if(number <0){
while(sb.length() <4){
sb.insert(1,"0");
}
}

sb.insert(sb.length()-2, ".");
if(sb.length() < fieldLength){
int length = fieldLength - sb.length();
for(int i = 0; i<length;i++){
sb.insert(0," ");
}
}
return sb.toString();
}

public static String leftJustify(String text, int fieldLength){
StringBuffer sb = new StringBuffer();
sb.append(text);
if(sb.length() < fieldLength){
int length = fieldLength - sb.length();
for(int i = 0;i<length;i++){
sb.insert(sb.length()," ");
}
}
return sb.toString();
}
}
[/java]

Posted: Tue Oct 08, 2002 10:53 pm
by Adil
hello there. opening files is not allowed by the online judge. all the input/output is done to the standard i/o device. so you better remove this line:

Code: Select all

System.setIn(new FileInputStream(new File("p187.in")));
best of luck.

Posted: Wed Oct 09, 2002 11:18 am
by Preben
I removed the line but get this reply:


Here are the compiler error messages:

gcj: Internal compiler error: program jc1 got fatal signal 11 :oops:

some help

Posted: Thu Nov 14, 2002 8:30 pm
by sacra
Read the thread "Giving up on gcj" for some help. Basically, you should try creating an instance of your Main class and call methods on it:

...
public static void main(String args[])
{
Main m = new Main();
m.start();
}

private void start()
{
//implement all functionality here
}
...

This usually works but I don't know why