Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: input.next and taking ints and doubles

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default input.next and taking ints and doubles

    Hi all,

    I am writing a program (weekly assignment) that takes numbers from a text file called Q2.txt, ignores white space, and gives the user a printout of the sum total and average of all the numbers in the text file.

    I have it working, but i can only make it work for doubles or integers only.
    I'll get full marks for what i have below, but for personal interest, i'd like to know how to make the program work for any and all number formats.

    How can i make it work for both ints and doubles?

    my Q2.txt is contains = 5.0 5.0 5.0 5.0

    Program returns : Average = 5.0, total = 20.0

    Thanks for any and all advice

    EDIT: I rewrote my test file Q2.txt with a mix of doubles and ints and it worked.
    So can i change my question to: Is this best practice? How can i improve this?



    import java.io.*;
    import java.util.Scanner;
     
    public class Q2
    {
    	//Open main method and throw any exceptions.
    	public static void main(String[] args)
    	throws Exception
    	{
    		//input from file called Q2.txt
    		Scanner input = new Scanner(new File("Q2.txt"));
    		//initialise variables for total and counter
    		double sum = 0.0;
    		int counter = 0;
    		//while there is still scores in the file unread, read them, add to the total and increment the counter
    		while (input.hasNext())
    		{
    			sum += input.nextDouble();
    			counter++;
    		}
    		//divide the sum by the counter
    		double average = sum / counter;
     
    		//print the results
    		System.out.println("The total of the scores in Q2.txt is " + sum);
    		System.out.println("The average of the scores in Q2.txt is " + average);
    		//close user input
    		input.close();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: input.next and taking ints and doubles

    The Scanner class has several different hasNext... methods that allow the program to see what kind of data is available to be read. Try using some of them with the matching next... method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Trouble With Doubles
    By TheSlowLearner in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 13th, 2012, 10:17 AM
  2. Error with taking float numbers from an input dialog box.
    By deepcrimson76 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2012, 12:58 PM
  3. Adding doubles and ints.
    By SkyAphid in forum Java Theory & Questions
    Replies: 6
    Last Post: September 8th, 2011, 05:54 PM
  4. Taking Input in String Array
    By syehjafa in forum Collections and Generics
    Replies: 8
    Last Post: July 25th, 2011, 07:18 AM
  5. Parsing many Ints from a user input String.
    By helpmeplease111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2011, 08:41 PM