Search in array Learn programming Java

Lesson:

Arrays, Structures and Strings


Exercise:

Search in array 15


Objetive:

Create a java program that says if a data belongs in a list that was previously created.

The steps to take are:
- Ask the user how many data will he enter.
- Reserve space for that amount of numbers (floating point).
- Request the data to the user
- Later, repeat:
* Ask the user for a number (execution ends when he enters "end" instead of a number).
* Say if that number is listed or not.

Must be done in pairs. but you must provide a single source file, containing the names of both programmers in a comment.


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		System.out.print("Cuantos datos reservo: ");
		int repeticiones = Integer.parseInt(new Scanner(System.in).nextLine());
		float numero;
		float[] listaNumeros = new float[repeticiones];

		for (int i = 1; i <= repeticiones; i++)
		{
			System.out.printf("Dime numero " + i + "para guardar en la lista: ");
			listaNumeros[i] = Float.parseFloat(new Scanner(System.in).nextLine());
		}

		System.out.print("Que numero comprueba en la lista? ");
		numero = Float.parseFloat(new Scanner(System.in).nextLine());

		while (!new Scanner(System.in).nextLine().equals("end"))
		{
			for (int i = 1; i <= repeticiones; i++)
			{
				if (listaNumeros[i] == numero)
				{
					System.out.printf("El número " + numero + "existe en la lista" + "\r\n");
				}
			}
		}
	}
}