Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

Array accept only positive values

array

  • Please log in to reply
8 replies to this topic

#1 ahwoo

ahwoo

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:Java
  • Learning:Java

Posted 10 October 2012 - 12:44 AM


import java.util.Scanner;
public class test {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter numbers: ");
int num = scan.nextInt();
int numbers = 5;
int array[] = new int [numbers];
int numCount = 0;

while (numCount < numbers) {
array[numCount++]=scan.nextInt();

}

for(int i:array){
System.out.println(i);

}
}
}

I'm trying to do two things, make it accept only positive numbers from user,
and also to terminate if the user enters 0

And also when I enter like the numbers 1,2,3,4,5,6

in a row it omits the first 1 I enter, and only stores the number 2,3,4,5,6

Any help would be appreciated
  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 10 October 2012 - 01:13 AM

You need to check the entered number if it's negative, then don't accept it and let the user add a new number. if it's 0, terminate.

is it limited to 5 numbers, or is it just your test?
  • 1

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 ahwoo

ahwoo

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:Java
  • Learning:Java

Posted 10 October 2012 - 01:22 AM

You need to check the entered number if it's negative, then don't accept it and let the user add a new number. if it's 0, terminate.

is it limited to 5 numbers, or is it just your test?


Yep limited to 5 numbers,
and for the check do I put it after the first while loop or before? and then nest the while loop,
yeah im also not sure where to add the check if num = 0, at the beginning of the loop
something like
if (num < 0)
  • 0

#4 lintwurm

lintwurm

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 261 posts
  • Programming Language:C, Java, C++, C#, PHP, (Visual) Basic, Python, Visual Basic .NET

Posted 10 October 2012 - 01:22 AM

I suspect your problem lies in this line:


array[numCount++]=scan.nextInt();

the reason for me saying this is: numCount starts at 0. You get input from the user, numCount now equals 1. you assign the input from the user to the array at pos: 1 and not 0...

rather split everything up.
It is more to type, but you can follow the logic more easily.
By splitting it up you can also check if the number is positive as well.


while(numCount <numbers)
{
int input = scan.nextInt();
if( input > 0 )
array[numCount]= input;
else
break;
numCount++;
}

If the code is wrong, i apologize as I am not currently able to compile and check right now :D
Hope that helped.

Fixed the color **...
  • 1
No harm was done to the world in sending this message, but a few million electrons have been SERIOUSLY inconvenienced...

#5 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 10 October 2012 - 01:29 AM

I suspect your problem lies in this line:


array[numCount++]=scan.nextInt();

That line is actually fine, numCount will be 0 when it's used to store stuff in the Array, and after it is used it'll be 1, ready for the next loop.

The reason the first digit isn't stored is because you read the number before the loop and don't do anything with it.
int num = scan.nextInt();
//num is never used

  • 1

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 10 October 2012 - 01:38 AM

you check inside the loop. A better way for this is to do the whilecheck to check if the number is 0 and if the entered numbers is 5, and before the while and in the end of each loop ask for the number

by the way, you need to check that there is an int entered as well, otherwise your program will crash. (tips, scan.hasNextInt() )
  • 1

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#7 ahwoo

ahwoo

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:Java
  • Learning:Java

Posted 10 October 2012 - 01:45 AM

Thanks for the help so far!!!
I'm trying to add the termination to the loop
inside the while loop
however im getting this error:

else if (num = 0) {
break;
}
Type mismatch: cannot convert from int to boolean

yeah trying to add that but it won't let me
  • 0

#8 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 10 October 2012 - 01:56 AM

You're just assigning zero to 'num' there, checking if it's equal is with double '='

if(num == 0)

  • 1

#9 ahwoo

ahwoo

    CC Newcomer

  • Member
  • PipPip
  • 16 posts
  • Programming Language:Java
  • Learning:Java

Posted 10 October 2012 - 02:32 AM

Thanks for the help guys,
stuck on the next part of the question which requires me to
reverse and output the int array
got this
for (int i = numCount - 1; i >= 0; i--)
System.out.println(i)

However when I terminate it doesn't reverse it properly

EDIT: Solved
  • 0





Also tagged with one or more of these keywords: array

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download