Hello everyone, right now I'm taking a java class, and I'm having one error in my app.

Right now I have to .java files, one which calls the methods from another .java file.

When I compile the first java file, it doesn't give me any error. But when compiling the 2nd one, Jgrasp gives me the following error.

UsaArreglos.java:80: existeValor(int[],int,boolean) in Arreglos cannot be applied to (int[])
				x2 = Arreglos.existeValor(a);
				             ^
1 error

The first java file (which stores the methods) is the following

import javax.swing.*;
public class Arreglos {
int valor;
	// Metodo 1
	public static void inicializa1(int a[]) {
		for (int i=0; i<a.length; i++) {
			a[i] = (int)(Math.random() * 999);	
		}
	}
	
	public static void inicializaTeclado (int a[]) {
		for (int i=0; i<a.length ; i++){
			a[i]=Integer.parseInt(JOptionPane.showInputDialog(null, "Teclea el valor de a["+i+"]","Leer Arreglo",-1));
		}
	}
	
	public static void inicializa2 (int a[]){
		int[] a0;
		int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Tecle los arreglos que quiere ver : ","Leer Arreglo",-1));
		a0 = new int [x];
		for (int i=0; i<=a0.length ; i++){
			a[i] = i;
		}
		String s;
		s = "";
		for (int i = 0; i<a0.length; i++)
		// Contatenar l contenido de a a s
		s = s + "a[" + i + "]=" + a[i]+"\n";
		//despliegar GUI
		JOptionPane.showMessageDialog (null,s,"Desplegar", -1);
	}
	
	public static void inicializa3 (int a[]){
		for (int i = 0; i<a.length; i++){
			int x0 = i * 2;
			a[i] = x0;
		}
	}
	
	public static void inicializa4 (int a[]){
		int[] a0;
		int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Tecle los arreglos que quiere ver : ","Leer Arreglo",-1));
		a0 = new int [x];
		for (int i=0; i<=a0.length ; i++){
		x = x - 1;
		a[i] = x;
	}
	String s;
		s = "";
		for (int i = 0; i<a0.length; i++)
		// Contatenar l contenido de a a s
		s = s + "a[" + i + "]=" + a[i]+"\n";
		//despliegar GUI
		JOptionPane.showMessageDialog (null,s,"Desplegar", -1);
	}
	
	public static void inicializa5 (int a[]){
		int x0 = 1;
		for (int i=0; i<a.length; i++){
		a[i] = x0;
		int x1 = x0 * 2;
		x0 = x1;
		}
	}
	
	//Metodo que encuentra l valor Maximo del arreglo
	// Inicializa el mayor integer.min_value
	public static int elMayor (int a[]){
		int mayor = Integer.MIN_VALUE;
		for (int i=0; i<a.length ; i++){
			if (mayor < a[i]) {
				mayor = a[i];
				}
		}
		//returnar el mayor
		return mayor;
	}
	
	
		public static int elMenor (int a[]){
		int menor = Integer.MAX_VALUE;
		for (int i=0; i<a.length ; i++){
			if (menor > a[i]) {
				menor = a[i];
				}
		}
		//returnar el menor
		return menor;
	}
	
	public static int laSuma (int a[]){
		int suma;
		suma = 0;
		for (int i =0; i<a.length ; i++){
			suma = suma + a[i];
		}
	return suma;
	}
	
	public static double promedio (int a[]){
		double suma;
		double promedio;
		suma =0;
		for (int i = 0; i<a.length ; i++){
			suma = suma + a[i];
		}
	promedio = suma/a.length;
	return promedio;
	}
	
	public static boolean existeValor (int a[], int valor, boolean v){
		v = false;
		int i = 0;
		JOptionPane.showInputDialog(null,valor,"Inserte un Valor",-1);
		while (v = false || a.length > i ){
			if (a[i]== valor){
				v = true;
			}
			else 
			i++;		
		}
		return v;
	}
	
	//Despliega el contenido
	public static void despliega(int a[]) {
		String s;
		s = "";
		for (int i = 0; i<a.length; i++)
		// Contatenar l contenido de a a s
		s = s + "a[" + i + "]=" + a[i]+"\n";
		//despliegar GUI
		JOptionPane.showMessageDialog (null,s,"Desplegar", -1);
		
}
}

and then I have the next java file, which calls the methods

import java.util.*;
import javax.swing.*;

public class UsaArreglos{
	public static void main (String [] args) {
		//Declaracion de variables
		String menu;
		int opcion;
		// Declaracion 
		int [] a0;
		int [] a;
		int x;
		int y;
		double x1;
		boolean x2;
		//Creacion
		a = new int [10];
		a0 = new int [0];
	
		menu = "0.terminar \n1.Inicializa1 \n2.inicializaTeclado \n3.inicializa2 \n4.inicializa3 \n5.inicializa4 \n6.inicializa5 \n7. elMayor \n8.ElMenor \n9.LaSuma \n10.Promedio \n11.ExisteValor \n12.BuscaValor \n13.cuentaRepeticiones \n14.muestraArreglo \n15.copiaArreglo \n16.copiaInversa \n17.iguales" + " \n \n \nTeclea la opcion: ";
	
		do {
			//Despliega el menu y leer la opcion deseada
			opcion = Integer.parseInt(JOptionPane.showInputDialog(null, menu, "Menu", -1));
			switch (opcion) {
				//public static void inicializa1(int a[])
				case 1 :
				Arreglos.inicializa1(a);
				Arreglos.despliega(a);
				break;
				
				case 2 :
				Arreglos.inicializaTeclado(a);
				Arreglos.despliega(a);
				break;
				
				case 3 : 
				Arreglos.inicializa2(a);
				break;
				
				case 4 : 
				Arreglos.inicializa3(a);
				Arreglos.despliega(a);
				break;
				
				case 5 : 
				Arreglos.inicializa4(a);
				break;
				
				case 6 :
				Arreglos.inicializa5(a);
				Arreglos.despliega(a);
				break;
				
				case 7 :
				x = Arreglos.elMayor(a);
				JOptionPane.showMessageDialog (null,"El mayor = "+x,"El Mayor", -1);
				break;
				
				case 8 :
				x = Arreglos.elMenor(a);
				JOptionPane.showMessageDialog (null,"El menor = "+x, "El Menor", -1);
				break;
				
				case 9 :
				x = Arreglos.laSuma(a);
				JOptionPane.showMessageDialog (null,"La suma del arreglo es "+x,"La suma", -1);
				break;
				
				case 10 :
				x1 = Arreglos.promedio(a);
				JOptionPane.showMessageDialog (null,"El promedio del arreglo es "+x1,"Promedio", -1);
				break;
				
				case 11 :
				x2 = Arreglos.existeValor(a);
				if (x2 = false) {
					JOptionPane.showInputDialog(null,"El valor no existe","Valor no Existe",-1);
				}
				else
				JOptionPane.showInputDialog(null,"El valor si existe","Valor si Existe",-1);
				break;

				
				
				case 0 :
				JOptionPane.showMessageDialog(null, "Gracias por usar el software", "Termino",-1);
				break;
			}
		}
	while (opcion !=0);
	}
}

Edited 7 Years Ago by fortex: n/a

Comments
Yep. Code tags deserve rep.
Code tags on first post!!! Thank you :)

compare

case 11:
    x2 = Arreglos.existeValor(a);

with

public static boolean existeValor(int a[], int valor, boolean v) {

existeValor(a) --->missed valor and v parameters in invocation
existeValor(int a[], int valor, boolean v)

compare

case 11:
    x2 = Arreglos.existeValor(a);

with

public static boolean existeValor(int a[], int valor, boolean v) {

existeValor(a) --->missed valor and v parameters in invocation
existeValor(int a[], int valor, boolean v)

So I have to change

case 11:
    x2 = Arreglos.existeValor(a);

with

case 11:
    x2 = Arreglos.existeValor(a, valor, v);

?

The point is, the existeValor method takes three arguments, but you were only passing in one argument. That will only result in a compiler error - the two are treated as completely different methods. So yes - you are on the right track.

Edited 7 Years Ago by BestJewSinceJC: n/a

This article has been dead for over six months. Start a new discussion instead.