aspose file tools*
The moose likes Java in General and the fly likes Getting an array out of bounds error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting an array out of bounds error" Watch "Getting an array out of bounds error" New topic
Author

Getting an array out of bounds error

bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
Okay so i'm getting this error saying:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at TestHomework.main(TestHomework.java:55)

I've tried fixing , but i haven't had any luck anyone care to help me on this problem?

Heres my code:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11320
    
  16

so, line 55 is this:

That is saying "take the first command parameter and save it as "filename".

how are you running this? It is expecting you to pass in a file name, so something like this:

c:>java TestHomework my.filename


my guess is you are just doing this:

c:>java TestHomework


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
I have the same opinion. Without command line parameter: java.lang.ArrayIndexOutOfBoundsException.
bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
I'm using xcode on mac to do the program and then running it on terminal.

Okay i get you now.
An yes i was running it like this: c:>java TestHomework

Is there any way i can make it so when i run it it will ask me what i put like "What is the name of the shuffled deck file? " and then enter the name of the file
instead if putting it along with the java TestHomework?
Ivan Franko
Ranch Hand

Joined: May 30, 2011
Posts: 44
bob [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:jenkins[/surl]]I'm using xcode on mac to do the program and then running it on terminal.

Okay i get you now.
An yes i was running it like this: c:>java TestHomework

Is there any way i can make it so when i run it it will ask me what i put like "What is the name of the shuffled deck file? " and then enter the name of the file
instead if putting it along with the java TestHomework?


Of course you can do it. Take a look at this link, please: webpage
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

bob [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:jenkins[/surl]]
Is there any way i can make it so when i run it it will ask me what i put like "What is the name of the shuffled deck file? " and then enter the name of the file
instead if putting it along with the java TestHomework?


You'll want to print the question out to System.out and read the response from System.in. You can do that with java.util.Scanner or java.io.BufferedReader. Google for java io tutorial if you're not sure how to get started, and post again if you get stuck.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61227
    
  66

Ivan Franko wrote:
Of course you can do it. Take a look at this link, please: webpage

That is exactly what he did not want to do.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
Thanks for those links Jeff. I looked at it, but i'm having trouble on how to implement it in my program. Do i pretty much have to erase this whole part of the program:



Or do i just modify it?
i'm a little confused haha
bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
another little simple question. is it bad that i have my main on the bottom of my program or does it even matter?
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
for entry from the command line you can just wrap System.in inside a Scanner:



dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
bob [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:jenkins[/surl]]another little simple question. is it bad that i have my main on the bottom of my program or does it even matter?


It doesn't matter. Oracle's coding conventions advise placing variables before constructor before methods, but I don't see anything specifically addressing the placement of main. I think it's a good practice to keep main separate from other methods, so as last element in the class I think it's well placed.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11320
    
  16

bob [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:jenkins[/surl]]another little simple question. is it bad that i have my main on the bottom of my program or does it even matter?

it is certainly allowed. as far as java is concerned, it does not matter.

beyond that, you get into a grey area. Different companies will have different coding standards, so the correct answer is "what does your coding standard say about the placement of main()?" I don't recall seeing anything in the offical java standard for where the main() method should go, but don't quote me on that.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

bob [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:jenkins[/surl]]Thanks for those links Jeff. I looked at it, but i'm having trouble on how to implement it in my program. Do i pretty much have to erase this whole part of the program:



Or do i just modify it?
i'm a little confused haha


If it's working the way you want reading from a file, and you're going to have the same line structure for user input at the console as what's in the file, then you can just create a Scanner for System.in instead of for your file and keep the rest of the code the same. The scanner doesn't care if it reads

from a file or from the console. Either one is just another input source as far as it's concerned.

bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
OKay just to sum it up.(for myself purposes) What i am trying to get accomplished right now is when i run my program i want it so when i input this command in the terminal:

java Homework5

i want it so when i enter ^^ it then says : What is the name of the shuffled deck file?

what i have right now only lets me put the txt file along with the command such as:

java Homework5 deck1.txt

The part of the program where i am fixing this looked like this:



and finally to accomplish what i want i have to have the program like this? :



Please correct me if i'm wrong as i think i am.
I GREATLY appreciate the help i'm getting! i used to go to another website one which i won't name, but none of them really helped or didn't help at all. Once again i appreciate the help guys!
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Bob the code I gave you will achieve what you need
bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
Wesleigh Pieters wrote:Bob the code I gave you will achieve what you need


Yeah i understand that, but when i enter your code i get errors saying:

TestHomework.java:83: cannot find symbol
symbol : variable StringfileName
location: class TestHomework
StringfileName = input.next();
^
TestHomework.java:83: cannot find symbol
symbol : variable input
location: class TestHomework
StringfileName = input.next();
^
TestHomework.java:86: scan is already defined in main(java.lang.String[])
Scanner scan = new Scanner(System.in);
^

i don't know if im putting it in right or what's happening
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Read the error messages carefully. Look closely at the line they're pointing at.

What, exactly, do you expect this line to do? What do you think each piece means?

Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Jeff Verdegan wrote:Read the error messages carefully. Look closely at the line they're pointing at.

What, exactly, do you expect this line to do? What do you think each piece means?



yes this, give your mind some space to think
bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
My Brain Hurts 0_0
okay give me a sec let me see if i can figure this out.
by the way thanks for the quick replies
bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
Well i'm stumped
I'm sorry, but i can't figure it out my brains not working as it should
I feel like i'm bugging you guys with my dumb questions
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
bob [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:jenkins[/surl]]Well i'm stumped
I'm sorry, but i can't figure it out my brains not working as it should
I feel like i'm bugging you guys with my dumb questions


you have said StringfileName instead of String fileName, I.e no space
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

bob [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:jenkins[/surl]]Well i'm stumped
I'm sorry, but i can't figure it out my brains not working as it should
I feel like i'm bugging you guys with my dumb questions


You wrote this line:


What, exactly, did you intend when you wrote StringfileName? Tell me what that word means.

Then go back and read the first error message very closely.


The compiler's error messages usually tell us exactly what we did wrong, and it's usually pretty direct.
bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
WOW am i stupid the littlest mistake and i couldn't spot it >

okay so i currently have this in that spot



it compiles which makes me happy, but when i run it nothing happens, it stays blank
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

bob [surl='http://jenkins-ci.org/' class='api' title='Continuous Integration Engine wrote:jenkins[/surl]]WOW am i stupid the littlest mistake and i couldn't spot it >


Don't worry about it. We all get that tunnel vision sometimes. Experience doesn't make it go away completely, but it does help us to recognize the patterns and know where to look for what we missed.


it compiles which makes me happy, but when i run it nothing happens, it stays blank


Please post the complete current code, if it's short, or an SSCCE if it's not, so that we can see the current state of it in one go, rather than having to scroll back and piece it together from disjoint chunks.
bob jenkins
Greenhorn

Joined: Nov 14, 2012
Posts: 12
Sure i changed a big chunk of the program hope that doesnt make a difference however the part we were working on which is the main method stayed the same.

Here it is:

Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
Bob it does work, I just reordered it so that you are asked for the prompt before you need to give it

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting an array out of bounds error