473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Double and Int

176 100+
Is it possible to type:
Expand|Select|Wrap|Line Numbers
  1. int number = scan.nextInt();
  2.  
and not get an error if the user enters 0.0? I am trying to write a program that will average numbers until the user enters text. I want the user to be able to enter decimals as well. Here is my source:
Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.Scanner;        //import a scanner
  3.  
  4. public class NumberAverager {    //create a class
  5.  
  6.     public static void main(String[] args) {        //start the main function
  7.         int divideby = 0, number = 0, sum = 0, average = 0;            //declare variables
  8.         Scanner scan = new Scanner(System.in);            //create the scanner
  9.         //explain the program
  10.         System.out.println("This program will average WHOLE numbers.");
  11.         System.out.println("To stop averaging numbers enter anything that is not a whole number.");
  12.         System.out.println("Please enter the numbers: ");
  13.         //create a loop to execute as long as the user enters numbers
  14.         while ( scan.hasNextInt() ) {
  15.             number = scan.nextInt();        //prompt the user for numbers
  16.             sum = sum + number;                //add up the numbers
  17.             divideby ++;                    //increase the amount to divide the numbers by
  18.             if ( scan.hasNextInt() == false) {        //if the user doesn't enter a whole number
  19.                 average = sum / divideby;            //calculate the average
  20.                 //print the average
  21.                 System.out.print("The total average of the numbers is ");
  22.                 System.out.print(average);
  23.                 System.out.println(".");
  24.             }
  25.         }
  26.     }
  27. }
  28.  
May 7 '08 #1
3 2301
Laharl
849 Expert 512MB
No, that's a double literal. Use the Scanner's hasNext*Type* methods to determine what type the input is or parse the input as a String (scanner's next() method) and use a bunch of nested try/catch blocks to attempt different types.

As a general rule, though, just pick one of doubles or ints and use it for something like this. Any int can be represented as a double, so if you want to enable decimals, just use doubles exclusively.
May 7 '08 #2
BigDaddyLH
1,216 Expert 1GB
As a general rule, though, just pick one of doubles or ints and use it for something like this. Any int can be represented as a double, so if you want to enable decimals, just use doubles exclusively.
I agree. If the user can enter a sequence like:

2 3 5 8 13 23.3 36.6

There is no benefit in reading some of them as ints and others as doubles. Read them all in as doubles.
May 7 '08 #3
Kid Programmer
176 100+
Thank you I understand :-)
May 7 '08 #4

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

Similar topics

12
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
10
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
3
by: BlueTrin | last post by:
I am using a DLL written in C, it uses some pointers on functions, I have defined a wrapper around it in C# which uses some delegates: #region Delegates and Marshalling to call solvopt public...
67
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the...
1
by: JWest46088 | last post by:
I keep getting these error messages: area(double,double) in Rectangle cannot be applied to () return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ...
2
by: dj10fld | last post by:
I am getting a (cannot convert double to double in assignment errors) here is a part of my code #include <iostream> #include <iomanip> #include <cmath> using namespace std; #define MaxSize...
2
by: Genro | last post by:
#include<stdio.h> #include<TX/graphics.h> #include<time.h> // I need help! struct Krug{ double _x; double _y; double _skox; double _skoy; double...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.