• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Program

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone help me through this please? I know this is incomplete, and probably a mess, but I have no idea how to finish or fix this.



For this class file, I'm having trouble figuring how to code items e-g (ln. 67-94)



Here, I'm having trouble declaring my objects and Strings (ln 26-27, 40, 41). You'll see in the comments that we are to call these by the same name in both places, which is complicating things for me.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's start with one. numVowels(). You have an instance variable called "quote." And you need to figure out how many vowels are in it. Forget about Java for a moment. Suppose you had to explain to a pre-schooler how to find the vowels in a list of letters. You can assume the pre-schooler knows the alphabet and how to count, but not what a vowel is.

First question: how would you explain to the pre-schooler which letters are vowels?
Second question: try to explain in words the algorithm. in particular, look at each letter in time and keep track of what?

You can even post the answers here if you are still struggling or even just for fun Then try to translate what you describe to Java code. See how far you can get implementing this algorithm and post back here with the code you write that doesn't compile/doesn't quite meet your algorithm.

Good luck! I look forward to seeing your questions as you progress though the problem.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I would first say that a vowel is an a, e, i, o, or a u. So in psuedocode:

vowels=0 //We have zero vowels at the begining
if ("a", "e", "i", "o", or "u") //If it's a vowel
vowels++ //Increment the counter

And keep going till you reach the end of the String. The trick is how to code this.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Pauken wrote:Well, I would first say that a vowel is an a, e, i, o, or a u. So in psuedocode:

vowels=0 //We have zero vowels at the begining
if ("a", "e", "i", "o", or "u") //If it's a vowel
vowels++ //Increment the counter

And keep going till you reach the end of the String. The trick is how to code this.


This algorithm is good. I'd add a more explicit loop to your instructions:
vowels=0 //We have zero vowels at the beginning as that makes it easier to see what needs doing.
for each letter in the quote:
if ("a", "e", "i", "o", or "u") //If it's a vowel
vowels++ //Increment the counter

Next step is to translate each line of this code to Java. vowels++ is done already . And line 1 is really close.

Same deal. Try to translate the lines to Java and post what specifically you are stuck on. I'm sure you can translate line 1. For the if statement, look at charAt in the String class
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here's what I have for finding vowels.



For finding consonants, I think it's best to do a "If not a vowel", add them up.

>

I guess point "g" is next.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost. Look at the return statement. Right now it calls the method again. Which will go on until your computer runs out of memory. Instead you need to return the variable in which you are storing the count.

For consonants, your approach is fine. There's also a clever way of doing it. Think about the relationship between the length of the string, the # of vowels and the # of consonants.

And thanks for bearing with the questions/iterations/subtle clues. You'll learn more and become a better programmer that way than if I gave the full answer.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's what I have so far. I don't really know how to input a character. I just found what I have on line 4 on the internet. IF you could help me through that, that would be great. As for searching the String for the characters and adding them up, I'm a little stumped

 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the other way is "Add up all character - vowels = consonants"?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Pauken wrote:So the other way is "Add up all character - vowels = consonants"?


Right. It's less duplicate code which is more important on the job. And add up all the characters is just the length method so the whole thing becomes one line.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Pauken wrote:Here's what I have so far. I don't really know how to input a character.


If you are allowed to use Java 7, it's easy. Scanner.console().readLine(). That returns a String but you already know how to get the first character of the line out of a string.

If you aren't allowed to use Java 7, you'd need to use System.in and get a reader that way. I think you'd be allowed to use Java 7, so I'm going to skip looking it up. (It's amazing how fast one forgets the "old ways.")
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I'm missing something. I'm getting a "Method undefined for type Scanner" for console(). (I'm using Java 7 v 40).

 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. System.console().
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still getting the red Xs.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's should be a different reason now though. You have to choose whether to catch it or throw it.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry. I'm new to this. What is catching and throwing? What does this look like written out?
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I got this for inputting a character.



Any idea what might be going on in my driver? (Explanation in initial post.)>
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hadn't read the initial post in full (the driver part.) Knowing you have a scanner available, I agree that is better to use that. I'm not seeing how the numTimes method compiles (it doesn't appear to have the scanner passed to it.)

More importantly, I don't think that method is supposed to be inputting the character now that I reflect. I think it is supposed to just count instances of the character "what" in the quote. The driver seems to handle all input/output.

As far as the algorithm goes, this doesn't do the right thing. charAt takes the position in the string you care about. See your vowel method. And 'c' is a hard coded value. (That's the one I think needs to be "what")
if (quote.charAt(c)=='c')

I'm not sure what you are asking about the driver. Can you elaborate?
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like this?


For the driver, I'm supposed to create and instance of the String class and a Quote object in two different places. In both instances, I have to call the Strings and Quotes by the same name. I don't know how to accomplish this.>
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using nextLine after a previous nextLine call? If you call nextLine after nextAnythingElse, it may not work. You may in that instance suffer an out of bounds exception
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, at least I don't think so.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Closer. What I'm saying is that I don't think you need these two lines in that method at all.

String strType = scan.nextLine();
char c = strType.charAt(0);
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I found that and fixed it. Thanks!
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Pauken wrote:Yeah, I found that and fixed it. Thanks!



I know this post is a couple years old but thanks for posting. I had a similar issue with my code.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What was the problem? It is good to see that three‑year‑old threads are still useful.
 
Warren Lucero
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Similar issue to Sam's question. I believe I am taking the same course that Sam took a few years ago because the example was similar. Very new to the discussion board and java coding. I am stuck on another assignment. How do I post a question?


 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of checking for a, and e, and i, etc. in a loop, you might consider creating a String VOWELS="AEIOUaeiou" and investigating what methods of String exist to see if a string contains a character.
 
Warren Lucero
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it working. Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic