Results 1 to 16 of 16
Thread: What code is this?
- 01-23-2011, 01:46 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
What code is this?
Hello I had a problem about this program that my stupid Teacher give me. The statement says that it includes looping. The program output is this :
============
Enter a value: 2
Enter a value: 1
Enter a value: 0
The sum is 3
============
When the input is 0, it will add the non-zero digits, and if the value is not a zero digit, it will repeat the statement to input another value...This program includes LOOPING statement. like "Enter a value" will repeat if the input value is not a 0....I wish someone will post a code for this program.....BTW, the program class is not a BUFFEREDREADER or JOPTIONPANE. The program class is SCANNER....Pls. someone help me....this program is really important, otherwise Im going to fail my subject in JAVA programming.....:confused:
- 01-23-2011, 02:04 PM #2
- 01-23-2011, 02:42 PM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
make use of ArrayList
ArrayList a = new ArrayList();
In your for loop use this line to add elements to array
a.add(i);
wrap the above line in an if condition to check for zero.
if zero, exit and sum the elements of array.
to get elements of array use
a.get(i)
For more info
ArrayList (Java 2 Platform SE v1.4.2)
- 01-23-2011, 10:17 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Perfect situation for a do while loop. Just figure out what condition you want to end the loop at and the problem nearly solves itself.
- 01-23-2011, 11:19 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
I just don't know how to start this program. can anyone show the code for this program....I really need it....FOR LOOPING is the method for this program....
- 01-23-2011, 11:25 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
We try not to give you the answer too directly, and instead push you in the correct direction, the syntax for a do while loop is this:
What this means is, first it will do whatever is in the first code block(where it says //do something) This will always execute once, the next thing it does is check the condition, if the condition returns true, it moves back up to the do and repeats the first part of the loop. This continues until the condition is not met.Java Code:do{ //do something } while(condition);
Either give it a shot, program the best you can and post what you have with code tags here, or explain to me step by step what you want this code to do and what methods to use, I will tell you if you are heading in the correct direction or not. Good luck!
- 01-23-2011, 11:33 PM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Of course you will fail a Java course if you cannot write such a simple program. Have you read any of your text? Have you been awake in class? Have you taken any notes? Have you made any effort at all toward writing this program? Do you really think it would be a good thing if someone wrote this program for you?
-Gary-
- 01-23-2011, 11:41 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
if you cant do this, you are gonna have problems with if then...you should think about if you really want to do anything with programming or if it would be better to drop the class...
- 01-23-2011, 11:46 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
While coding can be challenging when you first start this is really an extremely simple exercise if you take a minute and sketch out what you want to do.
You can even do the sketching here and we will help you, simple state in english, what should happen at each step.
Some questions to consider:
Is the input in a position where you want to end the program?
what variables should be declared?
what happens when the condition is met?
If the condition isn't met, what should be done?
What should the condition be?
Answering these questions should help, like I said, this program may be a lot simpler then you think, especially if you work out what to do in plain english first.
- 01-24-2011, 09:56 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
import javax.swing.*;
public class ZeroAdd
{
public static void main (String [] acrs)
{
String x1 = "";
String x2 = "";
String x3 = "";
int num1;
int num2;
int num3;
int sum;
sum = num1 + num2 + num3;
x1 = JOptionPane.showInputDialog(null, "Enter Integer: ");
num1 = Integer.parseInt(x1);
x2 = JOptionPane.showInputDialog(null, "Enter Integer: ");
num2 = Integer.parseInt(x2);
x3 = JOptionPane.showInputDialog(null, "Enter Integer: ");
num3 = Intger.parseInt(x3);
for (num1 = 0;x1 <= 10; x1++) {
for (num22 = 0;x2 <= 25; x2++) {
for (num3 = 0; x3 == 0; x3++) {
if (num3 == 0) {
JOptionPane.showMessageDialog(null, sum);
}
else if (num2 == 0) {
JOptionPane.showMessageDialog(null, sum);
}
else if (num1 == 0) {
JOptionPane.showMessageDialog(null, sum);
}
else {
JOptionPane.showMessageDialog(null, "Invalid Input.");
}
}
}
}
}
}
------------------------------------------------
Am I correct?
- 01-24-2011, 09:58 AM #11
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
if you input 0.....The ff inputs of non-zero digit will be add....and the program will stop repeating the question....
- 01-24-2011, 10:04 AM #12
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Here's some pseudocode:
Java Code:while input is not 0 add input to total display total
- 01-24-2011, 10:11 AM #13
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
No.
1. The exercise calls for a console program, not a Swing GUI program.
2. The exercise calls for you to loop an indeterminate number of times, prompting for and reading an int each time, and adding it to a total, until the user enters a 0, at which time you should report the total. You need a total of two variables to accomplish this: one to get a value from the user (you can keep re-using this variable, because once you've added the int to the total you don't need it anymore), and one to store the total. You declare a bunch of different variables, confuse their names quite a bit (num1, num2, num3, x1, x2, num22, sum...), and the only time you do any addition is before any of your variables is initialized.
3. You have a nested for loop that runs 250 times and never does anything useful.
I'm sorry to seem cruel, but seriously, why are you taking this course? It really seems like you'd be happier doing something else.
-Gary-
- 01-24-2011, 10:13 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I don't know if you need to use swing, but if you are unfamiliar with how to do it normally, don't complicate it with swing just yet, first figure out the main way to do it.
I like that you wrote the condition, the only things you need for this program are the following:
Scanner
two int variables
do..while loop
Try creating it with only those things, don't be afraid to view the apis for them, You don't need any if else clauses either
also, you got the condition right, now translate it into java for me.
- 01-24-2011, 12:17 PM #15
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
its not that im failing to this course. I don't find difficult in during QUIZZES & EXAMINATIONS....I just don't own a JAVA IDE on my computer, And the worst is I always depending on BOOKS.
- 01-24-2011, 12:57 PM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
It's probably better to learn in something simpler than an ide, if you really want one however, eclipse and the sun one are free I believe, but sticking with notepad++ or any other text editor and cmd is good for learning.
The problem is your logic doesn't seem there. Have you made changes and removed the swing functionality? If so, please post some more code.
Similar Threads
-
Code to check if a piece of code is legal.
By vahshir in forum New To JavaReplies: 3Last Post: 08-30-2010, 04:21 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks