wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes At the main method create a single dimension array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "At the main method create a single dimension array" Watch "At the main method create a single dimension array" New topic
Author

At the main method create a single dimension array

William Castillo
Greenhorn

Joined: Jun 12, 2012
Posts: 8
I NEED HELP!!!
I am currently working on a program for my Java class but I quite don't understand it. These are the instructions::::

"At the main method, create a single dimension array containing your "first name $ middle name % last name and ONE space" i.e. "George & Washington % Rodriguez "
NOTE: Every method dealing with your name, MUST be called from main, PASSING your name to it.
Using the for loop, if, else if, else, ||, and &&, printf, and any other command you want,
examine each character of your name and determine if it is:
a space, a vowel (a,e,i,o,u), a consonant (b,w,g, etc) the symbol % or the symbol $,
and using the current value of x in your for loop print:
"character [x] located at position x is a consonant" or
"character [x] located at position x is a vowel" or
"character [x] located at position x is a space" or
"character [x] located at position x is a not a vowel or a consonant or a space".
ie: My name is [George $ Washington % Rodriguez]
character [G] located at position 0 is a consonant
character [e] located at position 1 is a vowel
character [o] located at position 2 is a vowel
character [r] located at position 3 is a consonant
character [g] located at position 4 is a consonant
character [e] located at position 5 is a vowel
character [ ] located at position 6 is a space
character [$] located at position 7 is not a vowel or a consonant or a space
character [ ] located at position 8 is a space
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 860

Welcome..

First off:

"first name $ middle name % last name and ONE space" i.e. "George & Washington % Rodriguez "

So if middle name does not exist, no $ delimiter? (Look for clues like that).

Secondly:

TRY IT!

Put together your first attempt, analyze the requirements and story board it.

No one is going to do it for you.


WP
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Nice exercise,
start by writing the class and the main method. Inside of it create a single dimension array, containing the String required.
Show what you have done. Then we can go on.



Bye,
Nicola
Avinash Oak
Greenhorn

Joined: Jun 12, 2012
Posts: 15

william : go ahead,
Show us some code, then we debug.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

EVERY single time I start writing java, I start by creating my class with a main method that prints "hello, Fred". it's seven lines - and i put my braces on lines by themselves.

I compile that, run it, and make sure it works. THEN i'd start with the next piece:

At the main method, create a single dimension array containing <something that looks like a string>

I wouldn't even worry about the specific stat in it at this point, I'd just try and declare it and put SOMETHING in it. I may even write a line of code to print out "Hey, I created the array" - knowing that I will delete that line of code later.

Then I'd add the next piece, compile and test it, etc.

I never never never write more than 5 lines of code without compiling and testing...and ideally only one or two.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
William Castillo
Greenhorn

Joined: Jun 12, 2012
Posts: 8
This is what I got so far:
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1752
    
    7

Hi William, and welcome to JavaRanch!
I've added code tags to the source code you posted to make things a bit more readable.
When you post a topic or reply containing source code - Java or otherwise - you can add these using the Code button.
Also, it's very helpful to post code snippets that compile and run, if at all possible.
For instance, in this case you've posted the code for the main() method and a method called myName(), but there's no surrounding class.
Now this may have been intentional, but it's better to include those bits of code, because some people might not pick up on that fact.
It just saves on confusion and gets you helpful replies more quickly.




Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
William Castillo
Greenhorn

Joined: Jun 12, 2012
Posts: 8
Ok, I got this so far. But what I'm trying to accomplish is this:
character [W] located at position 0 is a consonant
character [i] located at position 1 is a vowel
character [l] located at position 2 is a consonant

I don't need the program done for me, I just need to understand the structure. Any sugestions?

Thank you.

William Castillo
Greenhorn

Joined: Jun 12, 2012
Posts: 8
Thanks for the tip on using the Code button Jelle. Learn something new everyday. Thanks to every one for your advices.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1752
    
    7

You seem to be unclear on some of the basics, which is why I wouldn't recommend that you rush into this assignment just yet.
Take Fred's advice and start with a small application that compiles, runs and behaves as you expect it to.
If it does expand it - slowly, and make sure it keeps on compiling, running and behaving.
If it doesn't, well then you have something very specific you can ask us to help you with.

Just to get you started I've modified your code a bit and created a simple program that just prints a hello world message in several ways.
It covers some basic points that will be relevant to your program, like printing to the console, creating and accessing arrays, a basic for-loop and defining and invoking your own method.
William Castillo
Greenhorn

Joined: Jun 12, 2012
Posts: 8
Thank you Jelle. I'll try that and move on little by little. I'll let you know how it goes.

Once again, thanks for the help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
William Castillo wrote:Thanks for the tip on using the Code button Jelle. Learn something new everyday. Thanks to every one for your advices.
You’re welcome. You should also avoid long lines (evry link will give you different figures for exactly how long). I have changed your long line, which was quite easy because of the + operators, and you can see how it is done. It can be very difficult to read long lines.
William Castillo
Greenhorn

Joined: Jun 12, 2012
Posts: 8
Ok I'm moving forward, but I'm getting an error in "myName.Length();" and "myName.charAt(x);". I just don't know how to get rid of this error, so I can run the program. Any body have any suggestion?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
There's something i don't get

The first vowel " + Letter + " located at position " + x + "is a consonant."


Are you looking for a vowel or a consonant?

When do you call the method (poorly) named Name?


Anyway arrays don't have a method called Length() nor a method called charAt() but a variable called length


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
What Nicola Garofalo is saying is that this particular problem might be sorted out if you got rid of the [] in the type of the parameter for the Name method. I agree Name is a poor name for that method.
William Castillo
Greenhorn

Joined: Jun 12, 2012
Posts: 8
Thank you for your help. I made the changes you guys recommended and it got rid of all my errors, except when I declaring "Organizer();" in my main method. I don't know if I'm missing something inside the () or what.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
If you are calling that method, you need to pass it a String (not a String[]). Pass the String (myName) to it in the () where you call it. Why are you creating a String[] in the first place? You don’t appear to use it.
Unless you want a command-line argument, which you can find details of here and here. In that case you would use args rather than creating a new array.

I am afraid Organiser is no better a name for your method.
William Castillo
Greenhorn

Joined: Jun 12, 2012
Posts: 8
I figure it out. I needed to put "Organizer(myName);". I was putting "Organizer();".

Thank you so much for all your help guys. This is my first time looking for help using a posting website. This one rocks!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
William Castillo wrote: . . . Thank you . . . a posting website. This one rocks!!!
You’re welcome and thank you, too
 
Consider Paul's rocket mass heater.
 
subject: At the main method create a single dimension array
 
Similar Threads
Can someone test my game again?
charAt used on arraylist
Sun Cirtification
JOptionPane.showInputDialog problem
why can't some people play my game