• Register
0 votes
29 views
Hi, when i try to execute this code i always get the error message from above, and i really want to know why, already tried to put a “public class XXXX” before the code, but not work anyway.
public static String invertirPalabras(String cadena) {
    cadena += " ";
    String palabra = "";
    String nuevaCadena = "";
    for (int i = 0; i < cadena.length(); i++) {
        palabra = cadena.charAt(i) + palabra;
        if (cadena.charAt(i) == ' ') {
            nuevaCadena += palabra;
            palabra = "";
        }
    }
    return nuevaCadena.trim();
}
0 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution

Since your class contains static  methods only (according to your question, the class has one static method ), the class must have “static” modifier, since it cannot be instantiated. Only classes with at least one not static method can be instantiated. The rule is valid for both: Java and C#.

So, I would suggest to rewrite this piece of code as follows:

public static class  Helper{ 

    public static String invertirPalabras(String cadena) { 

         cadena += " ";

         String palabra = "";

        String nuevaCadena = "";

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

           palabra = cadena.charAt(i) + palabra;

           if (cadena.charAt(i) == ' ') {

                 nuevaCadena += palabra; 

                  palabra = ""; 

        }

     }

     return nuevaCadena.trim();

  }

 }

9 5 2
17,020 points

Related questions

3 votes
1 answer 1.6K views
1.6K views
Problem: Hello Kodlogs, I am a novice Java programmer. I just wrote my very first program a few minutes earlier. Now I want to write a program that can add two different integer numbers for me. I guess I done my coding well but it says something wrong with my writing and throws back an error: &ldquo ... []args){ int x = 10; int y = 15; int sum = x+y; System.out.println("Sum is: " + sum); } }
asked Mar 23, 2020 Gavin 15.3k points
0 votes
1 answer 44 views
44 views
Problem: Hi there! I have been learning to program for a long time now and I recently learned inheritance in Java. I wrote the following program for practice: class Institutions { public void print_institutions() { System.out.println("This is an institution"); } } class ... , I have no idea what is causing this error. Can anybody here please clarify the cause of the error and its solution? Thanks!
asked Dec 29, 2020 Code Learner 6.2k points
1 vote
1 answer 20 views
20 views
Problem: I Created a Loan.java and compile it with JDK 1.6 and Run Loan.class with JRE 1.5. C:\Program Files\Java\jre1.5.0_06\bin>java -version java version "1.5.0_06" The following thing gives an error message: C:\Program Files\Java ... Source)         at java.net.URLClassLoader.defineClass(Unknown Source)         at java.net.URLClassLoader.access$100(Unknown Source) what should i do next ..
asked Mar 27, 2020 LizzyM 6.1k points
1 vote
1 answer 31 views
31 views
Problem: I am totally new in programming language. So I tried to write a program using: public static void main(String args[]) But it seems to give error : public static void main(String args[]) ^ IndentationError: unexpected indent What should I do?
asked Apr 2, 2020 LizzyM 6.1k points
0 votes
1 answer 60 views
60 views
Problem : I am facing following Error. Status 1 error, param missing, error because at least one required parameter is missing. Can anybody tell me the correct status code to give when the parameter is missing from the request? Is 400 the correct one? Or is it 404? [GET /search?q=ok] => 200 OK [GET /search] => 400 Bad Request? or 404 Not Found? Or 422 Unprocessable Entity? Others?
asked Dec 2, 2019 alecxe 7.5k points
0 votes
1 answer 11 views
11 views
java file with a default class access specifier get compiled?
asked Jan 24 TeamScript 13.5k points
0 votes
1 answer 44 views
44 views
How can I fix this?
asked Dec 27, 2020 TeamScript 13.5k points
0 votes
3 answers 111 views
111 views
Problem: When the class name and the filename of a given Java program doesn&rsquo;t match I get this error. Considering an example where file name is bal.java: public class Bal { public static void main(String[] args) { System.out.println("Hello, chutmarani!"); } } 1 error found : File:bal.java [line 1] Error: class Bal is public, should be declared in a file named bal.java Can anyone help?
asked Mar 23, 2020 LizzyM 6.1k points
0 votes
1 answer 13 views
13 views
I'm sorry if this question is of low quality, but I really need clarification on this. I hope experts in Java will help me.
asked Feb 13 TeamScript 13.5k points