aspose file tools*
The moose likes Beginning Java and the fly likes string with null value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string with null value" Watch "string with null value" New topic
Author

string with null value

Vicky Makhija
Greenhorn

Joined: Jan 09, 2008
Posts: 1

What is the result?
A. null
B. zero
C. some
D. Compilation fails.

The answer to the above is "D"...
Can anyone explain me why the answer is D ??

[edit]Add code tags. CR[/edit]
[ October 01, 2008: Message edited by: Campbell Ritchie ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3974
    
  17

Originally posted by Vicky M:
public static void main(String[] args) {
String str = "null";
if (str == null) {
System.out.println("null");
} else (str.length() == 0) {
System.out.println("zero");
} else {
System.out.println("some");
}
}
What is the result?
A. null
B. zero
C. some
D. Compilation fails.

The answer to the above is "D"...
Can anyone explain me why the answer is D ??


What syntax did you get wrong? Hint: it is in your conditionals.


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

"Vicky M",

Please read your private messages regarding an important announcement.

Thank you,

Rob


As for the answer, type this in and compile it. It will give you three errors, the first one is the line where the problem is.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ruban raj
Greenhorn

Joined: Aug 05, 2008
Posts: 7
if we want to check more than one condition . . . [deleted. CR]
}
i hope this code will help you.


Thanks and regards,
ruban

[ October 01, 2008: Message edited by: ruban raj ]

[edit]Deleted solution. CR[/edit]
[ October 01, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Ruban Raj, thank you for the solution, but I would remind you what it says when you enter the beginners' forum
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
It would have helped Vicky much more to have found the error herself; that way she would remember it far better.

I have felt it right to edit your post and delete the solution. Sorry if you think I have messed you around.
NehaJain Jain
Greenhorn

Joined: May 11, 2013
Posts: 1
Vicky Makhija wrote:
What is the result?
A. null
B. zero
C. some
D. Compilation fails.

The answer to the above is "D"...
Can anyone explain me why the answer is D ??

[edit]Add code tags. CR[/edit]
[ October 01, 2008: Message edited by: Campbell Ritchie ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

NehaJain Jain,

Please read the ENTIRE thread before you reply. Had you done so, you'd have seen Campbell's post at 9:13 a.m., stating that we prefer to HELP people find the solutions, not just hand them out.

I removed the answer from your post.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
It wasn’t 9.13. It was 3.13pm. On 1st October.




2008, that was.

I hope it didn't take OP 4½ years to find the missing if?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10929
    
  12

Damn time-zones...
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Dear Vicky,
As steve rightly pointed out "look carefully at your conditional expression", there is something amiss there
If you are unable to find it, then try to decipher the error thrown by JVM, Understanding error thrown by JVM can go a long way in understanding the syntax and other innate functions of the language
Cheers :-)


Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string with null value
 
Similar Threads
Strings
mock exam Q
Help: convert from polish to infix notation
how to convert from string("a") to char..
Syntax error on token(s), misplaced construct(s)