Error: non-static variable this cannot be referenced from a static context
Publicado por Roberto (13 intervenciones) el 27/10/2020 09:26:06
Hola.
Para empezar os pido disculpas por éste nuevo mensaje. Estoy empezando en el mundo de Java, llevo algún que otro programilla, pero los últimos tres con el mismo error en el código, y no logro entender el motivo.
Rogaría no solo solución, sino una expicación de porque me ocurre. Y ya os digo, no me lipidéis, que no entiendo nada de Java y los inicios me están resultando abrumadores.
En éste otro código, tengo el mismo error:
Alguien me puede comentar algo al respecto ?
Muchas gracias
Para empezar os pido disculpas por éste nuevo mensaje. Estoy empezando en el mundo de Java, llevo algún que otro programilla, pero los últimos tres con el mismo error en el código, y no logro entender el motivo.
Rogaría no solo solución, sino una expicación de porque me ocurre. Y ya os digo, no me lipidéis, que no entiendo nada de Java y los inicios me están resultando abrumadores.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Importamos las clases UTIL para Scanner y MATH para PI y POW
import java.util.*;
import java.lang.math.*; //error: unused import
import java.text.*;
//Creamos la clase circunferencia con los atributos indicados
public class Circuenferencia {
double teclado;
double radio;
double diametro;
double area;
double longitud;
/**
* @param args the command line arguments
*/
//Creamos el método MAIN
public static void main(String[] args) {
// TODO code application logic here
//Pedimos por teclado el valor del radio
System.out.println("Introduzca el valor del radio de la circunferencia: ");
//creamos el objeto teclado de la clase Scanner que recibe el dato del usuario
Scanner teclado = new Scanner(System.in);
//cogemos el valor dado por teclado y lo redirigimos a la variable radio
radio = teclado.nextDouble(); //Error: non-static variable this cannot be referenced from a static context
//Calculamos el DIÄMETRO, y la LONGITUD de la circunferencia y el ÁREA del círculo
diametro = 2 * radio; //Error: non-static variable this cannot be referenced from a static context
longitud = 2 * math.PI * radio; //Error: non-static variable this cannot be referenced from a static context
area = math.PI * math.pow(radio,2); //Error: non-static variable this cannot be referenced from a static context
//Formateamos las variables para que muestre dos decimales
DecimalFormat formato = new DecimalFormat("#.00");
//Imprimimos en pantalla los datos de los cálculos anteriores
System.out.println("Para un radio de: " + formato.format(radio) + ", tenemos las siguientes operaciones y resultados:"); //Error: non-static variable this cannot be referenced from a static context
System.out.println("El diámetro de la circunferencia es de: " + formato.format(diametro)); //Error: non-static variable this cannot be referenced from a static context
System.out.println("La longitud de la circunferencia es de: " + formato.format(longitud)); //Error: non-static variable this cannot be referenced from a static context
System.out.println("El área del círculo comprendeido dentro de la circunferencia es de: " + formato.format(area)); //Error: non-static variable this cannot be referenced from a static context
}
En éste otro código, tengo el mismo error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//importamos la clase util para tener acceso a la clase Scanner
import java.util.*;
//Creamos la clase pública "TextoTeclado"
public class TextoTeclado {
//Tendrá los siguientes atributos
String textoT;
int vocales = 0;
int consonantes;
int espacios = 0;
int longitud;
String TextoT = new String();
/**
* @param args the command line arguments
*/
//Creamos el método MAIN
public static void main(String[] args) {
// TODO code application logic here
//Creamos el objeto "textoteclado" de la clase Scanner
Scanner textoteclado = new Scanner(System.in);
//Pedimos por consola la introdución de un texto
System.out.println("Por favor, si es tan amable, escriba un texto: ");
//Capturamos el texto indtroducido y lo reasignamos a la variable "textoT"
textoT = textoteclado.nextLine(); //Error: non-static variable this cannot be referenced from a static context
//Creamos un array de tipo char con el texto introducido
char[] textocaracter = textoT.toCharArray(); //Error: non-static variable this cannot be referenced from a static context
/* Averiguamos el número de caracteres y la clase de los mismos */
//Creamos la variable del bucle for
int i;
//Contamos el número de caracteres del texto introducido por pantalla y lo asiganmos a la variable longitud
longitud = textocaracter.length; //Error: non-static variable this cannot be referenced from a static context
//Creamos el bucle for
for(i=0;i<longitud;i++) //Error: non-static variable this cannot be referenced from a static context
{
//Hacemos un condicionante para comprobar si dentro del array de tipo char, hay caracteres que coincidan con lo indicados (a,e,i,o,u)
if(textocaracter[i]=='a' || textocaracter[i]=='e' || textocaracter[i]=='i' || textocaracter[i]=='o' || textocaracter[i]=='u')
{
//cada vez que encuentra una de las vocales le suma 1 a la variable vocales
vocales++; //Error: non-static variable this cannot be referenced from a static context
}
if (textocaracter[i]==' ')
{
//cada ve que encuentra une espacio en blanco, le suma 1 a la variable espacios
espacios++; //Error: non-static variable this cannot be referenced from a static context
}
}
//para obtener el número de consonantes, a la longitud total del texto introducido por pantalla, le restamos las vocales y los espacios
consonantes=longitud-vocales-espacios; //Error: non-static variable this cannot be referenced from a static context
//imprimimos por pantalla todos los datos obtenidos con las operaciones anteriores
System.out.println("El texto introducido es: " + textoT); //Error: non-static variable this cannot be referenced from a static context
System.out.println("Tiene " + longitud + " caracteres"); //Error: non-static variable this cannot be referenced from a static context
System.out.println("El numero de vocales son: " + vocales); //Error: non-static variable this cannot be referenced from a static context
System.out.println("El numero de consonantes son: " + consonantes); //Error: non-static variable this cannot be referenced from a static context
System.out.println("El numero de espacios son: " + espacios); //Error: non-static variable this cannot be referenced from a static context
}
}
Alguien me puede comentar algo al respecto ?
Muchas gracias
- Circuenferencia.rar(988,0 B)
- TextoTeclado.rar(1,2 KB)
Valora esta pregunta
0