java - How to prevent duplicate user entries into an array? -
this question has answer here:
- check value in array 4 answers
the point ask user input name. then, another. , store in array. then, if user enters duplicate name, should print line saying, please enter name. here code(so far...also please point out syntax errors..much appreciated...also no hashsets or linkedhashsets because haven't covered in class yet):
import java.util.scanner import java.util.arrays import java.util.arraylist class noduplicates { static final int max = 5; public static void main(string[] args); scanner input = new scanner(system.in); int num = 0; float total = 0; float[] array = new float [max]; string[] name = new string [max]; // read , store marks, (only store) while (num < max) { system.out.println("enter name"); name[num] = input.nextline(); system.out.println("enter name"); name[num] = input.nextline(); arrylist.contains("name");
either loop through array before adding name, or use arraylist
, check if name exists using method arraylist.contains("name");
.
also may use linkedhashset
in answer here.
Comments
Post a Comment