Code doesn't compile on Hackerrank - java

The following code works fine on my IDE but I keep getting a "Compile Time Error" when I add it to Hackerrank. What am I doing wrong?
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.next();
System.out.println(delete(input));
}
public static int delete(String in){
char[] arr = in.toCharArray();
int del = 0;
for(int x=0; x < arr.length-1; x++){
if(arr[x] == arr[x+1]){
del++;
}
}
return del;
}
}

Your code works fine in Intellij too. But there is a wrong exist. Both main and delete methods are static. You use 'in' for both methods. Therefore just change the name of the string in delete method.

Related

CoderByte says all of my testcases are wrong?

I tried coderbyte and looks something is wrong with it. The first challenge is simply to reverse a string. I did this in java:
import java.util.*;
import java.io.*;
class Main {
public static String FirstReverse(String str) {
char[] chars = str.toCharArray();
String ret = "";
for (int i = chars.length - 1; i >= 0; i--) {
ret += chars[i];
}
return ret;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(FirstReverse(s.nextLine()));
}
}
It said, that three test cases had wrong output and there was the correct output. I tried running the code with the specified cases and it printed the same string as correct output for that case. So I tried resubmitting it and it said that only one test case was correct and all other had wrong out put. So I said OK and rewrote my code this way:
import java.util.*;
import java.io.*;
class Main {
public static String FirstReverse(String str) {
return new StringBuilder(str).reverse().toString();
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(FirstReverse(s.nextLine()));
}
}
Unfortunately it still says only one test case was successful... Any ideas what happened? Thanks

Correct Output for java method

I am trying to create a method that counts the number of times a specified character appears in a given string, by printing the sentence and the specified character and its count.
This is what I have completed so far:-
import java.util.Scanner;
public class Program4 {
public int count ( String sentence, char letter)
{
int times = 0;
for (int x=0;x<sentence.length();x++)
{
if (sentence.charAt(x) ==letter){times++;}
}
return times;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Program4 program4 = new Program4();
}
program4.count("Hello World",'o');
scan.close();
}
}
I understand I need a "system.output.println" but I don't know what value goes inside to get the output I am looking for. I would appreciate any help, I am beginner with java. Thank you.
it works, you had compile time error in code
import java.util.Scanner;
public class Program4 {
public int count(String sentence, char letter) {
int times = 0;
for (int x = 0; x < sentence.length(); x++) {
if (sentence.charAt(x) == letter) {
times++;
}
}
return times;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Program4 program4 = new Program4();
int timesCount = program4.count("Hello World", 'o');
System.out.println("count is :" + timesCount);
scan.close();
}
}

Getting Runtime Error(NZEC) on SPOJ AND CODECHEF?? Couldn't find why this error is occuring on my code

I am getting RUNTIME ERROR(NZEC) on my following code while running my code on competetive programming sites.But could not understand where is the problem in my code as it is running absolutely fine on eclipse:
My code is:
import java.util.*;
public class Main {
public boolean isPalindrome(int number) {
String s1 = String.valueOf(number);
StringBuffer s = new StringBuffer(s1);
String s2 = String.valueOf(s.reverse());
if ((Integer.parseInt(s1)) == (Integer.parseInt(s2)))
return true;
else
return false;
}
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int m1[] = new int[t];
int k=0;
for (int i = 0; i < t; i++) {
int m = sc.nextInt();
for (int j = m + 1;; j++) {
Main main = new Main();
if (main.isPalindrome(j)) {
m1[k++] = j;
break;
}
}
}
for(int l=0;l<m1.length;l++)
{
System.out.println(m1[l]);
}
}
}
Thanks in advance!!
Did you submit this code on CodeChef/Spoj?
Your code seems to be fine but you should provide the input or run your code on an interactive console(as we do in terminal).
You can observe your error by commenting on the scanner lines and initializing the variables in the code itself as I checked this by running it on CodeChef
Else, Try adding this Scanner class.
import java.util.Scanner; // Import the Scanner class
This error is caused when your code returns Non-Zero Error Code(NZEC)
In languages which have exception handling like Java, Python etc we can use exception hadling using try - catch blocks.
NZEC is a runtime error.
for detailed answer to the above question
click here

The infamous object bug

I discovered what i think is a bug whilst using netbeans. When i call up my method to sort an array containing names(ob.sort) in alphabetical order it automatically sorts another array which contains the original names when it isn't supposed to as the original names is not assigned to anything after it has been populated with input at the beginning(ob.input).
I experienced this problem whilst writing larger programs(encountered more than once), but i made a simpler one to demonstrate this problem. It looks like much as i copied the class methods an pasted it below the main class making it easier for you to trace the variables in the program.
public static void main(String args[]){
ObjectTest ob = new ObjectTest();
ob.input();
String x[] = ob.getNames();
System.out.println(x[0]);
ob = new ObjectTest(x);
System.out.println(x[0]);
ob.sort();
System.out.println(x[0]);
String y[] = ob.getNamesrt();
System.out.println(x[0]);
}
}
/*import java.io.*;
import javax.swing.*;
public class ObjectTest {
String name[];
String namesrt[];
public ObjectTest(){
name = new String[3];
namesrt = new String[3];
}
public ObjectTest(String j[]){
namesrt = j;
}
public void input(){
for(int i = 0; i < name.length; i++){
name[i] = JOptionPane.showInputDialog("Enter name");
}
}
public void sort(){
if(!(namesrt == null)){
for(int i = 0; i < namesrt.length; i++){
for(int c = i + 1; c < namesrt.length; c++){
if(namesrt[i].compareToIgnoreCase(namesrt[c]) > 0){
String n = namesrt[i];
namesrt[i] = namesrt[c];
namesrt[c] = n;
}
}
}
}
else{JOptionPane.showMessageDialog(null,"Names not received");}
}
public String[] getNames(){
return name;
}
public String[] getNamesrt(){
return namesrt;
}
public void setNames(String j[]){
name = j;
}
public void setNamesrt(String j[]){
namesrt = j;
}
}*/
I discovered what i think is a bug whilst using netbeans.
Well, it may be a bug in your code. It's not a bug in Java or in Netbeans. It's just demonstrating the fact that arrays are reference types in Java, and the way that objects work.
Here's a short but complete program demonstrating the same effect:
public class Test {
public static void main(String[] args) {
String[] x = { "hello" };
// Copy the *reference*
String[] y = x;
System.out.println(y[0]); // Prints "hello"
x[0] = "new value";
System.out.println(y[0]); // Prints "new value"
}
}
The values of x and y here are references to the same array object... so if the array is changed "through" x, that change is still visible as y[0].
If you want to make your code create independent objects, you'll want to change this:
public ObjectTest(String j[]){
namesrt = j;
}
to:
public ObjectTest(String j[]){
namesrt = j.clone();
}
(Ideally change it to declare the parameter as String[] j, or better yet fix all your variable names to be more meaningful, but that's a different matter.)

why does it stop half way through my code?

So the first part creates a vector and adds a digit to the 10 slots. Then after this nothing happens, i have no errors in my code but it just stops.. why?
package ovn7;
import java.util.Scanner;
public class ovn7a {
int []vektor;
Scanner scan = new Scanner(System.in);
public static void main(String []args) {
int []vektor = new int[10];
for(int k=1; k<10; k++){
vektor[k]=0+k;
System.out.println(k);
}
}
public int find(int tal) {
System.out.println("tal");
tal = scan.nextInt();
int i = 0;
while(i<10 && vektor[i] != tal) {
i++;
}
return (i <10) ? i : -1;
}
}
This is your main method:
public static void main(String []args) {
int []vektor = new int[10];
for(int k=1; k<10; k++){
vektor[k]=0+k;
System.out.println(k);
}
}
That's all your program does - when it hits the closing right brace of the main method, execution ends. If you want it to execute public int find(int tal) as well, you need to include a method call to your main method:
int index = find(5); //for example
Remember, the main method is the only one that is called automatically when executing the program! You'll have to call find yourself inside main.
EDIT: per request, an example of main with the method call included:
public static void main(String []args) {
int []vektor = new int[10];
for(int k=1; k<10; k++){
vektor[k]=0+k;
System.out.println(k);
}
int index = find(5); // <-- this find(5) here is a method call for find!
System.out.println("The method returned a value of " + index + ".");
}
You can replace that "5" with any integer, as the method find accepts an integer as an argument. (as a side note, it doesn't matter which integer you pass to find - it overwrites the argument with a new value anyway)

Categories