DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2005
    Posts
    2

    How to create a multiplier using the add function

    I am creating a program that will allow the user to input two numbers. What I want to do is multiply the two numbers together however I want to use the add function. for example if they input 5 and 3 i want the program to take 5+5+5=15 instead of taking 5*3. This is what I came up with but I know its wrong. Can anyone get me on the right track?

    String firstStr=JOptionPane.showInputDialog("Enter an Integer");
    int x=Integer.parseInt(firstStr);
    String secondStr=JOptionPane.showInputDialog("Enter a second Integer");
    int y=Integer.parseInt(secondStr);

    int number=1;
    for (number=1;number>y;number++){x=x+x;}
    System.out.println(x);

  2. #2
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    x = 5
    y = 3

    your loop will produce this : ( look at the note )
    step 1 : x=x+x ---> x = 5 + 5 -> x = 10 ( you lost original x)
    step 2 : x=x+x ---> x = 10 + 10 -> x = 20
    step 3 : x=x+x ---> x = 20 + 20 -> x = 40

    use another variable to hold the first value of x
    or use another variable to hold the sum.

    note : number > y or number <= y ?
    Last edited by mr1yh1; 11-01-2005 at 06:33 AM.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Extended version:

    Code:
    public class CountAdder {
      public CountAdder() {
      }
      /**
       * Get user integer input, don't stop until it's correct
       * @param prompt
       * @return
       */
      private int getUserInt(String prompt) {
        while (true) {
          String s = JOptionPane.showInputDialog(prompt);
          try {
            return Integer.parseInt(s);
          }
          catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, s + " is not an integer",
                                          "Input error", JOptionPane.ERROR_MESSAGE);
          }
        }
      }
      public void doMultiplication() {
        int x=getUserInt("Enter an Integer");
        int y=getUserInt("Enter a second Integer");
    
        int total=0;
        for (int i=0; i<y; i++) {
          total += x;
        }
        System.out.println(total);
      }
      /**
       * Make an instance of class, then invoke the instance method(s).
       * @param args
       */
      public static void main(String[] args) {
        CountAdder cA = new CountAdder();
        cA.doMultiplication();
      }
    
    }
    eschew obfuscation

Similar Threads

  1. call function for button
    By angela_quests in forum VB Classic
    Replies: 2
    Last Post: 04-13-2007, 04:57 AM
  2. Getting a GUI to run
    By Eric in forum Java
    Replies: 4
    Last Post: 04-14-2006, 09:09 AM
  3. Getting a GUI to function
    By Eric in forum Java
    Replies: 1
    Last Post: 11-27-2001, 06:53 AM
  4. Cannot execute Add function of ActiveX server object
    By Robert Turnbull in forum VB Classic
    Replies: 0
    Last Post: 07-04-2000, 08:53 PM
  5. Please help me -- urgent -- deadlock error
    By chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 07:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links