Java - "int" and "long" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Java - "int" and "long"

Hi. I created this program: public class MainClass { public static final int anzahl1 = 100; public static int[][] feld = new int[anzahl1][anzahl1]; public static int[][] wert2 (int y, int x) { return feld; } public static int wert(int y, int x) { if ((x==0 ) || (y<=x)) { feld[y][x]=1; return feld [y][x]; } feld[y][x]=feld[y-1][x-1] + feld[y-1][x]; return feld[y][x]; } public static void main(String[]args) { for (int y=1; y < anzahl1; y++) { for (int x =1; x <= y; x++) { System.out.print(wert (y, x) + " "); } System.out.println(); } } } but when i run it, i get negative numbers (cause theyre too big). which "int"s do i have to change to "long"s so it works?? thank you all :D

3rd Jul 2018, 2:53 PM
PhoenixPlay
20 Answers
+ 4
PhoenixPlay you're being pretty rude, just saying https://code.sololearn.com/cQTAGZzLoUX9/?ref=app also here, i don't see negative numbers in the output so this should be the ok
3rd Jul 2018, 7:39 PM
hinanawi
hinanawi - avatar
+ 9
PhoenixPlay dont change array int then.
3rd Jul 2018, 3:25 PM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 8
PhoenixPlay Its better to change all int to long.
3rd Jul 2018, 2:58 PM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 3
PhoenixPlay here on SoloLearn we have possibility of save our codes and post his url in discussions then why you copy/paste code when you can save on "Code Playground" section? Have you understanded that this make more easy read code then help who has problems?
3rd Jul 2018, 4:48 PM
KrOW
KrOW - avatar
+ 3
PhoenixPlay it works because anzahl1 is the size of the array, and that has to be an integer. also, it crashes because you either exceeded SL's time limit or you exited the bounds of the array.
4th Jul 2018, 7:20 AM
hinanawi
hinanawi - avatar
+ 2
Thank you soo much hinanawi !! can you explain why it works when the array type is long but "anzahl1" is still int? 😓
4th Jul 2018, 4:54 AM
PhoenixPlay
+ 1
PhoenixPlay Most of my SL code are developed on mobile... You can save your code on PC (in "Code Playground" section) and you will find on mobile also
3rd Jul 2018, 4:51 PM
KrOW
KrOW - avatar
+ 1
Is this program used to find pascal's triangle?
18th Oct 2019, 12:58 PM
Alok Raj
Alok Raj - avatar
+ 1
I was just looking for characteristics of the keyword "long" other than the obvious definition.
18th Oct 2019, 2:19 PM
Alok Raj
Alok Raj - avatar
0
but the int in the array can't be changed...?
3rd Jul 2018, 3:24 PM
PhoenixPlay
0
but the array takes the others to calculate (which u want to change to long). if it would be that easy I wouldn't ask
3rd Jul 2018, 3:27 PM
PhoenixPlay
0
it's definitely possible though to get those negative numbers away with some long instead of int
3rd Jul 2018, 3:28 PM
PhoenixPlay
0
i'm sry Dude I coded in eclipse and now I'm on mobile KrOW
3rd Jul 2018, 4:49 PM
PhoenixPlay
0
yeyeyeye ik.. could you pls just help me? please? 😓🤐
3rd Jul 2018, 4:52 PM
PhoenixPlay
0
https://code.sololearn.com/c93DdKKRJVmk/?ref=app I do it on mobile at YOUR place
3rd Jul 2018, 4:59 PM
KrOW
KrOW - avatar
0
And why the heck is it so hard to tell me what I need to change so i dont get these negative numbers?!? god damn it, you'll be mod soon. you're supposed to help beginners (which i am). just frickin do your job, ok? ._. so, when you put it into the editor, couldn't you tell me what to change??
3rd Jul 2018, 5:25 PM
PhoenixPlay
0
My job its not help beginners... I help they if i can but this dont mean only give answers. I try to help they to get right behaviour because THIS is a comunity and has rules as well. Sometimes there are explicit rules (like don SPAM, dont offend other users etc), sometimes are implicits like if you want help, woul be better if you place who help in right condition. At example by posting a code (if its greater than 5-6 lines) not by copy/paste but saving it in "Code Playground" so we have only execute your code for debug/check it. Its so hard to understand? You want answer but dont want follow this implicits rules and this make who want help less prone to help you. I tell this just for your convenience. Anyway a solution its yet gived you, use long... You code make overflow on int ( 32 bit) but using long (64 bit) you have much more numeric range to disposition
3rd Jul 2018, 6:04 PM
KrOW
KrOW - avatar
0
1. I just apologized for not putting the code into playground. I don't know why you just cant understand that. 2. a mod's job IS helping beginners in the forums, which of course doesn't mean you can't ask questions yourself. 3. I tried changing every int to long, but i don't know how to create an array with long as content instead of int. that's exactly why I am asking. 4. I know the difference between 32 bit and 64 bit, Ty.
3rd Jul 2018, 7:17 PM
PhoenixPlay
0
PhoenixPlay 1) I understanded this but i dont understand because when i suggested you to save your code, you dont it 2) I am not a mod and main role of mod its make sure that all respect rules (lucky they help also) 3) I repeat... Post your code in right format. Tell that you have changed all int its not help. If you understand that we want help, you stop to critics and post your code following procedure that i have suggested 4) Welcome
3rd Jul 2018, 7:26 PM
KrOW
KrOW - avatar
0
well I just crashed the whole array. is there a limit of how many long type numbers you can save in an array? (btw I crashed it with creating 3000 lines)
4th Jul 2018, 5:58 AM
PhoenixPlay