Runtime error in UVA Online Judge [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm doing problem 10003 "cutting sticks" of the UVa online judge, I'm pretty sure my code works and I think I' correctly exiting the problem. But still I get a runtime error, I read somewhere that is because I'm not exiting the application like I'm supposed to do it. I wish you guys can help me with this problem.
import java.io.*;
class MAIN {
static int len, c, min;
static int mat[][] = new int[52][52];
static int arr[] = new int [52];
static BufferedReader BufferReader = new BufferedReader(new InputStreamReader(System.in));
public static void llenaarr()throws IOException{
for(int i=0; i<c-1; i++) {
arr[i+1] = Integer.parseInt(BufferReader.readLine());
}
arr[0] = 0;
arr[c] = len;
}
public static void llenamat(){
for(int i=0; i<=c; i++) {
for(int j=0;j<=c;j++) {
mat[i][j] = Integer.MAX_VALUE;
}
}
for(int i=0; i<c; i++) {
mat[i][i] = 0;
mat[i][i+1] = 0;
}
mat[c][c] = 0;
for(int i=0; i<c-1; i++) {
mat[i][i+2] = arr[i+2] - arr[i];
}
}
public static void minimo(){
for(int k=3; k<=c; k++) {
for(int i=0; i<=c-k; i++) {
for(int j=i+1; j<=i+k-1; j++) {
min=mat[i][j] + mat[j][i+k] + arr[i+k] - arr[i];
if((min< mat[i][i+k])) {
mat[i][i+k] = min;
}
}
}
}
}
public static void main (String args[]) throws IOException {
while((len = Integer.parseInt(BufferReader.readLine())) > 0){
c = (Integer.parseInt(BufferReader.readLine()))+1;
llenaarr();
llenamat();
minimo();
System.out.println("The minimum cutting is "+mat[0][c]+".");
}
}
}

Your program is throwing some kind of runtime Exception during processing input. To verify this, you should encapsulate your code (the part that operates on the input and produces output) in a try-catch block.
The first thing to try to catch is the generic Exception. This will catch any type of runtime exception. Run it through the online judge again, and you should now see a Wrong Answer result (assuming something is not processing right, because it is throwing an exception after all).
Now comes the detective work. Go back to your source code and look at all the calls you make and use the API Docs to check all the possible exceptions they may throw. For example, if you call Integer.parseInt(inputString), it may be throwing a NumberFormatException. So above your catch (Exception e) block, start adding some more specific exceptions you think may be causing the problem.
Ideally (if you have the time and patience) you'll want to add these one at a time and resubmit to the online judge after each one. When the result moves from Wrong Answer back to Runtime Error, you'll know you've found the exception that is causing the problem. From here, hopefully this will help you narrow down what to focus on (to fix) in your source code solution.
Good luck.

Related

On the Codechef aug long challenge, AUG21C >CHFINVNT, this is the code I wrote, but the submit doesn't work. Its showing RE(NZEC) error

On the Codechef August long challenge, AUG21C > CHFINVNT, this is the code that I wrote, but the submit doesn't work. It's showing RE(NZEC) error.
The output still works.... but it won't submit. Please help.
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner scr = new Scanner(System.in);
try {
int T = scr.nextInt();
while(T-- > 0)
{
int N = scr.nextInt();
int p = scr.nextInt();
int K = scr.nextInt();
int[] A = new int[N];
for(int i=0; i<N; i++)
{
A[i%K]++;
}
int sum=0;
for(int i=0; i<p%K; i++)
{
sum+=A[i];
}
int add = (p - (p%K)) / K;
add++;
sum = sum + add;
System.out.println(sum);
}
}catch(Exception e){}
}
}
Obviously, you have a flaw in your code. NZEC means non-zero exit code. Your code probably triggered an uncaught exception. Although you have a broad catch statement, there are exceptions outside of your catch clause.
In the question statement, N can be as large as 10^9.
If that happens, int[] A = new int[N]; tries to allocate that memory. That is over the memory constraints. Probably that is causing the problem. Even if you could allocate that memory, you would hit the time limits: even counting to 10^9 shall fail on CodeChef.
Not directly related to the question, but I want to comment on your code from the perspective of software engineering and competitive coding best practices.
Having a broad catch block will hide any exceptions you may have. Even if you get an index-out-of-bounds exception, you shall not be able to see that in your trials. You may avoid catching broad exceptions.
Your code does not have a proper indentation. That makes reading & debugging cumbersome. Especially in competitive programming, speed is important. So, a proper indentation is critical in that regard. You may just use an IDE to format the code.
Your Java code does not follow the general conventions of Java. One of the rules is "local variables start with a lowercase". The local variables A and N violate that rule.
The code you submitted contains many empty lines. While submitting your code to StackOverflow, removing unnecessary lines shall ease the reading and increase the probability of getting a good answer.

If keep receiving runtimeException

The following code works fine with the eclipse but then in online editor I keep receiving runtimeException(NoSuchElementFoundException) please help me where am I going wrong?
{I have used sieve of eratosthenes alogirthm to find prime number in required range by the user}
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
if(sc.equals(""))
{
sc.close();
}
Integer t = sc.nextInt();
while(t>0)
{
int m = sc.nextInt();
int n = sc.nextInt();
int prime[]= new int[n+1];
for(int i=0;i<=n;i++)
{
prime[i]=1;
}
prime[0]=0;
prime[1]=0;
for(int i=2;i<Math.sqrt(n);i++)
{
if(prime[i]==1)
{
for(int j=2;i*j<n;j++)
{
prime[i*j]=0;
}
}
}
for(int i=m;i<n;i++)
{
if(prime[i]==1)
{
System.out.println(i+" ");
}
}
System.out.println();
t--;
}
}
}
I tried your code over the site you mentioned. I changed the class visibility to be default to avoid some compilation error. It produced internal error with no further details and it suggested to test the code on https://ideone.com/ site. I used the sample test case and it passed successfully on ideone site..I suggest that you use hacker rank, I think it is more handy. In hacker rank you can unlock test cases to trace errors, bugs in your code, also you can have discussion with colleagues there over the problem. On the other hand I couldn't get the exception you mentioned "NoSuchElementFoundException" if you have detailed stack trace kindly include it and I shall update my answer accordingly.

Create bubble sort code, but facing some errors [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Had a problem with compilation, getting error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at practise.code.main(code.java:11)
Here is my code:
package practise;
public class code {
static int number[]={1,8,5,9,4,7};
static int c[] = new int[number.length];
static int p=0;
static int q;
public static void main(String[] args){
change(number);
System.out.println("Array Before Bubble Sort");
for(int y: c){
System.out.print(y + "\t");
}
}
public static void change (int x[]){
for(int a: x){
for(int i=0; i<=x.length; i++){
if(a > x[i]){
continue;}
else {
p++;}
q = x.length - p - 1;
c[q] = a;
}
}
}
}
}
Some meta-help for future reference:
Compiler errors: these are when you experience an error that occurs at compile time. For Java, this is when you use javac, and your Java code is being turned into bytecode files for interpretation later.
Runtime errors: this is when you experience an error that occurs when you run your code. For Java, this is when you use java, and your Java code is being run.
If you find the two confusing, add into your question the thing you typed in order to experience the error. Specify all the flags and options you used, and format it with a code block, for example like this:
java -jar code.jar
When asking questions here - or indeed anywhere on the web where you can get technical help - try to ask yourself what clarifications you would need if you saw your question for the first time. Your first edit did not include your code, so ask yourself: would you be able to ascertain someone else's similar problem without code? Broadly here the answer is "no", and thus the moral of this story is: always include your code.
Also, do spend a moment to learn the code formatting tools. To use them, paste your block of code into the question, select it, and click the "code" button. It will apply a four-space Markdown indent, which you can now see in the question.
If you need to add clarifications to your post, it is OK to add them as comments, but do also edit the body of the question so that new readers can understand the question. It is well worth spending time making it as readable and clear as possible, so you can get the best possible help, and so that people do not take a look and decide that another question is a better use of their time.
Since you are using an IDE, do you get any warnings/errors in the editor, to help you identify potential problems in you code? If so, and you do not understand them, then paste them into your question, in order to clarify it.
Thanks for your advices... Finally made it working..
thank you once again
package practise;
public class code{
public static void main(String[] args){
int[] Array = {5,8,6,4};
int[] newArray = new int[Array.length];
int a, b, c, d, e, f =1;
for(int z : Array ){
d=0;
for(int i=0; i<Array.length; i++){
a = z; b = Array[i];
if( a >= b){
continue;}
else{
d++;}
}
c = Math.subtractExact(Array.length , d);
e = Math.subtractExact(c, f);
for(int j=0; j< Array.length; j++){
while( j == e){
newArray[j] = z;
break;
}
}
}
System.out.println("Here is your old Array :");
for(int k : Array){
System.out.println(k);
System.out.println("Here is your new Bubble Sort Array :");
for(int q : newArray){
System.out.println(q);
}
}
}
}

No error yet no output. Help.(Sudoku solver getter) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to add text fields a01, a02, ... to the array a.
I want to display the value of val so that I would know if the text is being taken from text fields. This code does not show any errors, but, well, it doesn't give me output as well.
int i, j;
JTextField[][] a = new JTextField[9][9];
int[][] val = new int[9][9];
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
val[i][j] = Integer.parseInt(a[i][j].getText());
System.out.println(val[i][j]);
}
}
It is from my old question here.
You did not give them value
int i,j; // counter
JTextField[][] a = new JTextField[9][9];
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
JTextField tf = new JTextField();
tf.setText("a"+i+j);
a[i][j] = tf;
}
}
In your version the call to a[i][j].getText() should throw a NullPointerException. This should either kill your application, end up on the console or you have somewhere something like
try {
// more code here
} catch (Exception ex){}
which will silently swallow the exception and is veeeeery bad practice.
The code you have shown us does generate an error - it throws a NullPointerException when you try to access the text of the text fields via a[i][j].getText().
You didn't initialize the JTextFields in your array a.
ideone example

Comparing elements of two arrays in a for loop (java)? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
It seems to be a very common question relating with arrays and comparing in java, however I couldn't find the right answer in all of these for my case.
In this application I am trying to make a program which 'encrypts' text given by the user. For example: user gives characters 'a b c' and the program returns it as '# # $'. But as you may notice I am having some issues in the code
"pozita[i] = j;".
Why won't this code work? It doesn't give me an error? Or is there anyway of doing it as "new pozita[i]" or anything like that?
Well, I'd be glad if someone could help me through. I am stuck for a while. Thanks in advance! :)
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class TestPerProgram extends JFrame
{
char[] alfabeti = {'a','b','c','r','n','t'};
char[] kodimi = {'#','#','%','*','^','$'};
int[] pozita;
//Scanner merr = new Scanner(System.in);
String fn = JOptionPane.showInputDialog("Jepe tekstin:");
char[] input = fn.toCharArray();
void numro()
{
for (int i=0; i<=input.length; i++)
{
for(int j=0; j<=input.length; j++)
{
if(alfabeti[j] == input[i])
{
pozita[i] = j;
System.out.println(pozita[i]);
}
}
}
/*
for (int k=0; k<=input.length; k++)
{
System.out.println(pozita[k]);
}
*/
}
public static void main(String[] args)
{
TestPerProgram pjesa = new TestPerProgram();
pjesa.numro();
}
}
I'm not 100% clear on how your algorithm is supposed to work, but it seems you may want to replace the line
pozita[i] = j;
with
pozita[i] = kodimi[j];
Right now, you are only writing the matching index to pozita, not a replacement character.
If my assumption is correct, you would also change
int[] pozita;
to
char[] pozita;
and initialize it to an array of length input.Length.
You never instantiated the array pozita. After you instantiate pozita, you can then start overriding the values in pozita. You are assigning j to posita[i] , posita is null.
Do something like:
int posita[] = new int[20]
and if you don't want to set size then just use an arraylist.
You have not requested memory to be allocated for your variable pozita or otherwise instantiated it. The way you are currently using it, you would write pozita[] = new int[input.length]; at some point after retrieving your input from the user.

Categories