Compile error when sending to third party verify

3

I'm practicing Java in SPOJ and came up with a simple exercise:

  

Continue typing a number and when 42 appears, stop printing.

And so I made it quiet here on my PC compiling as expected. The problem is that when I submit to SPOJ the solution it always gives compilation error with the words:

AndIdonotknowwhatelsetodotoacceptthecode.

I'llleavethesourcecodehereandtheproblemstatementhere.

importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){intvalor[]=newint[5];inti;for(i=0;i<valor.length;i++){Scannerin=newScanner(System.in);valor[i]=Integer.parseInt(in.nextLine());if(valor[i]>=100){Scannerln=newScanner(System.in);valor[i]=Integer.parseInt(ln.nextLine());}}i=0;while(valor[i]!=42){System.out.println(valor[i]);i++;}}}

    
asked by anonymous 30.09.2015 / 17:03

2 answers

3

You can not start a new Scanner with each pass in the loop. Then I drew your tie creation. I also have read the Int instead of converting, I think it gets better, but you can go back to what it was if you wish. I also had to sort out the condition of the while end that popped the limit of array .

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        int valor[] = new int[5];
        int i;
        Scanner in = new Scanner(System.in);
        for (i = 0; i < valor.length; i++) {
            valor[i] = in.nextInt();
            if (valor[i] >= 100){
                valor[i] = in.nextInt();
            }       
        }
        i = 0;
        while(i < valor.length &&valor[i] != 42){
            System.out.println(valor[i]);
            i++;
        }
    }
}

See running on ideone .

The code has logic errors but is not the focus of the question. For example, if the person types an invalid value the second time, it passes. The code has coding style problems that do not affect the end result. This is the tip.

    
30.09.2015 / 17:19
1

The problem with these sites, such as SPOJ and UVA, is that some do not tell you how the input should be done. I've already caught a lot trying to figure out a solution to UVA input problem until, after all, after having to take over some things and also find a code template on one of the sites, I did it.

First you have to assume that it is not a user who is running your program and, rather, a machine.

This machine will grab a txt file with chunky values and run your program with the incoming txt file. It is the same as you run your program through the terminal with the command "java Main < input.txt". You can do this to test your code.

You have to assume, too, that each line of the txt file will output its output at the same instant. That is, in your example, in your program you have to read the number 1 and already generate the output of number 1 on the screen.

Knowing this, and with the proper code, you'll never have input problems on such sites again. Here is the code:

import java.io.IOException;

class Main implements Runnable {

  static String ReadLn(int maxLength) {

    byte line[] = new byte[maxLength];
    int length = 0;
    int input = -1;

    try {

      while (length < maxLength) {

        input = System.in.read();

        if ((input < 0) || (input == '\n'))
          break;

        line[length++] += input;

      }

      if ((input < 0) && (length == 0))
        return null;

      return new String(line, 0, length);

    } catch (IOException e) {

      return null;

    }

  }

  public static void main(String args[]) {

    Main myWork = new Main();
    myWork.run();

  }

  @Override
  public void run() {

    new myStuff().run();

  }

}

class myStuff implements Runnable {

  @Override
  public void run() {

    // O SEU PROGRAMA AQUI

    int input = 0;

    String linha;

    while((linha = Main.ReadLn(255)) != null && (input =   Integer.parseInt(linha)) != 42) {

      System.out.println(input);

    }

  }

}

Notice that here is the program (everything it should do):

class myStuff implements Runnable {

  @Override
  public void run() {

    // O SEU PROGRAMA AQUI

    int input = 0;

    String linha;

      while((linha = Main.ReadLn(255)) != null && (input = Integer.parseInt(linha)) != 42) {

        System.out.println(input);

    }

  }

}

This part says to read to the end of the text file or until you find the number 42:

while((linha = Main.ReadLn(255)) != null && (input = Integer.parseInt(linha)) != 42)

And that's it! I hope it all right! I hope I've helped! Any questions just talk!

    
01.10.2015 / 01:09