• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't get this code to work  RSS feed

 
maya tomarchio
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have this code...



and I can't get it to work. There are no error messages until I try and run it, and even then it dosn't say where or why. Any suggestions?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maya tomarchio wrote:There are no error messages until I try and run it, and even then it dosn't say where or why.


Well, regardless, it may help if you tell us what error message you actually got when you ran it.

Henry
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look how your code should be looking like when it gets formatted properly. When it follows convention of upper and lower cases, when it has spaces around binary operators.

Compare and see how your code differs:


Please tell us, how many elements you have in an array and what are their indices (each of them)?
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maya tomarchio wrote:So I have this code...and I can't get it to work. There are no error messages until I try and run it, and even then it dosn't say where or why. Any suggestions?

JDoodle, which is what I use to code, actually does give me an error. It's hard to believe Eclipse, if that's what you're using, doesn't give out any error.

The error is:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at MyClass.main(MyClass.java:8)"
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maya tomarchio wrote:There are no error messages until I try and run it, and even then it dosn't say where or why.

well..you don't get any errors when you compile it because it is a syntactically valid program. 

When I run in on the command line, it most certainly gives me an erorr, telling me exactly where and why. So now we have to ask - how are you running it?
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:. . . . It's hard to believe Eclipse, if that's what you're using, doesn't give out any error. . . .
I never thought that even Eclipse would give a warning in advance before such an Exception is thrown.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Sergiu Dobozi wrote:. . . . It's hard to believe Eclipse, if that's what you're using, doesn't give out any error. . . .
I never thought that even Eclipse would give a warning in advance before such an Exception is thrown.


Sorry, by error I meant exception.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Sergiu is questioning the "even then it dosn't say where or why" WRT the error.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Eclipse doesn't give you a stack frame with the appropriate line numbers, there is something wrong with it. The stack frame usually appears in red because Eclipse prints everything on System.err in red.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maya tomarchio wrote:There are no error messages until I try and run it, and even then it dosn't say where or why.

Out of curiosity and to answer any doubts other might have in their minds, what exactly *do* you see when this program fails at runtime? Copy/paste your actual error messages to a new post, please.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your mistake is very easy to see if, as Liutauras said, you formatted your code better. Look at the version of your code that he posted. Then look at the example for-loop here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Compare the two very carefully and see what you did differently. When you understand what the difference is between your code and the example in the tutorial, you'll be able to fix the problem yourself.
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Here, I have solved. IDK why sum is 1, it should be 10
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Actually this is btter, and I think it works as you wanted.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:Actually this is btter, and I think it works as you wanted.
It isn't. Actually it contains exactly the same problem as original OP's code. Your previously posted code is logically incorrect, which is worse than original OP's code.

Interesting, what output you got when you ran this code?
 
bob john
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
bob john wrote:Actually this is btter, and I think it works as you wanted.
It isn't. Actually it contains exactly the same problem as original OP's code. Your previously posted code is logically incorrect, which is worse than original OP's code.

Interesting, what output you got when you ran this code?

10
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maya tomarchio wrote:{21, 34, 8, 7};

21 + 34 + 8 + 7 = 70

bob john wrote:10


Apart from the incorrect solution, you're not telling us the truth, because the code you showed us wouldn't give you an output of 10.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob's code isn't adding the array numbers up.
It's just counting the number of elements in the array (or thereabouts).

Either way, of course, the answer isn't 10.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob john wrote:
Liutauras Vilda wrote:
bob john wrote:Actually this is btter, and I think it works as you wanted.
It isn't. Actually it contains exactly the same problem as original OP's code. Your previously posted code is logically incorrect, which is worse than original OP's code.

Interesting, what output you got when you ran this code?

10


You're probably speaking about a completely different program, the one you wrote above will never run.
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:

You're probably speaking about a completely different program, the one you wrote above will never run.


Well, it will run, but it will throw an exception.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shall we get back to the original subject? Has OP got the program to complete normally?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!