Build error when sending to third party check

Asked

Viewed 110 times

3

I am practicing Java in SPOJ and came a simple exercise:

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

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

inserir a descrição da imagem aqui

And I don’t know what else to do to get you to accept the code.

I will leave here the source code I made and the statement of the problem.

    import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        int valor[] = new int[5];
        int i;

        for (i = 0; i < valor.length; i++) {

            Scanner in = new Scanner(System.in);
            valor[i] = Integer.parseInt(in.nextLine());

            if (valor[i] >= 100){

                Scanner ln = new Scanner(System.in);
                valor[i] = Integer.parseInt(ln.nextLine());
            }       
        }

        i = 0;
        while(valor[i] != 42){
            System.out.println(valor[i]);
            i++;
        }
    }
}

inserir a descrição da imagem aqui

  • Take advantage of what you did in ideone and put the link there.

  • It was bad hehe, here the link: http://ideone.com/0DyF7F

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

3

You can’t start a new Scanner every step in the loop. Then I took your creation out of the loop. I also had the Int instead of converting, I think it looks better, but you can go back to what it was if you wish. I also had to fix the condition of while that would exceed the limit of the array.

import java.util.Scanner;

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The code has logic errors but is not the focus of the question. For example, if you enter an invalid value the second time, it passes. The code has encoding style problems that do not affect the final result. The tip.

1

The problem with these sites, such as SPOJ and UVA, is that some do not say how the input should be done. I’ve caught up a lot trying to figure out a solution to input problem in UVA until, at last, after having to take over some things and, also, find a code template on one of the sites, I got.

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

This machine will take a txt file with abirtrários values and run your program with the input txt file. It is the same as running your program from the terminal with the command "java Main < input.txt". You can do this to test your code.

You have to assume, too, that at each line of the txt file your output will have to be generated 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 the number 1 on the screen.

Knowing this and, with the proper code, you will never again have input problems on sites of this type. Follow 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 works out! I hope I’ve helped! Any questions just talk!

  • So this Main class will make all the programs I do (using this structure) work or this solution is just for this problem?

  • All programs

  • Very orbed! D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.