cyph3r 0 Report post Posted October 19, 2005 (edited) Heya guys,Been a looooong time eh, yeah well i was kinda buzy with college stuff and all those assignments just keep pouring in. I have java this semester and i have "NO" idea how to go abt it, 1 reason being that my proff. is going real fast in the explaination part and i doubt she knows anything in java Newayz , i was asked to do a small program in java , heres how it goes :I need to enter a word prefixed by the number of letters in it, and the output should show me the smallest character in that word.eg: 5Hello .... Smallest character is : eWe were started off in studio .net and i have again no idea of how that works .. but playing around a bit made me get used to a few controls. This is how i started off , but i duno how to continue , could someone please point out any mistake and give me any ideas or better off , just give me ideas and not the whole program so i would try it myself , i am really bad at logic i know that suc*s .. but err .. i need to pass out oof this subject and get rid of this annoying proff. i have ... package smallchar;import java.io.*;import java.awt.*;/** * Summary description for Class1. */public class Class1{ public Class1() { // // TODO: Add Constructor Logic here // } /** @attribute System.STAThread() */ public static void main(String[] args) throws IOException { DataInputStream n=new DataInputStream(System.in); String ch; String a; char small; int p; System.out.println("Enter The String After Entering The Number Of Characters"); ch=n.readLine(); int i; a=ch.substring(0,1); p=Integer.parseInt(a); small=ch.charAt(1); for (i=2;i<p;i++) ...... How do i proceed with the for loop ? i really have no idea .. Thankx in advanceDhanesh Edited October 21, 2005 by microscopic^earthling (see edit history) Share this post Link to post Share on other sites
miCRoSCoPiC^eaRthLinG 0 Report post Posted October 21, 2005 You were on the right track but not quite. First of all you really don't need the package statement, unless you want to classify your code in a separate namespace. But anyway, no harm with that. Besides if you're using Visual Studio.NET, this will be added automatically. Also you don't need to import java.awt - that's the Abstract Windowing Toolkit - which you need only when you are designing Windows based Appz with a GUI. package smallchar;import java.io.*;import java.awt.*; This part is fine... although you might consider naming your class something meaningful, instead of simply Class1 /** * Summary description for Class1. */public class Class1{ public Class1() { // // TODO: Add Constructor Logic here // } You don't really need to add that throws IOException to your main - it will do so in any case. /** @attribute System.STAThread() */ public static void main(String[] args) throws IOException { The part below this is really messed up. You can use DataInputStream to read in your line, but I'd advise you to use a subroutine that I use to fetch lines for me.. works like a charm.Here it goes: public String readLine( ) { char nextChar; String result = ""; boolean done = false; while (!done) { nextChar = readChar( ); if (nextChar == '\n') done = true; else if (nextChar == '\r') { //Do nothing. //Next loop iteration will detect '\n'. } else result = result + nextChar; } return result; } ============================================= String ch; String a; char small; int p; System.out.println("Enter The String After Entering The Number Of Characters"); ch=n.readLine(); int i; a=ch.substring(0,1); p=Integer.parseInt(a); small=ch.charAt(1); for (i=2;i<p;i++)[/code] ============================================= As for this messed up part... I wouldn't go into rectifying it - but rather am providing you with a full sample program that would do what you're trying to do. Full Sample package LowestChar;import java.io.*;import java.util.*;/** * Summary description for Class1. */public class LowestChar{ public LowestChar() { // // TODO: Add Constructor Logic here // } /** @attribute System.STAThread() */ public static void main(String[] args) { // Variables String line; int Lowest = 128, Ascii = -1; // To keep the compiler happy // Prompt System.out.println ("Enter a line of text: "); // Read Line line = readLine (); // Loop through each character of the string and find which is the lowest for ( int Count = 0; Count < line.length(); Count ++ ) { // This is the step that gets you the lowest value // Every time Count increment, a new character is taken into consideration // String.charAt (Position) gives you the particular character at that position of the string // Type casting it to 'int', by: (int) character - gives you the Ascii value of that character // You store that in Ascii and compare to lowest. If it is lower than the lowest value, make Lowest // equal to that character and loop again. Ascii = (int) line.charAt(Count); if ( Ascii < Lowest ) Lowest = Ascii; } // Print out the lowest char by type casting the integer value back to char System.out.println ( "Lowest Character: " + (char) Lowest ); } // Method to read in a line of text public static String readLine( ) { char nextChar; String result = ""; boolean done = false; // Loop till enter/newline character is met while (!done) { nextChar = readChar( ); if (nextChar == '\n') done = true; else if (nextChar == '\r') { //Do nothing. //Next loop iteration will detect '\n'. } else result = result + nextChar; } // Return the string return result; } // end readLine // Method to read a Character public static char readChar( ) { int charAsInt = -1; // Keep the compiler happy try { // Read the character as integer charAsInt = System.in.read( ); } catch(IOException e) { System.out.println(e.getMessage( )); System.out.println("Fatal error. Ending program."); System.exit(0); } // Return the character return (char)charAsInt; } // end readChar} // end class NOTE: Keep in mind that this wouldn't return the lowest char as you've shown. The lowest char is actually the character with the lowest ASCII value. The lowest values will be that of Numbers followed by CAPITAL Alphabet and then small Alphabet. This should help you out in what you are trying to achieve. Feel free to modify the code to suit your needs. Share this post Link to post Share on other sites
cyph3r 0 Report post Posted October 23, 2005 First off ... Welcome Back ! Had a nice vacation i see .. heheDo u NOT know anything ? Jeez I least expected you to reply , and wow what a timing. Thankx a bunch for the quick reply.About the code, well i gave ur code a nice look and tried doing the thing on my own, well it took some time to get it right, but finally came up with the solution, i checked up with my proff at college ( belive me i hate that woman to the core ) , she had like a million problems with it, its like " I dont want u to do it the easy way and use pre frunctions .. write your own and do it " HELL !Newayz , this was the final outcome /** @attribute System.STAThread() */ public static void main(String[] args)throws IOException { DataInputStream n=new DataInputStream(System.in); String ch; String l; String a; char small; int p; System.out.println("Enter The String After Entering The Number Of Characters"); ch=n.readLine(); int i; a=ch.substring(0,1); p=Integer.parseInt(a); small=ch.charAt(1); for (i=2;i<p;i++) { if(small>ch.charAt(i)) small=ch.charAt(i); } System.out.println("\n Smallest Character"); System.out.println(small); l=n.readLine(); }}I got an idea from ur code and used it in the one i did, hope that isnt a problem. The only thing bothering me is that .. what is "throws IOException" i know its one of the components of import java.io, but in this case as u said it will do it any ways .. How does that happen ? I mean dont we have to define every bit of code supporter while programming ?Please pardon me for being so pestering , but i really duno this subject that well , i mean javascript was fine , it was fun and cool , but this is just crazy, either i need a good teacher or i need a good book. I rather consider myself to be a Hardware Freek than a software one.Thankx for the Reply and i guess m done with this bit of program just working on the palindrome program now .. ughhRegardsDhanesh Share this post Link to post Share on other sites
evought 0 Report post Posted October 23, 2005 <snip...> I got an idea from ur code and used it in the one i did, hope that isnt a problem. The only thing bothering me is that .. what is "throws IOException" i know its one of the components of import java.io, but in this case as u said it will do it any ways .. How does that happen ? I mean dont we have to define every bit of code supporter while programming ? Please pardon me for being so pestering , but i really duno this subject that well , i mean javascript was fine , it was fun and cool , but this is just crazy, either i need a good teacher or i need a good book. I rather consider myself to be a Hardware Freek than a software one. Thankx for the Reply and i guess m done with this bit of program just working on the palindrome program now .. ughh Regards Dhanesh 1064327262[/snapback] "throws IOException" tells the compiler that the method is allowed to throw an Exception derived from IOException to indicate a problem. When an exception is thrown, it travels up the stack, back through all the methods that have been called until it reaches a "catch" statement which handles it. If there is no 'catch', the exception goes all the way up to main and exits the program. The reason you do not need it in your case is that the method you were adding it to *was* main. There is no where else for the exception to go, so do not bother declaring it. If an exception is thrown in main, it will jsut exit the program anyway. If you wanted to handle the error in some way, like printing put a message or giving the user another chance, you would put the read calls inside a try...catch block. Share this post Link to post Share on other sites
cyph3r 0 Report post Posted October 24, 2005 "throws IOException" tells the compiler that the method is allowed to throw an Exception derived from IOException to indicate a problem. When an exception is thrown, it travels up the stack, back through all the methods that have been called until it reaches a "catch" statement which handles it. If there is no 'catch', the exception goes all the way up to main and exits the program. The reason you do not need it in your case is that the method you were adding it to *was* main. There is no where else for the exception to go, so do not bother declaring it. If an exception is thrown in main, it will jsut exit the program anyway. If you wanted to handle the error in some way, like printing put a message or giving the user another chance, you would put the read calls inside a try...catch block. 1064327279[/snapback] Hey evought .. thankx for the explaination , kinda understood what it really does , wished i had a better proffessor to teach .. newayz have to live with it tho .. I need to pass out of this semester ! Sheesh .. i sound desperate ! Share this post Link to post Share on other sites
miCRoSCoPiC^eaRthLinG 0 Report post Posted October 25, 2005 No problem at all - that's why I told you, that I'll give you a basic working code - just modify it to suit your needs. Also good explanation of the IOException, evought. Couldn't have done it better myself. Dhanesh - it's like speaking in english terms, Exceptions are thrown, when something exceptional occurs - i.e. something out of the ordinary, an event that isn't supposed to happen during the normal flow of your code. To handle such scenarios we use the exception catchers in the form of a try..catch.. block. Exception itself is a base class, sort of a mother of all exception classes, which is thrown when some error occurs. When you write code to deal with a particular type of exception, you usually inherit the exception class and modify it to suit your needs. When it comes to your void main() - it is already pre-taught to throw the IOException. That is why, it is not really necessary to add the code to it - although it doesn't do any harm. If it was for some other sub-routine, then yeah, of course, you'd have to add it, to inform the subroutine that it is to throw a particular type of exception when a problem occurs. If you face any more tough java nuts to crack, post 'em all here. Haven't touched Java in a long while - would be fun again brushing up my skills Regards, m^e Share this post Link to post Share on other sites
BitShift 0 Report post Posted May 9, 2006 My computer teacher told me that although "throwing" and exception is handy, its a bad idea.He said always use the try..catch blocktry{//code here}catch(Exception exc){} Share this post Link to post Share on other sites
iGuest 3 Report post Posted August 27, 2008 character Need Help: Find Lowest Character Using Java Do you NOT know anything ? Jeez I least expected you to reply , and wow what a timing. Thankx a bunch for the quick reply. About the code, well I gave your code a nice look and tried doing the thing on my own, well it took some time to get it right, but finally came up with the solution, I checked up with my proff at college ( belive me I hate that woman to the core ) , she had like a million problems with it, its like " I don't want you to do it the easy way and use pre frunctions .. Write your own and do it " HELL ! Newayz , this was the final outcome /** @attribute System.STAThread() */ Public static void main(String[] args)throws IOException { DataInputStream and=new DataInputStream(System.In); String ch; String l; String a; Char small; Int p; System.Out.Println("Enter The String After Entering The Number Of Characters"); Ch=and.ReadLine(); Int I; A=ch.Substring(0,1); P=Integer.ParseInt(a); Small=ch.CharAt(1); For (I=2;I<p;I++) { If(small>ch.CharAt(I)) Small=ch.CharAt(I); } System.Out.Println("and Smallest Character"); System.Out.Println(small); L=and.ReadLine(); } } I got an idea from your code and used it in the one I did, hope that isnt a problem. The only thing bothering me is that .. What is "throws IOException" I know its one of the components of import java.Io, but in this case as you said it will do it any ways .. How does that happen ? I mean don't we have to define every bit of code supporter while programming ? Please pardon me for being so pestering , but I really duno this subject that well , I mean javascript was fine , it was fun and cool , but this is just crazy, either I need a good teacher or I need a good book. I rather consider myself to be a Hardware Freek than a software one. Thankx for the Reply and I guess m done with this bit of program just working on the palindrome program now .. Ughh Regards Kuljeet -reply by kuljeet Share this post Link to post Share on other sites