![]() |
It doesn't look like there is an easy way
In C++ you can output to the screen using cout and get user input by using cin. It's very easy and quick. For example this program prompts the user to enter an integer and then displays it back:
int main( ) { int number; cout << "Enter an integer"; cin >> number; cout << "You entered " << number << endl; return 0; } Simple. Now I'm looking for a way to do the same thing in Java. I know how to output a message. Just type System.out.println("You entered "); But how do you get input? I'm not talking about using dialog boxes(GUI components). A book I have shows how to do that. But I'm talking about how to get input from the console. From what I've turned up so far it looks too complicated for me to understand. After all I'm at the beginning and only done some C++ console programming. If you can show some code in order to do this that would be great. |
:
import java.util.*;http://java.sun.com/j2se/1.5.0/docs/...l/Scanner.html ^^ for more information on Scanner. |
I have a SimpleInput wrapper class laying around somewhere... I'll try to find it tomorrow.
|
solved (but not simple)
yeah, this is one of those cases where java is kind of a pain in the ass compared to C/C++. usually you have to instantiate a buffered reader then read in the lines and then convert them to integers or whatever you want. the other guys probbaly know more and may have a simpler conclusion.
:
String inData;then it takes the user input and puts it into the string variable inData. then inData is converted into an "int" and placed into the value x. then x is printed. this may be slightly off because i pulled code snippets from my java calculator which you can find on this forum and also because i don't remember a lick of java. oh, and add the header files and shit. |
Well I found a piece of code on the web about how to do exactly what I was looking for. But there are only a few lines I understand. When I think about it I'm really putting the cart before the horse here. You see, my only programming experience has been console programming with C++ and that was about 4 years ago. I studied it for about maybe 6 or 7 months and then stopped due to problems with depression.
My impressions with Java tells me that in order to understand a program like this you first must have studied about classes and objects and data members and what it means to create an instance of the class and so on. I think I almost got to that chapter when I stopped my study in C++. That's what I like about C++. You can start with just learning the constructs and syntax of the language. You can study that for months before ever learning about objects and classes. But it appears in Java your up against objects and classes right off the bat, so I am dubious about whether I would recommend any beginner to start with Java. Personally I think C++ is a better place to start. The reason I'm so confused is because I don't really know about objects and classes and creating an instance of the class and data members and all that OOP stuff. I may have a faint memory of it but that is all. So this program that I wanted to write and found some code for I'm not going to understand since it uses more advanced concepts which I am not familiar with yet. |
you are exactly correct. java forces you into the OOP world whearas c/C++ does not. however, if you intend to learn those concepts, you still need to learn them, regardless of the language. after you read through it 30 times or so it will begin to make sense...i promise you.
|
Some change to the code
One small...change....to the post made by bl00dninja ...
just one important thing.... :
try{You actually need to check wherther user inputs are appropriate. In java, it gives u a these try....catch blocks where u can catch an input error or any kind of error and display appropriate actions. However, in C++, u wud need to write 'if and else' kinda statements.... now what does try and catch do? the try block is where all inputs are received...all your data entry work done in the try block... whenever u make an error in input..the catch block wud immediately catch (hence the name!) and display the error message.... e.g.. say u ask the user to input a integer and the user enters a float, the try block indicates this value to the catch and the message stating the error is printed... If more queries...feel free to ask.... there's a lot more to taking inputs in java....a very widely accepted and wide..wide..wide language....!! |
| All times are GMT -5. The time now is 4:49 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC