• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Two Dimensional array declaration in Java

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have wrritten a two dimensional array in Java.
Let's check the program.
It is not giving any compilation error but there is runtime error NumberFormatException.

Regards
Tanka


 
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you putting a try-catch around the code in inarr which doesn't declare an exception, and not catching the IOException from the BufferedReader? And why are you not closing the BufferedReader?

What are you entering to get the NumberFormatException? You must enter something which is a real number, then you won't get the NumberFormatException, so you can go on and deal with the NullPointerException which you will get in the next stage
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way: there is no such thing as a two dimensional array in Java. What you have set up is an array of arrays.
 
Tanka Banerjee
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
try-catch is not necessary in the code.
IOException is being thrown.
NumberFormatException comes when I give input like:
java multidimensionalarray 4 5 6 7 7 9 7 6
6 8 6 78 9 7
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at multidimensionalarray.main(multidimensionalarray.java:28)

Regards
Tanka


[ December 23, 2008: Message edited by: Tanka Banerjee ]
 
Tanka Banerjee
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Why are you putting a try-catch around the code in inarr which doesn't declare an exception, and not catching the IOException from the BufferedReader? And why are you not closing the BufferedReader?

What are you entering to get the NumberFormatException? You must enter something which is a real number, then you won't get the NumberFormatException, so you can go on and deal with the NullPointerException which you will get in the next stage



I need to change the logic.
 
Tanka Banerjee
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java multidimensionalarray 4.0 6.9
4.5 76.9
Exception in thread "main" java.lang.NumberFormatException: For input string: "4
.5 76.9 "
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at multidimensionalarray.main(multidimensionalarray.java:28)

I am getting the NumberFormatException when I am typing this input.

Regards
Tanka.
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to convert 4.5 to an int. 4.5 isn't an int.
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you are using a BufferedReader, you can only enter one number on each line. A Scanner would have been easier to use.

Why are you passing command-line arguments? You don't use them anywhere in the code. It seems a very strange bit of design to use "try" round code which doesn't declare an Exception and "throws" on code which does declare an Exception.
 
Tanka Banerjee
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code Modified.

[ December 23, 2008: Message edited by: Tanka Banerjee ]
 
Tanka Banerjee
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java multidimensionalarray 4
4

No error coming.
I will take System.out.println() to see what is happening inside.

Regards
Tanka
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have obviously worked out why you might get a NullPointerException and corrected that.
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
The first time I saw the program I didn't notice anything.
But when compiling & running I noticed NullpointerException it was when I realized that the array has not been initialized properly

I rectified the same & found the application running.

But when Campbell ritchie sees that with his vast experience, he was able to find some flaws in the code & offered unique solutions.
The things which I missed out were

1) Unnecessary wrapping up of code in try , catch block when that block of code doesn't have the potential to cause any exception.

2) For any streams or connections, it has to be closed which is not the case here .

But regarding throwing any exceptions from BufferedReader, it has to be kept in mind that this exception is declared in the throws clause.


By the way: there is no such thing as a two dimensional array in Java. What you have set up is an array of arrays.


Fitting reply.


And if you are using a BufferedReader, you can only enter one number on each line. A Scanner would have been easier to use.


How you are saying that?
You mean we can pass the values as commandline arguments & then use Scanner.useDelimiter property to parse them right.
But Scanner's constructor need a file as parameter right.
Then how can we get values & pass it to scanner.

But finally, it was a very good learning experience to learn about the best practices from experienced people like Campbell Ritchie.(Eventhough he is harsh in his reply, atleast it sounds like that)
Thanks Ritchie.
Regards.
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am very sorry for sounding harsh.

You can pass the numbers as command-line arguments, then they are a String array, and you can parse its members. You have seen that array hundreds of times and never noticed it. It is called args.

java DoubleArrayFiller 3

If you forget the command-line argument you will get an ArrayIndexOutOfBoundsException with the number 0. If you pass 3.0 you will get a NumberFormatException.

You can write 2 3 4 5 6 (not at the command line) and pass that line to a Scanner, and use its hasNextInt() and nextInt() methods in a while loop to run the whole app five times, using 2 3 4 5 and 6. Scanner won't work in Java 1.4.2 or earlier.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You can write 2 3 4 5 6 (not at the command line) and pass that line to a Scanner


Are you saying i need to write a text file with 2 3 4 5 6 & pass that to scanner which takes these values.
Regards.
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can write a text file, or you can pass System.in to the Scanner constructor. Whichever you prefer.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using System.in Scanner i'm able to get the int values.


When I compile & run it:

Eventhough I'm able to get all the values I pass, remember control is not back to the JVM i.e. C:\sai here
It means that hasNextInt() always returns true even after reading all the input values.
How it can be avoided?
Regards.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i try to find the end of value in the input stream using

The output I get is:

Still the challenge gets tougher, not any lesser.
Regards.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually what happens is even after printing the inputstream values , the scanner waits for input from the inputstream .
I've modified my source slightly.


output:


See the values 56 & 78 are entered the second time when the scanner is open to get values from the input stream

Now if we want to close the stream
1)we should enforce an constraint on the number of values which can be given by creating an array with some length.



2) After the array has been filled with values , close the inputstream .
(Note: If you try to close the scanner you will get IllegalStateException )



This is the code



output:

Note: The control has been returned.

I want to know is there any way
1) to find the length of the input stream values we give.

2) Can we impose restrictions on the number of values scanner can parse in Scanner or in inputstream on number of values we can give.

Regards.
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.in and System.out and System.err probably don't need to be closed. All the other Streams do, not those three. You probably mean sc.close();

You can have a for loop to count how many values you want from the standard input.
The only way to get -1 from a Scanner reading from System.in is to write -1. It doesn't have a default to -1 for end of stream. But you can write -1 to signify finishing the application if you so wish.

You will occasionally have to use the nextLine() method to move onto a new line, and discard the input.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

System.in and System.out and System.err probably don't need to be closed. All the other Streams do, not those three. You probably mean sc.close();


No, if I try to close sc.close() it throws java.lang.IllegalStateException


You can have a for loop to count how many values you want from the standard input


That's what I've done


The only way to get -1 from a Scanner reading from System.in is to write -1. It doesn't have a default to -1 for end of stream.


I've tried it out by

But the problem with this approach
1) It doesn't display all the values I enter through System.in
2) Old problem of non-return of control persists.


You will occasionally have to use the nextLine() method to move onto a new line, and discard the input.



Not understanding this point.
Regards.
 
Campbell Ritchie
Marshal
Posts: 80632
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't take over somebody else's thread. Please post the questions about Scanner in a new thread; they are not related to the original poster's problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic