473,668 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scanner method: hasNext()

94 New Member
Hi, everyone. I'm new to the forum, and I would to ask you how does the following program works. It uses Scanner method hasNext(), and a sample input/output is shown after the code.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class test_while_with_scanner_method_has_next
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner dataReceiver = new Scanner(System.in);
  8.         int sum = 0;
  9.         int value;
  10.  
  11.         while ( dataReceiver.hasNext() )
  12.         {
  13.             System.out.printf("Enter a value: ");
  14.             value = dataReceiver.nextInt();
  15.             sum += value;
  16.         }
  17.  
  18.         System.out.printf("The sum is %d.", sum);
  19.     }
  20. }
Sample input/output at the command prompt, as soon as the program starts with command java test_while_with _scanner_method _has_next:

1
Enter a value: 3
Enter a value: 4
Enter a value: ^Z
The sum is 8.


^Z is the keystroke combination for indicating end-of-file. How is it possible that the first integer entered, 1, is included in the sum?

Thanks in advance! =]
Apr 17 '13 #1
1 1907
Rabbit
12,516 Recognized Expert Moderator MVP
Because the nextInt read in the 1.
Apr 18 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

18
2536
by: Wade Leftwich | last post by:
Every couple of months I have a use for the experimental 'scanner' object in the re module, and when I do, as I did this morning, it's really handy. So if anyone is counting votes for making it a standard part of the module, here's my vote: +1 -- Wade Leftwich Ithaca, NY
9
4548
by: Dan =o\) | last post by:
Hey guys, I wonder if you could please provide me with some ideas as to how to get around this problem. Symbol MC9000-k, Pocket PC 2003... With a scanner application I've written, data is loaded into ArrayList structures when the user performs some action. This list contains a list of barcode numbers that are currently accepted. Finally, there is a manual entry button in my application where the user can
7
25916
by: DemonWasp | last post by:
I've been having some trouble getting the Scanner class to operate the way I'd like. I'm doing some fairly basic file IO and I can't seem to get the class to load the last line/token any way I try. First, I tried using the following: File InFile=new File("input.txt"); Scanner In=new Scanner(InFile);
15
1525
by: djtosh | last post by:
hi there hoping someone can help me out with this. im designing a code that is a basic database holding peoples details and i need to implement a method that searches by txt for a record by their last name. import java.util.*; import java.io.PrintStream; import java.io.File; import java.io.FileNotFoundException; public class coursework4
6
9933
by: ibo3 | last post by:
/** I have to use the scanner class (no alternatives), to record a dynamic amount of grades. I use the hasNext() method to get the next value on the same line. For some reason when I am done entering numbers and I press enter the curser waites on the next line and does not exit the program. I want to be able to exit the program once I have entered multable grades and then pressed enter. */ public void enterGrades() { Scanner scan...
2
23578
by: CRGoose | last post by:
For my class i have to write a program that has a method to combine integer time input values into a single double value, return that to a method that sums the values, then return that to a main. The method names i have written have to be exactly as they are shown, and i cannot for the life of me figure out how to call the "public static double RouteTotalOperationalTime(Scanner input)" method into my main. If someone can explain to me how to...
1
6487
by: madman228 | last post by:
Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial in which i need a derivative method I have everything, just dont know how to start the derivative method. Any help will be appriciated here is the code: import java.io.*; import java.util.Scanner; public class Polynomial extends UnorderedArrayList { Scanner sc = new Scanner(System.in); //Default constructor
2
6580
by: milk242 | last post by:
import java.util.Scanner; public class DogDemo { public static void main(String args) { Dog myDog = new Dog; Scanner keyboard = new Scanner(System.in); String name, breed; int age; boolean boost;
5
1898
Jezternz
by: Jezternz | last post by:
Ok, basicly I have created a Class. public class ClassX{ public ShapeObj(){} public doThis(){} } And I have created a Vector and filled it with instances of this new class. Vector xv = new Vector();
1
2061
by: asabdo | last post by:
how add parse in scanner this is code take if statement and split to types and i want to add to code parse where check if statement that is in code with if correct statement and know do it is correct or no ? plz anyone help me and thank you import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;
0
8462
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8382
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1787
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.