Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default [SOLVED] another problem

    hi again
    im trying to work again another program,
    first it will display the value i entered on the array backward,
    and i display it well.
    my problem here is when adding the values of the 2 integer,
    the sum is always 0;
    anyone can help me fix this,
    here is my code:
    Java Code:
    public class Num7
    {
    	public static void main(String[]args)
    	{
    	char[] ch={'1','2','3','4','5','6','7','8','9'};
    	int x,y,z;	
    	for(x=ch.length;x>0;x--)
    	{
    	System.out.print(x);
    	}
    	System.out.println();
    	for(y=ch.length;y>0;y--)
    	{
    	System.out.print(y);
    	}
    	z=x+y;
    	System.out.print("The sum is: "+z);
    	}
    }
    z is always 0;
    i cant figure whats the problem here,
    pls help
    Last edited by beginner21; 12-01-2008 at 08:18 AM. Reason: code tags

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    x and y are 0, so x+y is zero.

  3. #3
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    but x and y is declared as the value of ch.length;
    here is the output:
    Java Code:
    987654321                     //probably x
    987654321                     //probably y
    The sum is: 0
    pls help..
    Last edited by beginner21; 12-01-2008 at 08:44 AM. Reason: code tag

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    They start out with the value 9 (which is the length of the character array, which is how you define them) and then here
    Java Code:
    for(x=ch.length;x>0;x--)
    you "count down" until they are zero. What exactly is it that you want to acheive?

    And those "numbers" you've shown are not the values of x and y, they are simply a printing of the char arrays values.

  5. #5
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    alright..
    i get it..
    so what should i place instead of
    Java Code:
    for(x=ch.length;x>0;x--)
    in order to print the array values backward.
    Last edited by beginner21; 12-01-2008 at 08:55 AM. Reason: tag

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You have printed the array values backwards. What else do you wish to acheive? What value are you expecting z to have (or what value do you want it to have)?

  7. #7
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    in order for the z to have values,
    i should have change the declaration of x and y in the for loop,
    my question is,
    is there any technique that i can use to print the array values backward instead of this code
    Java Code:
    for(x=ch.length;x>0;x--)

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    What values should "z" have? I still don't know what it is you want to achieve (in realtion to "z").

    Why don't you provide us with a description of what you expect?

  9. #9
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    ok i got your point,
    here is the problem:
    1. the digit should store backwards//like what i do
    2. the digits should be read into a partially filled array, and you might find it useful to reverse the keyboard.(Whether or not you reverse the order of the elements is up to you. It can be done either way,and each way has its advantages and disadvantages)//i don't understand it clear
    3. Perform the addition.
    4. The result of the addition should be stored in array of size 20//im not performing this as of now because z is always 0
    here it is..

  10. #10
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    alright..
    here is my modified code:
    Java Code:
    import java.io.*;
    public class Num7
    {
    	public static void main(String[]args)throws IOException
    	{
    	BufferedReader en=new BufferedReader(new InputStreamReader(System.in));
    	char[] ch={'1','2','3','4','5','6','7','8','9'};
    	int x,y,z;
    	for(x=ch.length;x>0;x--)
    	{
    	}
    	System.out.println();
    	for(y=ch.length;y>0;y--)
    	{
    	}
    	System.out.println("Enter first Integer: ");
    	x=Integer.parseInt(en.readLine());
    	System.out.println("Enter second Integer: ");
    	y=Integer.parseInt(en.readLine());
    	z=x+y;
    	System.out.println("\nThe sum is: "+z);
    	if(z>ch.length)
    	{
    	System.out.println("INTEGER OVERFLOW");
    	}
    	else
    	{
    	}
    	}
    }
    the output of this is:
    Java Code:
    Enter first Integer:
    5325256
    Enter second Integer:
    43252365
    
    The sum is: 48577621
    INTEGER OVERFLOW
    all i want here is the word INTEGER OVERFLOW
    it only prints when the sum is more than the maximum number of digits(more than 8 digits)..
    but on my program..it always print..
    what should i modify or what was the error?

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I'm also not sure what it is you are expected to do (I can only assume you've had more context in your class). All I can do is show you something like this

    Java Code:
    int[] digits = { 1, 2, 3 };
    int[] added = new int[digits.length];
    for (int i = digits.length - 1, j = 0; i >= 0 && j < digits.length; i--, j++) {
        added[j] = digits[j] + digit[i];
    }
    Probably not anywhere close to what you actually need, but without the context that the class provided, I am nowhere close to knowing what you actually need.

    This just a little example that loops forwards, and backwards, through the same array adding the values at the referenced contexts and inserting them into another array.

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by beginner21 View Post
    alright..
    here is my modified code:
    Java Code:
    import java.io.*;
    public class Num7
    {
    	public static void main(String[]args)throws IOException
    	{
    	BufferedReader en=new BufferedReader(new InputStreamReader(System.in));
    	char[] ch={'1','2','3','4','5','6','7','8','9'};
    	int x,y,z;
    	for(x=ch.length;x>0;x--)
    	{
    	}
    	System.out.println();
    	for(y=ch.length;y>0;y--)
    	{
    	}
    	System.out.println("Enter first Integer: ");
    	x=Integer.parseInt(en.readLine());
    	System.out.println("Enter second Integer: ");
    	y=Integer.parseInt(en.readLine());
    	z=x+y;
    	System.out.println("\nThe sum is: "+z);
    	if(z>ch.length)
    	{
    	System.out.println("INTEGER OVERFLOW");
    	}
    	else
    	{
    	}
    	}
    }
    the output of this is:
    Java Code:
    Enter first Integer:
    5325256
    Enter second Integer:
    43252365
    
    The sum is: 48577621
    INTEGER OVERFLOW
    all i want here is the word INTEGER OVERFLOW
    it only prints when the sum is more than the maximum number of digits(more than 8 digits)..
    but on my program..it always print..
    what should i modify or what was the error?
    Then seemingly you have it (Other than the fact that the loops are useless). Was there something else you needed?

    Edit:
    Should it print, maybe, when the text length of the sum is longer than 8 (rather than when the sum is greater than 8)? I can guarantee that if you enter 1 and 2 after the questions that that message won't show.

    If it is to print when the text length of the number is greater than eight, than look at the API docs for String and it's valueOf and length methods.
    Last edited by masijade; 12-01-2008 at 10:40 AM.

  13. #13
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    Lightbulb

    Quote Originally Posted by beginner21 View Post
    to print the array values backward
    I noticed you're not actually accessing the values in the array ch that you created, but instead are just printing out the length of the array decrementing each time by one.

    In this case it would work, but say you had
    Java Code:
    char[] ch={'0','1','2','3','4','5','6','7','8'};
    you would still print out "987654321" and not the values of the array which are "876543210".

    As for your loop, instead of printing out the value of x, use x as the index of the array ch to access the value you want.

    Hope this helps and sorry if I get what you want to do mixed up :)

    Meli

  14. #14
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    where can i find it?
    can you post here the link?

  15. #15
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by beginner21 View Post
    System.out.println("\nThe sum is: "+z);
    if(z>ch.length)
    {
    System.out.println("INTEGER OVERFLOW");
    }

    all i want here is the word INTEGER OVERFLOW
    it only prints when the sum is more than the maximum number of digits(more than 8 digits)..
    but on my program..it always print..
    what should i modify or what was the error?
    When you look at that if statement, z is the sum which lets just say you got 122 for example. That will always be greater than the length of the ch array so it's always gonna print out INTEGER OVERFLOW. Instead here what you want to do is, since ints don't have a .length action, what is another way to check if an int has greater than 8 places?

    Meli

  16. #16
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

  17. #17
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    When you look at that if statement, z is the sum which lets just say you got 122 for example. That will always be greater than the length of the ch array so it's always gonna print out INTEGER OVERFLOW. Instead here what you want to do is, since ints don't have a .length action, what is another way to check if an int has greater than 8 places?
    your right,but how do i get that the meaning of 8 is the digits?
    example:111111111 is a 9digit number,right?
    the INTEGER OVERFLOW will only show when the sum is greater than the digit an array can perform,
    pls help
    Last edited by beginner21; 12-01-2008 at 11:30 AM. Reason: tag

  18. #18
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    Default

    Okay, to see if I got this, you want to print INTEGER OVERFLOW when the number of digits of the sum you got is greater than the number of values the array has?
    miss.meli (-.-)zzZZ

  19. #19
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    yah right...

  20. #20
    beginner21 is offline Member
    Join Date
    Nov 2008
    Posts
    34
    Rep Power
    0

    Default

    alright..
    i solve this atlast..
    ch.length should be replaced by Integer.MAX_VALUE
    tnx to all...

Page 1 of 2 12 LastLast

Posting Permissions

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