• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting an array out of bounds error

 
bob jenkins
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same opinion. Without command line parameter: java.lang.ArrayIndexOutOfBoundsException.
 
bob jenkins
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob jenkins wrote: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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob jenkins wrote:
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
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bob jenkins
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for entry from the command line you can just wrap System.in inside a Scanner:



 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob jenkins wrote: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
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob jenkins wrote: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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob jenkins wrote: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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob the code I gave you will achieve what you need
 
bob jenkins
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob jenkins wrote: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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob jenkins wrote: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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob jenkins wrote: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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob it does work, I just reordered it so that you are asked for the prompt before you need to give it

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic