Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I want to make a program where the user can enter numbers (any number-2,-3,-4,0,100;1000)
until he enters 0.
I have an error at the while loop,scanner is not compatible with int,how can i get rid of that? Something is missing and i dont know what.Help pls. (sry if i wrote stupid things im new )

my code so far:

C#
public static void main(String[] args) {

       // int[] list = new int[10];
        int i;

        System.out.println("Enter numbers :");
        Scanner input = new Scanner(System.in);

        for (i = 1; i < 100; i++) {
            while (input != 0){         // problem here , incompatible Scanner and int
             i = input.nextInt();
            }


        }

    }


XML
Ok. so i get what you said and ty for your help,but its not working with 0.only numbers more then 0.If i neter 0 i get





Exception in thread &quot;main&quot; java.util.regex.PatternSyntaxException: Unclosed character class near index 525
(([-+]?(((((?i)[]|\p{javaDigit})++)|([\p{javaDigit}&amp;&amp;[^0]]((?i)[]|\p{javaDigit})?((?i)[]|\p{javaDigit})?(\,((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit}))+)))))|(((((?i)[]|\p{javaDigit})++)|([\p{javaDigit}&amp;&amp;[^0]]((?i)[]|\p{javaDigit})?((?i)[]|\p{javaDigit})?(\,((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit}))+)))|(\Q-\E((((?i)[]|\p{javaDigit})++)|([\p{javaDigit}&amp;&amp;[^0]]((?i)[]|\p{javaDigit})?((?i)[]|\p{javaDigit})?(\,((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit}))+)))

like this:

&lt;pre lang=&quot;cs&quot;&gt;public static void main(String[] args) {


         int i;

            System.out.println(&amp;quot;Enter numbers :&amp;quot;);
            Scanner input = new Scanner(System.in);

            for (i = 1; i &amp;lt; 100; i++) {
                while (input.hasNextInt(8)){        //need 0 here
                 i = input.nextInt();

                //if(input.hasNextInt()){


                 }

                }



    }

}&lt;/pre&gt;</pre>
Posted
Updated 21-Mar-15 6:00am
v4
Comments
Afzaal Ahmad Zeeshan 21-Mar-15 10:13am    
It is always better to at least first know the language and the API you're going to use. See solution 1.

Java
while (input.hasNextInt()) {
   // ...
}


Maybe a look at Class Scanner[^] would give some useful examples of its usage.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 21-Mar-15 10:12am    
+5 for giving him the accurate answer. :)
phil.o 21-Mar-15 10:23am    
Thanks for the "virtual" five ^^
Afzaal Ahmad Zeeshan 21-Mar-15 10:26am    
I did receive 1 point for up-voting your answer. :laugh:
phil.o 21-Mar-15 10:29am    
No problem, really. What would be interesting to know is what the OP has managed to do with this answer.
Member 11524654 21-Mar-15 10:45am    
its not working with 0 as i posted below
import java.util.ArrayList;
import java.util.Scanner;

public class SuccesiuneDeNumerePanaLa0 {

public static void main(String[] args) {

int sum = 0;

ArrayList<integer> negative_list = new ArrayList<integer>();
ArrayList<integer> pozitive_list = new ArrayList<integer>();

System.out.println("Enter number to the list: ");

Scanner input = new Scanner(System.in);

while (input.hasNextInt()) {

int i = input.nextInt();
if (i < 0) {
negative_list.add(i);
}
if (i == 0) {
break;
}
if (i > 0) {
pozitive_list.add(i);

}

for (int j : pozitive_list) {
sum += j;
}

}

System.out.println(" " + sum);
System.out.println(" " + negative_list.size());

}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900