5 Replies - 1282 Views - Last Post: 02 May 2014 - 09:16 PM Rate Topic: -----

#1 neewb   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Unable to pull out the lowest week number in program

Posted 29 April 2014 - 11:41 PM

This program reads off of a text file that I have attached to my post. It is supposed to:

display the total sales for each week
the average daily sales for each week
the total sales for all of the weeks
the average weekly sales
the week number that had the highest amount of sales
the week number that had the lowest amount of sales

I can get everything but the very last one. In my code you will see that I tried to use a really large number so it would pull out the lowest amount, but it still didn't work: low = 100000000;

Any help would be appreciated.

Thank you.Attached File  SalesData.txt (173bytes)
Number of downloads: 117



import javax.swing.JOptionPane;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

public class SalesAnalysis 
{
	private String line;
	private Scanner inputFile;
	private double averageDaily;               // Holds the average daily sales for each week
	private double totalSales;                 // Holds the total sales for all of the weeks
	private double averageWeekly;              // Holds the average weekly sales
	
	// Constructor, takes a file name as its argument
	public SalesAnalysis(String file_Name) throws IOException
	{
		// Open the file
		File file = new File("SalesData.txt");
		inputFile = new Scanner(file);
	}
	
	// Method that reads the next line(if there is any) in the file
	public boolean interpretNextLine() throws IOException
	{
		boolean readNext;
		
		// Determine if there are more lines to read in the file
		readNext = inputFile.hasNext();
		
		// Now, read the next line if there is one
		if(readNext)
			line = inputFile.nextLine();
		
		return readNext;
	}
	
	// Method that calculates the total sales for all three weeks
	public double getTotal()
	{
		totalSales = 0;
		
		// Tokenize the last line read from the file
		String[] token = line.split(",");
		
		// Calculate the total of the sales
		for(String string : token)
		
			totalSales += Double.parseDouble(string);
		
		return totalSales;
	}
	
	// Method that calculates the average daily sales for each week
	public double getAverageDailySales()
	{
		averageDaily = 0;
		String[] token = line.split(",");
		
		for(String string : token)
			averageDaily = getTotal() / token.length;
		
		return averageDaily;
	}
	
	// Method that calculates the average weekly sales
	public double getAverageWeeklySales()
	{
		String[] token = line.split(",");
	
		for( String string : token)
			
			averageWeekly = getTotal() / 3;
		
		return averageWeekly;
	}
	
	public void close() throws IOException
	{
		inputFile.close();
	}
	
	public static void main(String[] args) throws IOException
	{
		int count = 0;
		double totalSales = 0;
		double totalAvgWeek = 0;
		double high = 0;
		double low = 1000000000;
		
		// Create a String object
		String string = new String("Numbers");
		SalesAnalysis sales = new SalesAnalysis(string);
		
		// Display the results
		while(sales.interpretNextLine())
		{
			JOptionPane.showMessageDialog(null, "The total  sales for week " + (count + 1) + " was " + sales.getTotal());
			JOptionPane.showMessageDialog(null,  "The average daily sales for week " + (count + 1) + " was " + sales.getAverageDailySales());
			totalSales += sales.getTotal();
			totalAvgWeek += sales.getAverageWeeklySales();
			//high += sales.getTotal();
			//low += sales.getTotal();
			
			
			if(sales.getTotal() > high)
			{
				high = sales.getTotal();
				high = count;
			}
			
			if(sales.getTotal() < low)
			{
				low = sales.getTotal();
				low = count;
			}
			
			count ++;
		}
			
			JOptionPane.showMessageDialog(null, "The average weekly sales was " + totalAvgWeek);
			JOptionPane.showMessageDialog(null, "The total sales were " + totalSales);
			JOptionPane.showMessageDialog(null, "Week " + high + " had the most sales");
			JOptionPane.showMessageDialog(null, "Week " + low + " had the least sales");
			
		// Close the file
		sales.close();
		System.exit(0);
	}		
}



Is This A Good Question/Topic? 0
  • +

Replies To: Unable to pull out the lowest week number in program

#2 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Unable to pull out the lowest week number in program

Posted 30 April 2014 - 12:09 AM

if(sales.getTotal() > high)
	            {
	                high = sales.getTotal();
	                high = count;
	            }
	             
	            if(sales.getTotal() < low)
            {
	                low = sales.getTotal();
	                low = count;
	            }


Here, you are reusing your low and high variables to store your count(week number). But you missed a point that in next iteration you will end up comparing week number with sales total of next week due to this! You were lucky to get correct answer for high sale week (due to values in input file).

Use different variable to store count which depicts your week number. Also if you want your week numbers to start from 1 then initialize count to 1.

This post has been edited by AmitTheInfinity: 30 April 2014 - 12:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 IvGotAPocket   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 157
  • Joined: 12-August 13

Re: Unable to pull out the lowest week number in program

Posted 30 April 2014 - 12:24 AM

Hey man there are a couple things about you code im confused about?
whats the purpose of this code, other then to tell you if your file still have lines in it?
// Method that reads the next line(if there is any) in the file
	public boolean interpretNextLine() throws IOException
	{
		boolean readNext;
		
		// Determine if there are more lines to read in the file
		readNext = inputFile.hasNext();
		
		// Now, read the next line if there is one
		if(readNext)
			line = inputFile.nextLine();
		
		return readNext;
	}



hasNext() return a boolean as is. You could just say
while(scan.hasNext())
{
line = scan.nextLine();
}


in your main and remove that method all together.

Also as to your problem, I would make a method something like this..
public int findHighest()
{
	String[] numbers = line.split(); // spilt up the string

	int count=0; //variable to store count of weeks

	Double temp = Double.parseDouble(numbers[0]); //store first value

	for(int i =0; i<numbers.length;i++) //loops through number array
		if(temp < Double.parseDouble(numbers[i])) 
		{												
														//check if temp number is less than other numbers in array
				temp = 	Double.parseDouble(numbers[i]);	//if number[i] is bigger then temp make temp = number[i] 
				count++; // this will increment everytime temp gets changed.
				//..so if week three is largest..we would loop and temp is less then number[i] so temp is now number[i]..count hits 1..then repeat..temp = number[i]
				// count increments 1.. once we hit largest at 3..count wont increment and temp stays the same then we finish for loop. now week is three and temp is highest
		}
		return count;
	
}


This post has been edited by IvGotAPocket: 30 April 2014 - 12:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 neewb   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Unable to pull out the lowest week number in program

Posted 02 May 2014 - 07:52 PM

View PostIvGotAPocket, on 30 April 2014 - 12:24 AM, said:

Hey man there are a couple things about you code im confused about?
whats the purpose of this code, other then to tell you if your file still have lines in it?
// Method that reads the next line(if there is any) in the file
	public boolean interpretNextLine() throws IOException
	{
		boolean readNext;
		
		// Determine if there are more lines to read in the file
		readNext = inputFile.hasNext();
		
		// Now, read the next line if there is one
		if(readNext)
			line = inputFile.nextLine();
		
		return readNext;
	}



hasNext() return a boolean as is. You could just say
while(scan.hasNext())
{
line = scan.nextLine();
}


in your main and remove that method all together.

Also as to your problem, I would make a method something like this..
public int findHighest()
{
	String[] numbers = line.split(); // spilt up the string

	int count=0; //variable to store count of weeks

	Double temp = Double.parseDouble(numbers[0]); //store first value

	for(int i =0; i<numbers.length;i++) //loops through number array
		if(temp < Double.parseDouble(numbers[i])) 
		{												
														//check if temp number is less than other numbers in array
				temp = 	Double.parseDouble(numbers[i]);	//if number[i] is bigger then temp make temp = number[i] 
				count++; // this will increment everytime temp gets changed.
				//..so if week three is largest..we would loop and temp is less then number[i] so temp is now number[i]..count hits 1..then repeat..temp = number[i]
				// count increments 1.. once we hit largest at 3..count wont increment and temp stays the same then we finish for loop. now week is three and temp is highest
		}
		return count;
	
}



I don't think I am understanding this at all. I tried to use your idea and as you will tell it didn't work. It gives me week 3 with the highest when it should be week 2. The lowest returns as zero....very frustrating!!

public double getHighest()
	{
		String[] numbers = line.split(",");
		Double temp = Double.parseDouble(numbers[0]);
		int count = 0;
		
		for(int i = 0; i < numbers.length; i++)
		{
			if(temp > Double.parseDouble(numbers[i]))
			{	
				temp = Double.parseDouble(numbers[i]);	
				count ++;
			}
		}
		
		return count;
	}
	
	public double getLowest()
	{
		String[] numbers = line.split(",");
		Double temp = Double.parseDouble(numbers[0]);
		int count = 0;
		
		for(int i = 0; i < numbers.length; i ++)
		{
			if(temp < Double.parseDouble(numbers[i]))
			{	
				temp = Double.parseDouble(numbers[i]);
				count ++;
			}
		}
			
		return count;
				
	}
	


This is how I call it in main:

double high = sales.getHighest();
			double low = sales.getLowest();


And display it this way:

JOptionPane.showMessageDialog(null, "Week " + high + " had the most sales");
			JOptionPane.showMessageDialog(null, "Week " + low + " had the least sales");

Was This Post Helpful? 0
  • +
  • -

#5 IvGotAPocket   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 157
  • Joined: 12-August 13

Re: Unable to pull out the lowest week number in program

Posted 02 May 2014 - 08:41 PM

That code i wrote was just an idea and was untested. I will run your code and see whats wrong


edit: i think the problem could be with how the string is being split, basically its evaluating the values wrong i think? would you mine posting all of your code so i could run it?

This post has been edited by IvGotAPocket: 02 May 2014 - 09:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 neewb   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Unable to pull out the lowest week number in program

Posted 02 May 2014 - 09:16 PM

View PostIvGotAPocket, on 02 May 2014 - 08:41 PM, said:

That code i wrote was just an idea and was untested. I will run your code and see whats wrong


edit: i think the problem could be with how the string is being split, basically its evaluating the values wrong i think? would you mine posting all of your code so i could run it?


Sure..yeah I know that split method is messing with the array, because I actually tried one before you posted this one, and it still didn't work. If the file didn't have the delimiters then we could run the array no problem....

Here's my code...I ditched the if statement that was previously in main.

import javax.swing.JOptionPane;
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

public class SalesAnalysis 
{
	private String line;
	private Scanner inputFile;
	private double averageDaily;               // Holds the average daily sales for each week
	private double totalSales;                 // Holds the total sales for all of the weeks
	private double averageWeekly;              // Holds the average weekly sales
	
	// Constructor, takes a file name as its argument
	public SalesAnalysis(String file_Name) throws IOException
	{
		// Open the file
		File file = new File("SalesData.txt");
		inputFile = new Scanner(file);
	}
	
	// Method that reads the next line(if there is any) in the file
	public boolean interpretNextLine() throws IOException
	{
		boolean readNext;
		
		// Determine if there are more lines to read in the file
		readNext = inputFile.hasNext();
		
		// Now, read the next line if there is one
		if(readNext)
			line = inputFile.nextLine();
		
		return readNext;
	}
	
	// Method that calculates the total sales for all three weeks
	public double getTotal()
	{
		totalSales = 0;
		
		// Tokenize the last line read from the file
		String[] token = line.split(",");
		
		// Calculate the total of the sales
		for(String string : token)
		
			totalSales += Double.parseDouble(string);
		
		return totalSales;
	}
	
	// Method that calculates the average daily sales for each week
	public double getAverageDailySales()
	{
		averageDaily = 0;
		String[] token = line.split(",");
		
		for(String string : token)
			averageDaily = getTotal() / token.length;
		
		return averageDaily;
	}
	
	// Method that calculates the average weekly sales
	public double getAverageWeeklySales()
	{
		String[] token = line.split(",");
	
		for( String string : token)
			
			averageWeekly = getTotal() / 3;
		
		return averageWeekly;
	}
	
	public double getHighest()
	{
		String[] numbers = line.split(",");
		Double temp = Double.parseDouble(numbers[0]);
		int count = 0;
		
		for(int i = 0; i < numbers.length; i++)
		{
			if(temp > Double.parseDouble(numbers[i]))
			{	
				temp = Double.parseDouble(numbers[i]);	
				count ++;
			}
		}
		
		return count;
	}
	
	public double getLowest()
	{
		String[] numbers = line.split(",");
		Double temp = Double.parseDouble(numbers[0]);
		int count = 0;
		
		for(int i = 0; i < numbers.length; i ++)
		{
			if(temp < Double.parseDouble(numbers[i]))
			{	
				temp = Double.parseDouble(numbers[i]);
				count ++;
			}
		}
			
		return count;
				
	}
	
	
	public void close() throws IOException
	{
		inputFile.close();
	}
	
	public static void main(String[] args) throws IOException
	{
		int count = 0;
		double totalSales = 0;
		double totalAvgWeek = 0;
		
		
		
		// Create a String object
		String string = new String("Numbers");
		SalesAnalysis sales = new SalesAnalysis(string);
		
		// Display the results
		while(sales.interpretNextLine())
		{
			JOptionPane.showMessageDialog(null, "The total  sales for week " + (count + 1) + " was " + sales.getTotal());
			JOptionPane.showMessageDialog(null,  "The average daily sales for week " + (count + 1) + " was " + sales.getAverageDailySales());
			totalSales += sales.getTotal();
			totalAvgWeek += sales.getAverageWeeklySales();
			//high += sales.getTotal();
			//low += sales.getTotal();
			
			
			/*if(sales.getTotal() > high)
			{
				high = sales.getTotal();
				high = count;
			}
			
			if(sales.getTotal() < low)
			{
				low = sales.getTotal();
				low = count;
			}*/
			
			count ++;
		}
			double high = sales.getHighest();
			double low = sales.getLowest();
			JOptionPane.showMessageDialog(null, "The average weekly sales was " + totalAvgWeek);
			JOptionPane.showMessageDialog(null, "The total sales were " + totalSales);
			JOptionPane.showMessageDialog(null, "Week " + high + " had the most sales");
			JOptionPane.showMessageDialog(null, "Week " + low + " had the least sales");
			
		// Close the file
		sales.close();
		System.exit(0);
	}		
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1