SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2001
    Location
    Down South
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help adding Intergers in array(s)

    I'm having issues adding the numbers in the array together. I know I need a loop to start at position 19 and move downward...but I'm at a loss...

    any help would be greatly appreciated...I'm pulling my hair out.


    import
    java.util.Scanner;

    public
    class LargeNums {



    publicstaticfinalintMAXSIZE = 20;
    publicstaticvoid main(String[] args) {


    Scanner keyboard = new Scanner(System.in);
    char x ,y;
    int c1 = MAXSIZE - 1;
    String input1, input2;


    int [] num1 = newint[MAXSIZE];
    int [] num2 = newint[MAXSIZE];
    int [] num3 = newint[MAXSIZE];


    System.out.println("Please enter two positive integers with less than " + MAXSIZE + " digits" );
    System.out.print("Please enter the first integer: ");
    input1 = keyboard.nextLine();
    System.out.print("Please enter the second integer: ");
    input2 = keyboard.nextLine();




    for (int i= input1.length() -1; i>= 0; i--){


    x = input1.charAt(i);


    if(Character.isDigit(x)){


    num1[c1--] = x-48;
    }
    }
    for (int i= input1.length() -1; i>= 0; i--){


    y = input1.charAt(i);


    if(Character.isDigit(y)){


    num2[c1--] = y-48;


    }
    }
    for (int z = 0; z<num1.length; z++){
    System.out.print(num1[z]);
    }






    }


    }
    http://newgrounds.com/portal/content.php?id=31235#

    I cry, and I would give my life, and your death is not invane, this I promise

  2. #2
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you convert your string input to int? Integer.parseInt() ..

    If that does not work then try to debug it a step at a time by commenting out your code. Good Luck.

  3. #3
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well you have 2 options here, you can either use Integer.parseInt() ... and just add the numbers, much easier. If you're required to do it using arrays (I'm assuming this is homework) then you need to think about the problem a bit.

    You've managed to store the number in an array but they're all just digits 0-9. You've already got the loop part more or less correct, you just need to have an accumulator of some sort to get the actual value before adding them. This really becomes a matter of math, maybe work it out on paper first.


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
  •