• Post Reply Bookmark Topic Watch Topic
  • New Topic

returning values to int arrays  RSS feed

 
amanda allen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Just need a little help with an assignment - I'm a complete newb and have been staring at this (probably very simple) problem and desperately need some help. Basically, our teacher has asked us to create a DriverExam class that will work with the DriverExamDemo application he has provided us. I'm worked hard on the class and think I have almost everything right, and finally have it compiling. Problem is now, when I try to compile the teacher's demo to test everything, I'm getting this error at line 43: incompatible types: int can not be converted to int[] which pertains to this line missedQuestions = exam.questionsMissed();. Now, I get what it's saying, but I just can't think of the life of me how to change it, I've tried a million things and just end up with more errors. Can anyone help? My class and the demo is below.

Cheers,

N

Class is:


Demo is:




Thanks in advance for any points in the right direction!
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amanda.

You need to put your code inside code tags.
[code]place your code here[/code]

That will make it easier to read (as long as it is nicely formatted).

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now the error message is telling you that you are trying to assign an int to an int[] (an int array). I think your problem is that the questionsMissed() method is means to contain an array containing the question numbers that were missed, and not an int.

However the code that you have posted does not look to me like the code that produced your error. Did you change the demo class so that missedQuestions variable is an int instead of an array?

I bet the error you get when you compile the code you posted complains about the missedQuestions.length expression, because an int does not have a length.
 
amanda allen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Mike Sorry about not enclosing my code. I've never posted to a forum before. I've been using eclipse for the first time and managed to get both to now compile - but that's where the fun stops sadly - and I have no idea how to fix it.

I've posted the class and demo code below, with the fixes suggested by eclipse - which I'm afraid has given me the run around. Now, eben though everything finally compiles and runs, when I test the demo, for some reason there actually is no way to miss a question, ie if you just hit enter without inputting an answer, the whole program stops and a big error comes up saying: Exception in thread 'main' java.lang.stringindex.outofboundsExcex: 0... and a few other lines. This essentially means that I can't even test the missed questions anyway. On top of that, while my correct answers is working, my incorrect answers is giving me a random negative number (which indicates my 'questions missed count' isn't working'). Before I throw in the towel on this one - can you see anything obvious that is seriously wrong in the eclipse corrected code below? If not, thanks for your time anyway, I appreciate it. I've honestly never found anything as difficult as learning java - hopefully it gets easier with practice!

My class


and my demo


 
amanda allen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK have fixed my totalCorrect, totalIncorrect and passed, but still no luck being able to 'miss' questions when testing the demo...
 
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
amanda allen wrote:if you just hit enter without inputting an answer, the whole program stops and a big error comes up saying: Exception in thread 'main' java.lang.stringindex.outofboundsExcex: 0... and a few other lines. This essentially means that I can't even test the missed questions anyway.


The error occurs at line 22 of your demo code ...



Basically, you never get to the point of validating the input. If you just hit return, then the string returned doesn't have any characters. And when you try the charAt(0), which tries to get the first character, it will fail. You need to modify the code, to confirm that the string returned has at least one character before trying to get the first character.

Henry
 
amanda allen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Henry I'm so sorry though, I just am completely lost with what you think I should do. As a complete newb, our assignment was to write the class - the teacher wrote the demo, so I'm not 100% sure how the validation part works -- but is there something I should be fixing in my class that's interfering with the demo, or is the demo just wrong?

Thanks again - really helping me not go crazy here!!

N
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adamanda: you almost have the code tags right, but not quite. Here's a link: UseCodeTags. I'll edit them for you this time. Thanks for the effort.
 
amanda allen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Knute - learning as I go along, unfortunately it's all so foreign to me that my interpretation can be a bit off! Don't suppose you understood what Henry suggested I should do?

N
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Since input can be null (empty), the line input.charAt(0) can throw an error. To protect against that, test for null:

 
amanda allen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time Knute! I've added in your suggestion and it's still throwing the same error, have I done it wrong?


 
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
amanda allen wrote:I've added in your suggestion and it's still throwing the same error, have I done it wrong?


Basically, your demo program has more than once instances of that issue. And you fixed the second one. The error is still happening on the first one.

As an additional note, which you will encounter after you fix this ... your demo program doesn't let you enter any data that doesn't invalidate. This means that it will be impossible to skip questions, the program will repeatedly ask you for an answer until you enter a valid one.

Henry
 
amanda allen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry - that was such an obvious one, I'm so sorry - can I claim 12 hours staring at this thing and work starting in 4 hours as a little excuse? I've just updated and encountered your next note ie inability to actually miss questions - begs the question, was our teacher just setting us up for a long and confusing night, or is it just something he's overlooked. Because it's a char data type, there's no way to include an empty or even a 0 in as an acceptable character is there?

Thanks again - though my alarm will go off shortly, I'm a lot less frustrated!!

N
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to go back to the teacher and ask what exactly your requirements are. As the code stands each answer must be one of 'a', 'b', 'c' or 'd'. Unless one of these options means 'missed' then there is currently no way to miss a question.

You'll need to ask your teacher how a missed question will be indicated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!