Olá, qual seria o erro deste codigo?

17 respostas
jubei
package inverte;

import javax.swing.JOptionPane;


class Inverte{
     String InverteFrase(String x){
    
    String invertida;
    //String a = "guia do hardware";
    
    for (int i =x.length()-1;i>=0; i--){
      invertida = (x.charAt(i))+ invertida;
    }
    return invertida;
}
  }
/**
 *
 * @author pessoal
 */

public class Main {

   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
     
     //JOptionPane.showInputDialog("Digite a Frase a ser invertida");
     String a = InverteFrase("123");
     System.out.println(a);
    
    
    }
}

17 Respostas

furutani

Olá

Se você postasse a mensagem de erro ajudava muito a te ajudar.

F

Se mia memoria nao estiver lascada renicializa a string invertida=null:)

jubei

Desculpe,
aqui está o erro, que continua o mesmo depois que iniciei invertida com null.

symbol  : method InverteFrase(java.lang.String)

location: class inverte.Main

a = InverteFrase(123);

1 error

BUILD FAILED (total time: 0 seconds)
F

Cara o metodo nao é estatico vc nao pode chama-lo assim!!

jubei

Estou realmente começando, ainda não entende os tipos de metodos.

F

Bixo realmente é como te falei ,se nao queres instanciar a classe para depois chamar o metodo então faz assim mao nao vejo sentido.E depois a variável invertida deve ser renicializada.Mas ve a lógica do teu código.

package javaapplication1;

 class Inverte{  
    static  String InverteFrase(String x){  
       
     String invertida=null;  
     //String a = "guia do hardware";  
       
     for (int i =x.length()-1;i>=0; i--){  
       invertida = (x.charAt(i))+ invertida;  
     }  
     return invertida;  
 }  
   }  
 /** 
# * 
# * @author pessoal 
 */  
   
 public class Main {  
   
      
     /** 
      * @param args the command line arguments 
      */  
     public static void main(String[] args) {  
         // TODO code application logic here  
          
        
      //JOptionPane.showInputDialog("Digite a Frase a ser invertida");  
      String a = Inverte.InverteFrase("123");  
      System.out.println(a);  
       
       
     }  
 }
jubei

Continua dando o mesmo erro

package inverte;

import javax.swing.JOptionPane;

class Inverte{
static String InverteFrase(String x){

String invertida=null;
//String a = "guia do hardware";

for (int i =x.length()-1;i>=0; i--){
  invertida = (x.charAt(i))+ invertida;
}
return invertida;
}

}

/**

*
  • @author pessoal
    */

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
   
 
 //JOptionPane.showInputDialog("Digite a Frase a ser invertida");
 String a; 
 a = InverteFrase("123");
 System.out.println(a);


}

}

celso.martins

Tente assim:

public class Inverte {
	
	private static String inverteFrase(String x){

		String invertida = "";
		//String a = "guia do hardware";

		for (int i = x.length() - 1; i >= 0; i--){
			invertida += (x.charAt(i));
		}
		return invertida;
	} 

	public static void main(String[] args) {
		String a = inverteFrase("123");
		System.out.println(a);

	}
}

Abraços

EDITADO: Nome de método começa com caixa baixa.

tiidle

Amigo, você não pode chamar o método assim, como está chamando:

a = InverteFrase("123");

Você deve instanciar a classe que tem esse método e depois chamar o método.
Ex: Vamos supor que sua classe chama-se InverteFraseClass
Então, temos:

InverteFraseClass inverte = new InverteFraseClass();

Depois, pode fazer:

inverte.InverteFrase;

No seu caso, terá de fazer:

Inverte [nome_instancia] = new Inverte(); [nome_instancia.InverteFrase("123");

Espero ter ajudado… Abraço.

F

jubei:
Continua dando o mesmo erro

package inverte;

import javax.swing.JOptionPane;

class Inverte{
static String InverteFrase(String x){

String invertida=null;
//String a = "guia do hardware";

for (int i =x.length()-1;i>=0; i--){
  invertida = (x.charAt(i))+ invertida;
}
return invertida;
}

}

/**

*
  • @author pessoal
    */

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
   
 
 //JOptionPane.showInputDialog("Digite a Frase a ser invertida");
 String a; 
 a = InverteFrase("123");
 System.out.println(a);


}

}


Cara vc nao prestou atenção no meu código amigão!Eu chamo a classe!!!

claudneto

Não precisa criar um objeto...eu acho que com a própria String a vc consegue...

a.InverteFrase("123");

Mas ficaria melhor criando outro objeto mesmo...

InverteFraseClass inverte = new InverteFraseClass();

inverte.InverteFrase(a);

Se vc não entender alguma coisa...pergunta aqui que a gente te explica ou recomenda alguma apostila!

celso.martins

tiidle:
Amigo, você não pode chamar o método assim, como está chamando:

Pode sim, é só o método ser estático.

jubei

Obrigado a todos. Deu certo assim, mas a lógica para inverter não funcionou, imprimiu a palavra normalmente.
package inverte;

import javax.swing.JOptionPane;

class Inverte{
static String InverteFrase(String x){

String invertida="";
//String a = "guia do hardware";

for (int i =x.length()-1;i>=0; i--){
  invertida = (x.charAt(i))+ invertida;
}
return invertida;
}

}

/**

*
  • @author pessoal
    */

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

Inverte funcao = new Inverte();
String a = funcao.InverteFrase(“dados”);

System.out.println(a);

}

}

claudneto

celso.martins:
tiidle:
Amigo, você não pode chamar o método assim, como está chamando:

Pode sim, é só o método ser estático.

Realmente…

Poderia se fosse estático!

NESTE CASO…vc não pode chamar o método assim!

claudneto

class Inverte { public void inverte (String texto) { String invertida = ""; for (int i = texto.length() - 1; i >= 0; i--) { System.out.print(texto.charAt(i)); } System.out.println(); } }

ou

class Inverte { public void inverte (String texto) { String invertida = ""; for (int i = texto.length() - 1; i >= 0; i--) { invertida = invertida + texto.charAt(i); } System.out.println(invertida); } }

Mude ele como quiser…mas assim funciona…

jubei

Pronto, assim funcionou.

import javax.swing.JOptionPane;

class Inverte{
static String InverteFrase(String x){

String invertida="";
//String a = "guia do hardware";

for (int i =x.length()-1;i>=0; i--){
  invertida += (x.charAt(i));
}
return invertida;
}

}

/**

*
  • @author pessoal
    */

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

Inverte funcao = new Inverte();
String a = funcao.InverteFrase(“dados”);

System.out.println(a);

}

}

claudneto

Se funcionou…td bem…

Mas coloque seu código com as tags corretas!

//Assim é bem menos poluído!
Criado 22 de setembro de 2008
Ultima resposta 22 de set. de 2008
Respostas 17
Participantes 6