wood burning stoves*
The moose likes Beginning Java and the fly likes Enigma! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enigma!" Watch "Enigma!" New topic
Author

Enigma!

JC Bismark
Greenhorn

Joined: Jun 27, 2008
Posts: 29
Hi everyone,

I need some help to solve this exercice:

create a console application that accepts exactly one command-line argument. If it doesn�t receive the argument, the application must display an error message and exit. The application must parse the text input and output the number of times each letter of the alphabet occurs in the text. Case sensitivity is not required.

For example, if the command-line argument is �baaad� the displayed result must be:

There are 3 A's
There are 1 B's
There are 0 C's
There are 1 D's
There are 0 E's
There are 0 F's
etc...


Thanks in advance for your guidance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

Homework!

What have you got so far?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
JC Bismark
Greenhorn

Joined: Jun 27, 2008
Posts: 29
I was confused on how to start but I think it will be like this:

public class Torrin {
String arg1;
while( arg1!= null){
if (arg1 = null) {
System.out.println("error message");
break;
}
// I don't know how to formulate the parse method here and output the number of times
the letter occurs in the text?
}
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Originally posted by JC Bismark:
[QB]I was confused on how to start but I think it will be like this:



This much won't compile. You might start by fixing that much. It also appears that your logic is saying
if arg1 is null while arg1 is not null ... I hope I'm misunderstanding something.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
JC Bismark
Greenhorn

Joined: Jun 27, 2008
Posts: 29
Thanks Marilyn,I'm a beginner and I try to find my way to this exo...

public class Torrin {
protected String arg1;
protected String txtinput;
int alphabletcount ++ ;
if( arg1= null){

System.out.println("error message");
break;
}else{

System.out.println(String txtinput);
}
}

public static parseint(String txtinput){

this.txtinput= txtinput;

// please,someone help me to find the next step?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Please don't use thread titles like "enigma;" read this.

Have they told you how to use command-line arguments in the first place? Remember command-line arguments go to the main method. I tried googling and found (as usual) something useful in the Java� Tutorials. Now work out how to get "baaad" from the command line to your main method; create a class which prints
Your 1st command-line argument was baaad
or
Please enter a command-line argument
Then change the printout to the conventional error message, which reads something like:
"Usage: java Torrin <word>"
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Hi All,

I tried to do the above exercise and was successful in it.
I am working as a struts developer and I am able to solve all the problems that I have been given in core java.
But I am not sure if it is done in the right or the best way (especially oops concepts and performance).

Can you please verify the code and tell me how I can improve my coding.....



SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Just one thing to point out which is subtle but will drive you crazy if you don't realise:won't compile in Java. The reason is you're doing an assignment and not a comparison: arg1=null means "assign arg1 the value null" and since assignments as a whole also evaluate to their RHS, this expression will be null. Hence the statement above is equivalent to:which is obviously nonsense.

However, I mention it because in other languages, like C/C++, this is a legal statement. Anything which evaluates to a zero value is considered false; null is the zero pointer. Hence if(null) is equivalent to if(false), not what you intended but perfectly legal. This is a trap to watch out for, not in Java so much (as the compiler will spot your assignment isn't boolean), but in other languages. Better to learn from that common mistake now than use it in more dangerous places!


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
But I am not sure if it is done in the right or the best way (especially oops concepts and performance).
To be honest, there really isn't a need to use OO here at all. It's pretty much a linear one-method job to parse the arguments, so you can keep it all static (do it all in main really). OOP comes into its own when you start constructing complicated inheritance models, not simple procedural programs.

I can suggest a couple of improvements:I haven't tested this, but I think it will do the job (post back with a correction if it doesn't work!). I made lots of savings in loops by noting that unicode 65 corresponds to A and 90 to Z, so simple arithmetic will do the rest - we don't need to loop. Also char can be implicitly upcast to int for the comparison. I'll leave you to understand how that works and fit all the rest together.
[ June 30, 2008: Message edited by: Charles Lyons ]
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Can you please verify the code and tell me how I can improve my coding.
And I forgot to mention above that you should conform to the Code Conventions for the Java Programming Language as it will help make your code more readable by others (it also saves lines by not putting every { on separate lines).
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
A simple HashMap will help you to count the ocurrences of each letter.


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
A simple HashMap will help you to count the ocurrences [sic] of each letter.
I don't follow - what sort of design are you thinking about?
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Well, create an hashmap and fill it with the alphabet letters (they'll be the keys) and the values (the counter) will be iniciated to zero. Then, parse the input text and increase the counter on each letter found.I'm not sure it works. I�ts just a thought.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Ah I see. Yes that would work, except everything needs to be an (immutable) object. So you'd need Character as the key rather than char, and Integer as the value rather than int. Then for each character in the input you'd be boxing char to Character for comparison with the keys, unboxing Integer to int, incrementing that result, then re-boxing back again and setting that value back in the map (since Integers are immutable). So that's a lot of overheads for a simple set of counters.

Of course, your method is more robust - you can easily account for any characters in the input, not just the expected letters, numbers etc. The price of this flexibility is a performance impact for simple cases (as is usually the case). Which to choose all depends on what you want to do really.
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Just a small sample. I leave to the OP the interpretation:

which gives the results:

[ June 30, 2008: Message edited by: Manuel Leiria ]
JC Bismark
Greenhorn

Joined: Jun 27, 2008
Posts: 29
Thank you so much for your input guys,I can see that there is several paths to solve the exrecice.I learn so much on this network...
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Originally posted by JC Bismark:
Thank you so much for your input guys,I can see that there is several paths to solve the exrecice.I learn so much on this network...


Glad to help!

Cheers,
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Out of total interest, and too much free time , I ran the programs written by me (int[]) and Manuel Leiria (HashMap). For interest I also ported the int[] code to C++ and compiled using g++. All were running on the same computer (Dual Core Pentium T2080, 1733MHz). Here are the average execution times over 10 executions on the same sample data as provided by Manuel:

HashMap: 1298354ns = 1.3ms
int[]: 370249ns = 0.37ms
C++: 39338 cycles = 0.023ms

So the int[] method, avoiding all the (un)boxing, is about four times faster on this sample data in Java, and ten times faster than that in native code. Of course, purely academic on a scale of under 1ms for user input, but I like to bear performance in mind.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Originally posted by Charles Lyons:
And I forgot to mention above that you should conform to the Code Conventions for the Java Programming Language as it will help make your code more readable by others (it also saves lines by not putting every { on separate lines).

I think he should use the Style Guide his employer prefers. Some employers don't use Sun's Style Guide. Alternatives are available ... such as the JavaRanch Style Guide which espouses the original poster's style of curly braces.
Robin Lane
Ranch Hand

Joined: Apr 30, 2008
Posts: 76
Charles, just a quick "Thank You!" for the following in one of your posts above:

And I forgot to mention above that you should conform to the Code Conventions for the Java Programming Language as it will help make your code more readable by others...


I wish the Code had been required reading in my Java class! As it was, it was never even mentioned. :roll:

However, not putting {}s on seperate lines detracts from readability for beginners like myself. I put them on seperate lines, as well, just because it makes it easier for me to see them, since I am such a novice. I'm looking forward to the day that it is no longer necessary. (bright smile)

And, Marilyn, thank You for your input, too! I'm on my way to viewing that link right after I post this.
[ June 30, 2008: Message edited by: Robin Lane ]

Disclaimer: The author of this post makes no claims, implied or otherwise, as to her ability to understand anything but the simplest of answers. Further, the author of this post has no programming background whatsoever and has taken one Java Programming class which was 9 weeks long. She does want to learn, so please teach patiently.
JC Bismark
Greenhorn

Joined: Jun 27, 2008
Posts: 29
Tank you Marilyn for the link.It's helpful.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Thank you ALL !!!

Learnt a lot in this discussion !
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Thank you ALL !!!

Learnt a lot in this discussion !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enigma!