0

Trying to solve the kattis exercise "abc" (https://kau.kattis.com/problems/abc). Program runs fine in eclipse on my computer, but for some reason I get "run time error" when trying to submit my solution online. Any ideas what I should change? See link for problem formulation. As input the program takes 3 integers followed by 3 letters.

public class ABC {

  public static void main(String[] args) {
    int[] list = {0, 0 ,0};
    char[] order = {0 ,0 ,0};
    int  i;

    Scanner ob = new Scanner(System.in);

    for(i=0; i<list.length; i++) 
    {
        int a = ob.nextInt();
         list[i] = a;
    }
    int n=list.length;

    for (i = 0; i < n-1; i++) {

        for (int j = 0; j < n-i-1; j++) {
            if (list[j] > list[j+1]) 
            { 

                int temp = list[j]; 
                list[j] = list[j+1]; 
                list[j+1] = temp; 
            } 
        }
    }
    String s = ob.nextLine();

    for(i=0; i<order.length; i++)
    {
        order[i] = s.charAt(i);
    }

    ob.close();

if(order[0] == 'A' && order[1]=='B')
{
    System.out.print(list[0]+" ");
    System.out.print(list[1]+" ");
    System.out.print(list[2]);
}
if(order[0] == 'A' && order[1]=='C')
{ 
    System.out.print(list[0]+" ");
    System.out.print(list[2]+" ");
    System.out.print(list[1]);
    }
if(order[0] == 'C' && order[1]=='B')
{
    System.out.print(list[2]+" ");
    System.out.print(list[1]+" ");
    System.out.print(list[0]);

}
if(order[0]=='C' && order[1]=='A')
{
    System.out.print(list[2]+" ");
    System.out.print(list[0]+" ");
    System.out.print(list[1]);
}

if(order[0] == 'B' && order[1]=='A')
{ 
    System.out.print(list[1]+" ");
    System.out.print(list[0]+" ");
    System.out.print(list[2]);

}
if(order[0] == 'B' && order[1]=='C')
{ 
    System.out.print(list[0]+" ");
    System.out.print(list[2]+" ");
    System.out.print(list[1]);
}
  }
}
Ole V.V.
  • 65,573
  • 11
  • 96
  • 117

1 Answers1

0

That's because the Scanner.nextInt() method does not read the newline character in your input. while taking the string input use

ob.nextLine(); String s = ob.nextLine();

to move to the next line and read the String from the input.

Also using switch case for each char which will make the code better and will prevent writing if conditions again and again. Hope this helps. Cheers!