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: Need help with DecimalFormat class

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with DecimalFormat class

    I need assistance with a program I wrote for class. I got it to compile and do what I want, But I was wondering how I can format my methods to to display a decimal with 1-2 decimal places. Would I create a method in my NumberAnalysis class to do it for me? Or would I declare an instance of the DecimalFormat class in my main method? I am new to programming in General and would like some feed back.


    package lesson4.skowronek;
     
    import java.util.Scanner;  //Needed for Scanner Class
    import java.io.*;			   //Need for File and IOException
    import java.text.DecimalFormat;
     
     
    public class Ex8_11 {
     
     
     
     
    	public static void main(String[] args) throws IOException{
     
    		{
     
    		DecimalFormat decformatter = new DecimalFormat("#0.00");
     
    		NumberAnalysis na = new NumberAnalysis("C:\\Users\\*****\\Java Work\\Lesson 4\\src\\lesson4\\skowronek\\Numbers.txt");
    		System.out.println("Lowest Number: " +  na.getLowest());
    		System.out.println("Highest Number: " + na.getHighest());
    		System.out.println("Total Number: " + na.getTotal());
    		System.out.println("Total Average Number: " + na.getAverage());
     
    		}
     
     
    	}
     
     
     
    }
     
     
     
     
     
     
    package lesson4.skowronek;
     
    import java.text.DecimalFormat;
    import java.util.Scanner;
    import java.io.*;
     
     
    public class NumberAnalysis {
     
    	double[] arrayNum;
    	int cnt;
    	final int ARRAY_SIZE = 12;
     
     
    	NumberAnalysis(String filename) throws IOException
    	{
    		int cnt = 0;
    		arrayNum = new double[ARRAY_SIZE];
     
     
     
     
    	File numbers =  new File("C:\\Users\\******\\Java Work\\Lesson 4\\src\\lesson4\\skowronek\\Numbers.txt");
     
    		Scanner inputFile = new Scanner(numbers);
    		while (inputFile.hasNext() && cnt < arrayNum.length)
    		{
    			arrayNum[cnt] = inputFile.nextDouble();
    			cnt++;
    		}
    	inputFile.close();
     
     
     
    	}
     
    	public double getLowest()
    	{
    	 double lowest = arrayNum[0];
    	 for (int cnt = 1; cnt < arrayNum.length; cnt++)
    	 {
    		 if(arrayNum[cnt] < lowest)
    			 lowest = arrayNum[cnt];
    	 }
    	 return lowest;
    	}
    	public double getHighest()
    	{
    		double highest = arrayNum[0];
    		for (int cnt = 1; cnt < arrayNum.length; cnt++)
    		{
    			if (arrayNum[cnt] > highest)
    				highest = arrayNum[cnt];
    		}
    		return highest;
    	}
     
    	public double getTotal()
    	{
     
    		double total = 0.0; //accumulator
     
    		for (int cnt = 0; cnt < arrayNum.length; cnt++)
    		{
     
    		       total += arrayNum[cnt];
    		}
     
    		        return total;
     
    	}
    	public double getAverage()
    	{
    		double average = getTotal() / arrayNum.length;
    		return average;
    	}
     
    }


    --- Update ---

    This is my output:


    Lowest Number: 1.09
    Highest Number: 82.76
    Total Number: 367.89000000000004
    Total Average Number: 30.657500000000002

    --- Update ---

    Think I just solved the answer to my own question, I did it by declaring double variables in my main method and called the methods and instantiated the variables into the methods... Is there a better way to do this?

    package lesson4.skowronek;
     
    import java.util.Scanner;  //Needed for Scanner Class
    import java.io.*;			   //Need for File and IOException
    import java.text.DecimalFormat;
     
     
    public class Ex8_11 {
     
     
     
     
    	public static void main(String[] args) throws IOException{
     
    		{
    			 double lowest;
    			 double highest;
    			 double average;
    			 double total;
     
     
     
     
    		NumberAnalysis na = new NumberAnalysis("C:\\Users\\*****\\Java Work\\Lesson 4\\src\\lesson4\\skowronek\\Numbers.txt");
     
    		lowest = na.getLowest();
    		highest = na.getHighest();
    		average = na.getAverage();
    		total = na.getTotal();
     
    		DecimalFormat df = new DecimalFormat("#0.00");
     
    		System.out.println("Lowest Number: " +  df.format(lowest));
    		System.out.println("Highest Number: " + df.format(highest));
    		System.out.println("Total Number: " + df.format(total));
    		System.out.println("Total Average Number: " + df.format(average));
     
    		}
     
     
    	}
     
     
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with DecimalFormat class

    You might also look into using printf().

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  2. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  3. DecimalFormat error when compiled
    By roofninja in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 11th, 2012, 10:48 PM
  4. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  5. MessageDialog working with DecimalFormat?
    By Plural in forum Java Theory & Questions
    Replies: 2
    Last Post: October 12th, 2010, 04:07 AM

Tags for this Thread