Monday, July 27, 2009

Is there any Function(s) in JAVA like ' scanf ' or ' gets ' in C? If any, explain with example?

DataInputStream will do the trick....in DIS class there's a method called readLine...which reads aline of input...this input is a string by default. u can convert the type using other built in functions available..

Is there any Function(s) in JAVA like ' scanf ' or ' gets ' in C? If any, explain with example?
i eat dirt.
Reply:For scanf: http://www.ibiblio.org/javafaq/javafaq.h...





Are you simply attempting to read standard input? If so look here:





http://www.ericsson.com/mobilityworld/su...
Reply:u can use readline function to input data or


else use DataInputStream for that
Reply:Hi,





As some people have already explained DataInputStream, I wont repeat that again. I personally feel that one of the best ways of accepting inout in Java Programs is by using a small graphical window that is formally called : JOptionPane.





Here's an example.. if you need to read an integer input, just say :





import javax.swing.JOptionPane;


String intInput = JOptionPane.showInutDialog ( null, "Enter an integer" , "Input",1);


int n = Integer.parseInt(intInput);





Explanation :





Statement 1 : We are mererly including the required class file to enable the use of JOptionPane.





Stateent 2 : The data entered in the JOptionPane is by default, returned as a string. So we just store the user-entered value in a string.





Statement 3 : However, we finally want to treat the user's entered value as an integer. So we use the parseInt function of the "Integer" class to convert the string into an integer. It's recommended that this statement is surrounded by try-catch blocks to catch any NumberFormatExceptions that are generated, in case the string cannot be converted to an integer (obviously.. the string could be of a form "Hiii" or "123.56" which cannot be converted to a valid integer).





Try it out.. hope it helps.. refer java tutorials on http://java.sun.com for fther details on the JOptionPane class.





Cheers
Reply:there is function like that but better ig you can pas arguments by commandline,it will be more easy
Reply:DataInputStream dis = new DataInputStream(System.in);


String st=dis.getString( );


No comments:

Post a Comment