Results 1 to 19 of 19
Thread: why does it say "null"?!
- 09-20-2010, 07:50 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
why does it say "null"?!
hello! i was wondering why it says "null" whenever the value isnt 2-10, because when it hits for example 11, id like it to say "knekt"!
the names and stuff are in swedish i hope it doesnt bother ya too much, ill start programming in english *promise* :D
public class Kort {
private int kast;
private static int valör=13;
private static int färg =4;
private String KORT;
public void blandaKort(){
kast=(int)(Math.random()*valör);
if (kast==1){
String KORT=Integer.toString(kast);
KORT="ESS";
}
else if (kast==2){KORT="2";}
else if (kast==3){KORT="3";}
else if (kast==3){KORT="3";}
else if (kast==4){KORT="4";}
else if (kast==5){KORT="5";}
else if (kast==6){KORT="6";}
else if (kast==7){KORT="7";}
else if (kast==8){KORT="8";}
else if (kast==9){KORT="9";}
else if (kast==10){KORT="10";}
else if (kast==11){
String KORT=Integer.toString(kast);
KORT="knekt";
}
else if (kast==12){
String KORT=Integer.toString(kast);
KORT="DAM";
}
else if (kast==13){
String KORT=Integer.toString(kast);
KORT="KUNG";
}
}
public void skrivUt(){
System.out.println(KORT);
}
public class KortProgram {
public static void main (String[] args){
Kort spel = new Kort();
spel.blandaKort();
spel.skrivUt();
}
}
}
- 09-20-2010, 08:15 PM #2
Where does it say "null"?
A problem you have is that you define new variables: String KORT over and over and over inside of {} and give them values that go out of scope and disappear when the execution exits the {}s. Remove "String" from "String KORT" to use the one variable defined at the beginning of the code.
- 09-20-2010, 09:03 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
While we're at it: use enums for your playing cards. If I'm not mistaken the Tutorials even use playing card examples in their text ...
kind regards,
Jos
- 09-21-2010, 12:50 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
im sorry, i dont even know what enums mean :S
- 09-21-2010, 01:11 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
Hey u!
As Norm said :
A problem you have is that you define new variables: String KORT over and over and over inside of {} and give them values that go out of scope and disappear when the execution exits the {}s. Remove "String" from "String KORT" to use the one variable defined at the beginning of the code.
I copied ur code and run it without all those declarations inside the if else code and it worked!!!!!!!
Try to do this!!!!!
Another comment: check what static does when u use it for a variable ;) i tell u this cause when i was newbie i did exactly the same mistakes as u ;)
- 09-21-2010, 07:39 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
i very very appreciate ur help!
i modified my code and put "färg", which is the same principle as the "kort"... it means color resp. card in english.. again, sorry for the swedish coding :D
anyway, it seems to have the same problem, it creates a "null" value when i try to print it out and ive almost copy-pasted it from "kort", just changed the name to "färg" :eek:
public class Kort {
private int kast, kast2;
private static int valör=13;
private static int färg =4;
private String KORT;
private String FÄRG;
public void blandafärg(){
kast=(int)(Math.random()*valör)+1;
if (kast==1){
KORT=Integer.toString(kast);
KORT="ESS";
}
else if (kast==2){KORT="2";}
else if (kast==3){KORT="3";}
else if (kast==3){KORT="3";}
else if (kast==4){KORT="4";}
else if (kast==5){KORT="5";}
else if (kast==6){KORT="6";}
else if (kast==7){KORT="7";}
else if (kast==8){KORT="8";}
else if (kast==9){KORT="9";}
else if (kast==10){KORT="10";}
else if (kast==11){
KORT=Integer.toString(kast);
KORT="knekt";
}
else if (kast==12){
KORT=Integer.toString(kast);
KORT="DAM";
}
else if (kast==13){
KORT=Integer.toString(kast);
KORT="KUNG";
}
}
public void blandaFärg(){
kast2=(int)(Math.random()*färg)+1;
if (kast2==1){
FÄRG=Integer.toString(kast2);
FÄRG="Hjärter";
}
else if (kast2==2){
FÄRG=Integer.toString(kast2);
FÄRG="Spader";
}
else if (kast2==3){
FÄRG=Integer.toString(kast2);
FÄRG="Klöver";
}
else if(kast2==4){
FÄRG=Integer.toString(kast2);
FÄRG="Ruter";
}
}
public void skrivUt(){
System.out.println(FÄRG+" "+KORT);
}
}
public class KortProgram {
public static void main (String[] args){
Kort spel = new Kort();
spel.blandafärg();
spel.skrivUt();
}
}
- 09-21-2010, 07:48 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
one questions, does it returns both null?!
- 09-21-2010, 07:48 PM #8it creates a "null" value when i try to print it out
What variable are you talking about?
A comment on your program:
When you have a list of chained together if/else if statements, add an ending else to handle the condition that none of the preceding tests caught. Print out a message there showing the value and a message about the problem.
This will help you find bugs in your program.
- 09-21-2010, 07:51 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
well, the cards ("kort") are printing out just alright, just the "färg" that prints out null
- 09-21-2010, 07:54 PM #10
Add the else at the end of the if/else if chain and you should see what your problem is.
- 09-21-2010, 07:55 PM #11
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
- 09-21-2010, 08:00 PM #12
Let me repeat for the third time:
Add the else at the end of the if/else if chain and you should see what your problem is
- 09-21-2010, 08:01 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
you mean like this:
public void blandaFärg(){
kast2=(int)(Math.random()*färg)+1;
if (kast2==1){
FÄRG=Integer.toString(kast2);
FÄRG="Hjärter";
}
else if (kast2==2){
FÄRG=Integer.toString(kast2);
FÄRG="Spader";
}
else if (kast2==3){
FÄRG=Integer.toString(kast2);
FÄRG="Klöver";
}
else{
FÄRG=Integer.toString(kast2);
FÄRG="Ruter";
}
}
- 09-21-2010, 08:07 PM #14
No that wasn't what I said. I said to ADD an 'else', NOT to change the last 'else if' to an 'else'.
Why do these two statements:
FÄRG=Integer.toString(kast2);
FÄRG="Ruter";
The second one replaces the value saved by the first one?
Do you mean to concatenate the values? Something like this:
FÄRG=Integer.toString(kast2);
FÄRG += " Ruter"; // Concatentate to follow number
- 09-21-2010, 08:09 PM #15
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
mmmmm......
u have 2 methods differrent:
public void blandafärg()
and
public void blandFärg()
u never call the second one, so its normal the fact that your String is null!
- 09-21-2010, 08:14 PM #16
That shows another place where putting print outs in your code to show execution flow would help you find bugs. When a print out is NOT done, then you know that that code has NOT been executed.
- 09-21-2010, 08:21 PM #17
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
- 09-21-2010, 08:22 PM #18
Member
- Join Date
- Sep 2010
- Posts
- 28
- Rep Power
- 0
- 09-21-2010, 08:25 PM #19
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
I have to admit that in order to find your mistake i had to put System.out.println to your code in order to follow your execution!!!! Norm is absolutelly right about that, printing out is the BEST way (and for me the first and the only way)to find bugs!!!!
Last edited by jlmp; 09-21-2010 at 08:27 PM.
Similar Threads
-
How to "allow null" in a very simple script?
By Mattedatten in forum New To JavaReplies: 7Last Post: 12-05-2010, 12:09 AM -
JOptionPane.showMessageDialog(null,"Etc Etc"); - What does null actually do?
By markious in forum New To JavaReplies: 2Last Post: 03-19-2010, 06:30 PM -
jList Issues - getSelectedValue() Keeps returning "null"
By MoobKeeng in forum New To JavaReplies: 0Last Post: 07-28-2009, 07:45 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 07:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 08:35 AM
Bookmarks