Hello guys im having little problem here,
Firstly i want to parse string which comes from scanner into int, so i can later use it IF statement.
Code looks like this
List<String> list = new ArrayList<String>();
Scanner numbers = new Scanner(System.in);
do {
System.out.println("Current list is " + list);
System.out.println("Add more? (y/n)");
if (numbers.next().startsWith("y"))
{
if(Integer.parseInt(numbers)> 0 && Integer.parseInt(numbers) < 101)
{// i get error on above line, and im kinda lost here
System.out.println("Enter : ");
list.add(numbers.next());
}
}
else
{
break;
}
}while (true);
Any help appreciated, hopefully i made my self clear and my problem too
Edit:
do {
System.out.println("Current list is " + list);
System.out.println("Add more? (y/n)");
if (numbers.next().startsWith("y"))
{
System.out.print("Enter: ");
System.out.flush();
final int n = Integer.parseInt(numbers.next());
if (n > 0 && n < 101)
{
list.add(n);
// this is where i get the error.
//no suitable method found for add(int)
// method java.util.List.add(int,java.lang.String) is not applicable
// (actual and formal arguments lists differ in length)
// method java.util.List.add(java.lang.String) is not applicable
// (actual argument int cannot be converted to java.lang.String
// by method invocation conversion).
// end of log
}
else
{
System.out.println("Number you have entered exceeds raange between 0-100");
}
}
else
{
break;
}
}while (true);
I presume you wanted to write
java.util.Scanneralso contains the methodnextInt, which obviates the need forInteger.parseInt. You can just saynumbers.nextInt().You also need to change your list declaration from
List<String>toList<Integer>.