Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concept error? 1

Status
Not open for further replies.

chals1

Technical User
Sep 3, 2003
73
ES
Javac shows an only kind of error like this:

non-static method lectura() cannot be referenced from a static context
c2=lectura();

Here is the code about:

import consola.*;
class menu {
EntradaConsola consola = new EntradaConsola();
char eleccionMenu;
public static void main(String args[]) {
int n,k;
Complejo c1,c2,c3;
Complejo[] c4;
do {
System.out.println("Elija una opción:\n");
System.out.println("1. Sumar dos complejos");
System.out.println("2. Restar dos complejos");
System.out.println("3. Opuesto de un complejo:");
System.out.println("4. Conjugar");
System.out.println("5. Multiplicar dos complejos");
System.out.println("6. Dividir dos complejos");
System.out.println("7. Invertir");
System.out.println("8. Potencia");
System.out.println("9. Raíces");
eleccionMenu = consola.leerchar();
} while( eleccionMenu < '1' || eleccionMenu > '9');
System.out.println(&quot;\n&quot;);

switch (eleccionMenu) {
case '1':
c1=lectura();
c2=lectura();
c3 = c1.sumar(c2);
break;
case '2':
c1=lectura();
c2=lectura();
c3 = c1.restar(c2);
break;
case '3':
c1=lectura();
c2 = c1.menos();
break;
case '4':
c1=lectura();
c2 = c1.conjugar();
break;
case '5':
c1=lectura();
c2=lectura();
c3 = c1.multiplicar(c2);
break;
case '6':
c1=lectura();
c2=lectura();
c3 = c1.dividir(c2);
break;
case '7':
c1=lectura();
c2 = c1.invertir();
break;
case '8':
c1=lectura();
c2 = c1.potencia(n);
break;
case '9':
c1=lectura();
c4 = c1.raíces(n);
break;
}
}
public Complejo lectura() {
char eleccionRead;
double a;
double b;
Complejo c;

do {
System.out.println(&quot;1. Módulo_Argumento&quot;);
System.out.println(&quot;2. Parte real_Parte imaginaria&quot;);
eleccionRead = consola.leerchar();
} while( eleccionRead < '1' || eleccionRead > '2');

a = consola.leerdouble();
b = consola.leerdouble();

c = new Complejo(a,b);

if((eleccionMenu < 5) && (eleccionRead == 1)) {
a = c.getParteReal();
b = c.getParteImaginaria();
}
if((eleccionMenu > 4) && (eleccionRead == 2)) {
a = c.getMódulo();
b = c.getArgumento();
}

c = new Complejo(a,b);
return c;
}

}

 
What I normally do to solve that problem is in the main method create an instance of the class and then call the method &quot;doIt() on it. Then you are out of the static context.

Code:
public class DecimalFormatMain {

  public DecimalFormatMain() {
  }

  public static void main(String[] args) {
    DecimalFormatMain ex = new DecimalFormatMain();
    ex.doIt();
  }

  public void doIt() {
    double d1 = 8904.43;
    double d2 = 1234432.34;
    double d3 = 234554334.00;
    DecimalFormat format = new DecimalFormat(&quot;#,##0.00&quot;);
    System.out.println(&quot;d1 = &quot; + format.format(d1));
    System.out.println(&quot;d2 = &quot; + format.format(d2));
    System.out.println(&quot;d3 = &quot; + format.format(d3));
  }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top