• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Help with a Programming assignment

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just another update same result just added in the other letters
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Honestly I have no idea I think I was trying to store the characters from charAt into an array and use that
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deron Brown
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Jeez after two days of hard work I've got it working thanks to everyone who responded and tried to help me solve my problem! Special thanks to Rico Felix for being there and helping me through it one step at a time I've got the working code
solved.png
[Thumbnail for solved.png]
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although your code works you aren't finished yet... simply because the method you chose to print the information is not good practice... it should be done using a loop, that's why they were created to avoid repetitive typing and duplication
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you mentioned its two days you were working on this I think you won't want any more to do with this nuisance...

Here is a gift of the my version:


 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deron Brown wrote: . . .



No. That is dreadful. You have written the same code twenty‑six times. That needs to go in a loop.
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic