Click to See Complete Forum and Search --> : help!!
September 22nd, 1999, 12:06 PM
Hello, can someone help? This is my first real java program and I don't know how to do it. Here it is.
Write a standard program that takes any number of command line parameters and displays the number of (naturally occurring) lower-case es in all of them, combined.
poochi
September 22nd, 1999, 01:19 PM
public class FirstClass{
public static void main( String str[] ){
for( int i = 0 ; i < str.length ; i++ ){
System.out.println( str[i] );
}
}
}
poochi
September 22nd, 1999, 01:20 PM
public class FirstClass{
public static void main( String str[] ){
for( int i = 0 ; i < str.length ; i++ ){
System.out.println( str[i] );
}
}
}
poochi
September 22nd, 1999, 01:23 PM
In my Sample program , str is an array , so change the System.out line according to that.
here i could not able to specify the array. I dont know why.
September 22nd, 1999, 03:46 PM
Hi, thanks for your help! But, what I am having difficulty is to count the lower case letter 'e's in the strings and print out. Would you be able to help? Thanks again.
poochi
September 22nd, 1999, 05:25 PM
I am sorry , i didnt read your question properly.
public class FirstClass{
public static void main( String str[] ){
int naCount = 0;
for( int i = 0 ; i < str.length ; i++ ){
int nIndex = -1;
String tempString = str[i].toLowerCase();
while(( nIndex = tempString.indexOf( 'e' , nIndex+1 )) != -1 ){
naCount++;
}
}
System.out.println( naCount );
}
}
Put the brackets( [] ) in app. places. ( 6th line )
September 23rd, 1999, 11:47 AM
Hi, Pooch! Thank you very much for your information. I will try to complie it and let you know how I did it. I have another question for you. Here it is.
If the first parameter of this program starts with the switch ‘-f’ then write the result of the program to the file following the switch. (e.g.: test -fhello.txt Hello // result should be written to the file hello.txt) Do you think this can be done?
Thanks again, my friend.
poochi
September 23rd, 1999, 01:08 PM
Yes you can.. Give me some time .. i will post the source code ..
September 23rd, 1999, 01:21 PM
Thanks, Pooch. You are the best.
poochi
September 23rd, 1999, 03:35 PM
I am not the best :-)
import java.io.*;
public class ReadSaveFile{
public static void main( String[] str ){
if( ( str.length < 1 ) || ( str[0].indexOf( "-f" ) == -1 )){
System.out.println( "Usage : java ReadSaveFile -f< FileName > < Strings ...>" );
return;
}
int nIndex = str[0].indexOf( "-f" );
String tFileName = str[0].substring( nIndex + 2 , str[0].length() );
try{
PrintWriter pWriter = new PrintWriter( new FileOutputStream( tFileName ));
for( int i = 1 ; i < str.length; i++ )
pWriter.println( str[i] );
pWriter.close();
}catch( IOException e ){}
}
}
Check the Array notations.. This forum eats some of the array brackets...
September 23rd, 1999, 03:49 PM
Thank you for all your help, pooch. I will try out your code. By the way, the other one was great help. Thanks again. Please keep in touch. Would you like to exchange email address?
poochi
September 23rd, 1999, 04:27 PM
What do you mean "the other one" ?
My mail id is : sankar_s_74@yahoo.com
you are always welcom.
September 24th, 1999, 09:29 AM
Hi, Pooch! I was refering to the FirstClass program that you wrote. It worked just fine. Thank you. But, the other one (ReadSaveFile) didn't save the correct result to the file. I got something like '[Ljava.lang.String;@f36eb39a' in the hello.txt file.
Let me give you an example, if you type 'test -fhello.txt Hello' at the command line, it should create a 'hello.txt' file and '1' should be written to the file. Because according to the FirstClass program, letter 'e' only occuring once in the text 'hello'. Do you know what I mean? Any help will be appreciated, Pooch. Thanks again.
poochi
September 24th, 1999, 10:14 AM
Yes i can understand what you are saying ... This forum is eating some brackets in our code.
It seems u are writing the whole str array instead of each and every values in that array.
Check this line..
pWriter.println( str ) in my code ..
The array brackets are missing ... I has to be 'str[]' , and put 'i' inside the brackets.
I could not able to do that. It might be a bug of this forum.
September 24th, 1999, 11:22 AM
Great, it worked!!! I added [] in the FirstClass program, but forgot to add it on this one. Thanks very much. Can we do one more thing? This will be a good excersise for other beginners as well. Here it is.
Now change this program to accept the switch '-o<ClassName>' on command line. The -o will accept the name of a Java class creates an instance of that class if it has no-args constructor. The program converts the object to a String object and displays the result of conversion to users. (if -f switch specified the result is written to a file)
How's this question? Also, what would you recommand as a good book to read about string and string buffer? I looked in Baldwin's tutorial, but it doesn't say much. You have a great weekend, Pooch. Thanks again.
poochi
September 24th, 1999, 12:11 PM
Sure we can.. But you have to wait till the evening.. I got someother work to do now.
i will post the source code in the evening.
:-) pooch ? That's a nick name... "Poochi" = "insect" in English. :-)
Have a great weekend ..
September 24th, 1999, 03:45 PM
really? I thought 'Pooch'='Dog'. :) I know Pooch is your nickname, thanks to you I have a better understanding of file i/o now. Thanks. blue
September 25th, 1999, 03:29 PM
Hi, pooch! Any word of advise on this problem? Thanks.
Blue
poochi
September 25th, 1999, 09:38 PM
> accept the switch '-o<ClassName>' on command line. The -o will accept
> the name of a Java class creates an instance of that class if it has no-args constructor. The
> program converts the object to a String object and displays the result of conversion to users.
> (if -f switch specified the result is written to a file)
This is a very simple one ... Get the class name from the user as you get the file name ..
Then do like this ..
String className = <get The className from the Commandline argument>
Object obj = Class.forName( className ).newInstance();
System.out.println( obj.toString());
// Here is that class
class TestClass{
String strSomeValue;
public TestClass(){
strSomeValue = "junk";
}
public String toString(){
return strSomeValue;
}
}
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.