Results 1 to 20 of 21
Thread: [SOLVED] another problem
- 12-01-2008, 07:45 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
[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:
z is always 0;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); } }
i cant figure whats the problem here,
pls helpLast edited by beginner21; 12-01-2008 at 08:18 AM. Reason: code tags
- 12-01-2008, 08:34 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
x and y are 0, so x+y is zero.
- 12-01-2008, 08:43 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
but x and y is declared as the value of ch.length;
here is the output:
pls help..Java Code:987654321 //probably x 987654321 //probably y The sum is: 0
Last edited by beginner21; 12-01-2008 at 08:44 AM. Reason: code tag
- 12-01-2008, 08:48 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
They start out with the value 9 (which is the length of the character array, which is how you define them) and then here
you "count down" until they are zero. What exactly is it that you want to acheive?Java Code:for(x=ch.length;x>0;x--)
And those "numbers" you've shown are not the values of x and y, they are simply a printing of the char arrays values.
- 12-01-2008, 08:55 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
alright..
i get it..
so what should i place instead of
in order to print the array values backward.Java Code:for(x=ch.length;x>0;x--)
Last edited by beginner21; 12-01-2008 at 08:55 AM. Reason: tag
- 12-01-2008, 08:58 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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)?
- 12-01-2008, 09:24 AM #7
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
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--)
- 12-01-2008, 09:43 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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?
- 12-01-2008, 10:06 AM #9
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
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..
- 12-01-2008, 10:30 AM #10
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
alright..
here is my modified code:
the output of this is: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 { } } }
all i want here is the word INTEGER OVERFLOWJava Code:Enter first Integer: 5325256 Enter second Integer: 43252365 The sum is: 48577621 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?
- 12-01-2008, 10:32 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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
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.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]; }
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-01-2008, 10:36 AM #12
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 12-01-2008, 10:43 AM #13
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
you would still print out "987654321" and not the values of the array which are "876543210".Java Code:char[] ch={'0','1','2','3','4','5','6','7','8'};
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
- 12-01-2008, 10:46 AM #14
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
where can i find it?
can you post here the link?
- 12-01-2008, 11:01 AM #15
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
- 12-01-2008, 11:03 AM #16
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 12-01-2008, 11:29 AM #17
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
your right,but how do i get that the meaning of 8 is the digits?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?
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 helpLast edited by beginner21; 12-01-2008 at 11:30 AM. Reason: tag
- 12-01-2008, 11:33 AM #18
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
- 12-01-2008, 11:34 AM #19
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0
yah right...
- 12-01-2008, 11:40 AM #20
Member
- Join Date
- Nov 2008
- Posts
- 34
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks