Results 1 to 16 of 16
  1. #1
    Geoofrey is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Question 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:

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi, Can you show your code?
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    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)

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  5. #5
    Geoofrey is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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....

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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:
    Java Code:
    do{
     //do something
    } while(condition);
    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.

    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!

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  8. #8
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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...

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  10. #10
    Geoofrey is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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?

  11. #11
    Geoofrey is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    if you input 0.....The ff inputs of non-zero digit will be add....and the program will stop repeating the question....

  12. #12
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    Here's some pseudocode:

    Java Code:
    while input is not 0
        add input to total
    display total

  13. #13
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Geoofrey View Post
    Am I correct?
    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-

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  15. #15
    Geoofrey is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  1. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 05:21 AM
  2. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •