Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Positive and Negative integer counter

Thread view: 
StaR.BoY2005@gmail.com - 29 Sep 2006 05:10 GMT
Hello everyone,

I am working on a program that needs to read an unspecified number of
integers and determine how many positive and negative values there are.
Also, the program needs to end when the input is 0 (zero). Here is the
code I have which is causing me problems!!! I would greatly appreciate
all of the help!

import java.util.Scanner;
public class Counter
{

       public static void main(String[] args)
       {
                 int inputNumber;
                 int count;
                 int number;

                 Scanner keyboard = new Scanner(System.in);
                 count = 0;
                 number = 0;

                 System.out.print("Enter your first integer: ");
         inputNumber = keyboard.nextInt();

         while (inputNumber >0);
         {
                 count++;
                 System.out.print("Enter your next integer, or 0 to
end: ");
                 inputNumber = keyboard.nextInt();
         }

         while (inputNumber <0);
         {
                 number++;
                 System.out.print("Enter your integer, or 0 to end:");
                 inputNumber = keyboard.nextInt();
         }

         if (count == 0)
         {
             System.out.print("You didn't enter any data!");
          }
          else
          {
             keyboard.nextInt();
             System.out.print("You entered " + count + " positive
integers.");
             System.out.print("You entered " + number + " negative
integers.");
          }

       }

}

Thanks again.

--John
No Name - 29 Sep 2006 06:34 GMT
> I am working on a program that needs to read an unspecified number of
> integers and determine how many positive and negative values there are.
> Also, the program needs to end when the input is 0 (zero). Here is the
> code I have which is causing me problems!!! I would greatly appreciate
> all of the help!

Take your homework, shove it and piss off.
Tobias Schröer - 29 Sep 2006 08:16 GMT
Hi,

as this seems to be homework, I won't give you the answer, jsut a few hints.

StaR.BoY2005@gmail.com schrieb:
> Hello everyone,
>
[quoted text clipped - 14 lines]
>                   System.out.print("Enter your first integer: ");
>           inputNumber = keyboard.nextInt();

Look at the next steps from here: what does the used statements do?
Remember the specifications: read an int _until_ it is zero, else
increase either the positive or the negative integer counter.

>           while (inputNumber >0);
>           {
[quoted text clipped - 10 lines]
>                   inputNumber = keyboard.nextInt();
>           }

Now the result output...

>           if (count == 0)
>           {
[quoted text clipped - 3 lines]
>            {
>               keyboard.nextInt();
                ^^^^^^^^^^^^^^^^^^^
                why this?

>               System.out.print("You entered " + count + " positive
> integers.");
[quoted text clipped - 9 lines]
>
> --John

 Tobi


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.