This program should use only arraylists.A loop should be used that continues to ask for the userinput consisting of whole numbers.Every userinput should be added to the arraylist.The loop should execute 10 times but if the user wants to exit early, the letter x is entered.
Initially, I confused as to how two different variable types can be used within the same array.However,as arraylists are being used,this is possible.The below pasted code is what came up with.I thought I could use a boolean to check the arraylist for the letter x. However,it shows an exception if x is entered.Any help would be appreciated.
import java.util.*;
public class ArrayList {
public static void main(String[]args)
{
Scanner a=new Scanner(System.in);
int counter=0;
ArrayList myList=new ArrayList();
for(int i=0;i<10;i++)
{
System.out.print("Enter whole number.Enter x to quit: ");
int userinput=a.nextInt();
myList.add(userinput);
boolean element=myList.contains("q");
if(element=true)
{
System.out.println("Doesn't contain q.");
}
else
{
System.out.println("Contains q.");
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire