• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner purpose  RSS feed

 
Jonathan Vazquez
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm stuck on java. I'm a beginner and find java more confusing than C++ or other languages anyways. I'm curious as to how we implement a user input. I heard an read user input is "Scanner input = new Scanner(System.ini);" however how can I implement this on a calculator? My purpose is to add a calculator were the user can input an integer and have it stored on a variable and be able to input whether he/she wants to add/sub/mul/ or divided then again input his/her second integer then result the answer.

How can I implement this in a simple class? Thank you
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You ought not to try some many things all at once. The Scanner class was introduced in 2004 (Java5) probably because earlier versions had really awkward methods for getting input from the keyboard. Those of us who remember JDK1.4 and earlier wrote utility classes to permit keyboard input. I suggest you go to the Scanner documentation, where you will find an example of reading from the keyboard. I suggest you run the code you can see there (first example), followed by this sort of line:-
System.out.printf("You just entered %d.%n", i);
Only when you have got used to that, and tried some of the other methods of Scanner should you start using it in real programming.

Beware: there is a pitfall associated with the nextLine() method. You can read about it here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!