• 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

Enigma!

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Homework!

What have you got so far?
 
JC Bismark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
}
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
JC Bismark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>"
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.....

 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple HashMap will help you to count the ocurrences of each letter.
 
Charles Lyons
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
JC Bismark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tank you Marilyn for the link.It's helpful.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ALL !!!

Learnt a lot in this discussion !
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you ALL !!!

Learnt a lot in this discussion !
 
reply
    Bookmark Topic Watch Topic
  • New Topic