Greg Charles wrote:First, you changed newCard = false to !newCard. newCard == false would be equivalent to !newCard, but that's not what you had. Second, it didn't do nothing. It exposed your next bug, which unfortunately has similar symptoms. Look into how you set the newCard variable, and in particular once it's set to true, does it ever get set back to false? Put in System.out.printlns if you're having trouble following the flow.
By the way, since you know how to use loops, why don't you use one in place of lines 65 - 74, and another for 79-88? And what is line 63 for?
Greg Charles wrote:First, you changed newCard = false to !newCard. newCard == false would be equivalent to !newCard, but that's not what you had. Second, it didn't do nothing. It exposed your next bug, which unfortunately has similar symptoms. Look into how you set the newCard variable, and in particular once it's set to true, does it ever get set back to false? Put in System.out.printlns if you're having trouble following the flow.
By the way, since you know how to use loops, why don't you use one in place of lines 65 - 74, and another for 79-88? And what is line 63 for?
Greg Charles wrote:It's generally a bad idea to compare boolean variables with boolean literals using ==. That is, just use newCard instead of newCard == true, and !newCard instead of newCard == false. The reason this is a bad idea is that you can accidentally use the assignment operator = rather than the comparison operator ==, and create a hard-to-diagnose bug in your code.
Greg Charles wrote:It probably comes from line 63, where you refer to selectedSuit[10] (and also selectedCard[11]). You define both these arrays to have size 10 on line 37 and 38. Since Java arrays count from 0, only the indexes 0 through 9 are legal to use.
Campbell Ritchie wrote:There is something very wrong with having parallel arrays. They are error‑prone. There must be a simpler way to do it.
One suggestion: you know that chars represent letters, don't you? As all beginners know. But that is totally wrong. A char is a number. So you can do arithmetic on it. You can work out the index for the Morse code like that and print the code out. What you do about spaces I am not sure, but I suspect that printing | isn't quite it.
Please go back with thebutton and break those long lines; if you don't have a posh big screen, they are very difficult to read.
Matthew Brown wrote:It's helpful if you tell is what the errors are, but here are a couple of problems I can see:
- On line 5 you refer to Input. That might not be an error if you have a class of that name, and it has a static method called getString, but you haven't told us about that.
- On line 13 you refer to an input variable that isn't declared anywhere. Is that meant to be str.charAt(k)?
Rico Felix wrote:What that line does is create a new array of type char with the size of the integer representation of the character returned from the method invocation... doesn't do much good for what you are trying to achieve
Look at this section of code carefully:
If we carefully study what it does then we can probably fill in the void within the if statement
- We are looping through the length of the string... that mean if we have the string "Java" we are looping 4 times since it is made up of four characters
- For each iteration we are checking to see if the character in the string at index k is a letter... if its a letter we want to know what letter it is so we can increment the slot in the array that's keeping our running count of occurrences
We have the following array to keep our running count
This was set up because we know there are 26 letters in the alphabet so incrementing index 0 will say that we found an 'A' or 'a'... incrementing index 1 will say that we found a 'B' or 'b' etc...
In order to know which index in the array to increment within the if statement we came up with an algorithm that say since we are in the if statement its a letter so get the letter, transform it to lowercase and subtract 'a' from it to get the array subscript and increment
This is what you need to do in words... now transform it to working Java code
Rico Felix wrote:I'm glad that you are at least thinking about different solutions...
![]()
Now the question is... what is the meaning of this -> char [] chars = new char [str.charAt(k)];
Explain what you're doing in that line of code
Rico Felix wrote:I've deliberately left you to run it to see the error message... what you are seeing is array access error... the expression that you have placed for the subscript is working out to a negative number since 'a' = 67 and 'k' = 107 if you do the maths that is equal to -10 which is an unacceptable subscript for an array