• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two questions.  RSS feed

 
matthew meltzer
Greenhorn
Posts: 24
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first question is what do I need to do to fix the following code?



The error I am getting says that it reached the end of the file while parsing. What am I doing wrong?

The other question has to do with understanding the following code and what I am being asked to do.



"Can any errors or exceptions occur from the user failing to input arguments at runtime, or by putting certain arguments in at runtime? What will print if the user inputs nothing?
What will print if the user inputs an argument or several arguments?"


I don't understand what I am supposed to do.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
matthew meltzer wrote:The first question is what do I need to do to fix the following code?

Better indentation would allow you to see what's missing. Try properly indenting the code and see how the braces match up.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And have you really been told to call classes things like Q21? That is a very poor name for a class.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Can any errors or exceptions occur from the user failing to input arguments at runtime, or by putting certain arguments in at runtime? What will print if the user inputs nothing?
What will print if the user inputs an argument or several arguments?"

I don't understand what I am supposed to do.

What exactly are you stuck on? You just have to imagine you are running the code from the command line with or without some command line parameters and then work out what the code will do in each case.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if args is null ?

WP
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How on earth could args be null in a main method?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you post questions from a book, mock exam or some other source, then please tell us where you copied them from - QuoteYourSources.
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:How on earth could args be null in a main method?

Why not? Without any arguments passed while executing the class, the String[] blocks will have null or not?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:Why not? Without any arguments passed while executing the class, the String[] blocks will have null or not?

No. It will be an empty array. i.e. args.length will be zero.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:Why not? Without any arguments passed while executing the class, the String[] blocks will have null or not?

Not. It would be a 0-length array.

Winston
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Rajdeep Biswas wrote:Why not? Without any arguments passed while executing the class, the String[] blocks will have null or not?

Not. It would be a 0-length array.

Winston

Hi Winston,

I can think of 2 situations:

1. No args passed, the array is not constructed. So no question at all about its properties as the array does not exists!
OR
2. The args array has String type blocks, and the default value of String is NULL. So args[0].hashCode() [any instance method or else] will return a NullPointerException
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:1. No args passed, the array is not constructed. So no question at all about its properties as the array does not exists!

Yes it does. If no arguments are passed the array exists and has a length of zero.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:
I can think of 2 situations:

1. No args passed, the array is not constructed. So no question at all about its properties as the array does not exists!
OR
2. The args array has String type blocks, and the default value of String is NULL. So args[0].hashCode() [any instance method or else] will return a NullPointerException

Why not just print args in your main, and take a look what it prints..
You will get a nice looking hashcode telling you that its not null.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot get a null element in the command‑line arguments. You can pass new String[]{null, null, null}, but that array reference does not point to null. It is in fact the same as new String[3].
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!