473,473 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Can You Please Help Me with this "while loop program"?

aprilmae36
2 New Member
Nice day to all... I'm new in java programming and I'm having some problem with this program. The problem is this: Write a program that will read an unspecified number of non-zero integers and determine how many positive and negative values have been read. The program will abort when the user input a zero.

I've already made a program but I still can't get it right. I deeply appreciate if you will help me.

Here's the program I made:


import javax.swing.JOptionPane;

public class counter

{

public static void main(String [] args)

{

String num;

int count;

int number;

count = 0;

number = 0;

num = JOptionPane.showInputDialog(null, "Enter your first integer:");

int inputNumber = Integer.parseInt(num);

while (inputNumber!=0)

{

if (inputNumber > 0)

count ++;

else

number ++;

num = JOptionPane.showInputDialog(null, "Enter another integer, or enter 0 to end:");

}

if (count == 0)

{

JOptionPane.showMessageDialog(null, "Program aborting.");

}

else

{

JOptionPane.showMessageDialog(null, "There are " + count + " positive integers." );

JOptionPane.showMessageDialog(null, "There are " + number + " negative integers." );

}

}

}
Nov 7 '06 #1
1 2006
r035198x
13,262 MVP
Nice day to all... I'm new in java programming and I'm having some problem with this program. The problem is this: Write a program that will read an unspecified number of non-zero integers and determine how many positive and negative values have been read. The program will abort when the user input a zero.

I've already made a program but I still can't get it right. I deeply appreciate if you will help me.

Here's the program I made:


import javax.swing.JOptionPane;

public class counter

{

public static void main(String [] args)

{

String num;

int count;

int number;

count = 0;

number = 0;

num = JOptionPane.showInputDialog(null, "Enter your first integer:");

int inputNumber = Integer.parseInt(num);

while (inputNumber!=0)

{

if (inputNumber > 0)

count ++;

else

number ++;

num = JOptionPane.showInputDialog(null, "Enter another integer, or enter 0 to end:");

}

if (count == 0)

{

JOptionPane.showMessageDialog(null, "Program aborting.");

}

else

{

JOptionPane.showMessageDialog(null, "There are " + count + " positive integers." );

JOptionPane.showMessageDialog(null, "There are " + number + " negative integers." );

}

}

}

Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.JOptionPane; 
  2. public class counter
  3. {
  4. public static void main(String [] args)
  5. {
  6.  
  7. int negative = 0;
  8. int positive = 0;
  9. String num = JOptionPane.showInputDialog(null, "Enter your first integer:");
  10. int inputNumber = Integer.parseInt(num);
  11. while (inputNumber != 0)
  12. {
  13. if (inputNumber > 0)
  14. positive ++;
  15. else
  16. negative ++;
  17. num = JOptionPane.showInputDialog(null, "Enter another integer, or enter 0 to end:");
  18.  inputNumber = Integer.parseInt(num);
  19. }
  20. JOptionPane.showMessageDialog(null, "Program finished.\n" +
  21. "There are " + positive + " positive integers.\n"+
  22. "There are " + negative + " negative integers.");
  23.  
  24.  
  25.  
  26. }
  27. }
  28.  
  29.  
Like this?
Nov 7 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mark Creelman | last post by:
Hello all: I am attempting to use a database program that I downloaded from http://www.thescripts.com/serversidescripting/perl/tutorials/asimpledatabaseprogram/database.txt with good...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
1
by: Chris | last post by:
I built small C# Web and Web Service applications in a training class last week. The applications worked in the class, but when I tried to run them again over the weekend, they both bombed....
0
by: LBeveridge | last post by:
Hi, This morning I installed Visual Studio 2005 side-by-side 2003. The first problem I've noted is that projects in 2003 that have "Start External Program" set don't do that. I'm creating a...
2
by: holysmokes99 | last post by:
I am developing a component in .Net 1.1, and want to debug it using the "start external program" of the debugger in the IDE. The program I want to start references both 1.1 and 2.0 components. The...
1
aprilmae36
by: aprilmae36 | last post by:
Nice day to all... Here I am again, having another problem with loops... lol... Can you please help me make a while loop program that the output will be like this: 1 121 12321 1234321...
10
by: wd.jonsson | last post by:
Hmm, my while loop with "or" doesn't seem to work as I want it to... How do I tell the while loop to only accept "Y" or "y" or "N" or "n" input from the str(raw_input)? Thank's in advance! ...
1
by: DougFAI | last post by:
Hello there. Today, I was developing a college work software and then I saw thoe 2 types of while. My teacher asked me how does the "while(ifs)" (considering my ifstream was ifs) (that was the one...
1
by: Kerem Gümrükcü | last post by:
Hi All, i am too tired now to find this out by my own, but why does my while loop "while(readline==Console.ReadLine())" needs twice return hit to read the line, after setting...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.