I'm having trouble printing out what I want. If I put my printing statements inside the Calculate method, it does print out what I expect. However, I want to do it in a separate method. Can anyone tell me what I did wrong? Also, can anyone point out to me if I have any unnecessary codes?(meaning if I take them out, it still wouldn't affect the outcome.) Thanks in advance.

import java.util.Scanner;
	import java.io.File;
	//import java.io.FileNotFoundException;
	import java.io.IOException;
import java.text.*;
	
	
	public class Proj11852
	
	{
		private static boolean DEBUG= false;
	  public static void main(String[]args)throws IOException
	  
	  {			  
		  int [] Years = new int[50];
		  double[] Population = new double[50];

			 int LargeYear1=0;
			 int LargeYear2=0;
			 double CurrentPercent=0;
			 double LargestPercent=0;
			 int Size= (Population.length) -1;
			 
		  InputData(Years, Population, args[0]); 
		  if (DEBUG) printArrays(Years,Population,Size);
	      Calculate(Years, Population,Size, CurrentPercent, LargestPercent, LargeYear1, LargeYear2);
		  PrintResult(LargestPercent, LargeYear1, LargeYear2);
	      
	
	  }
	  
	  private static int InputData(int[]Years, double[]Population, String filename)throws IOException
	  
	  {
		  
			  Scanner s = new Scanner (new File(filename));
			  int counter=0;		  
			  
			  while(s.hasNext())
			  {
				  Years[counter]= s.nextInt();
			      Population[counter]= s.nextInt();
			      counter++;
			  } 
			  s.close();
			  return counter;
	  }
	  
	public static void Calculate(int [] Years, double [] Population, int Size, double CurrentPercent, double LargestPercent, int LargeYear1, int LargeYear2)throws IOException
	
	  {
		 int counter=0;
		 
		 while (counter<Size)
		 {
			 CurrentPercent= (((Population[counter+1])/(Population[counter]))-1)*100;
			  if (CurrentPercent>LargestPercent)
			  {
				  LargestPercent=CurrentPercent;
				  LargeYear1= Years[counter];
				  LargeYear2= Years[counter+1];				  
			  }
			  counter++;
			  
		 }						  
	  }
	  
	  private static void PrintResult(double LargestPercent, int LargeYear1, int LargeYear2)throws IOException
	  
	  {
		  DecimalFormat df = new DecimalFormat("00.00");
		  System.out.println(LargeYear1+ ", "+ LargeYear2+ ": "+ df.format(LargestPercent));
	  }
	  
	  private static void printArrays(int [] Years, double [] Population, int Size)
	  
	  {
	        for (int i=0 ; i<Size; i++)
	        {
	        	System.out.print(Years[i]+ " ");
	        	System.out.println(Population[i]);
	        }
	  }
	  
	}

Recommended Answers

All 6 Replies

it does print out what I expect

Can you show what is printed out
and show what you expect or want to be printed out?

1) Please follow naming conventions of class name starting with capital and every new word is also capitalized, method names and variables starts wit lower case letter and every new word is capitalized. Constants are all capitals
2)Your if statement will be always ignored because you never change boolean value of DEBUG
3) Either create object that will hold all parameters array years, population etc, or declare them as global. If you do not do that there will be zero values as InputData does localized assignment

Norm1,
What I expect to be printed out is,

Input file: example.data (--> the name of data file which I type in at the command line)
1994,1995: 13.02% (--> the years and the percentage increase for the two consecutive years that had the greatest
percentage increase)

peter budo,
Can you show me how to create object that will hold all parameters array years, populations... etc? I'm new to java and I'm not really familiar with the terms.

Thank you both for responding my question!

Can you show what is printed out?

It printed out

Input file: example.data
0, 0: 0.0%

(Sorry that I did not see your first question)

public static void Calculate(int [] Years, double [] Population, int Size, double CurrentPercent, double LargestPercent, int LargeYear1, int LargeYear2)throws IOException  {
...
CurrentPercent= (((Population[counter+1])/(Population[counter]))-1)*100;
...
LargestPercent=CurrentPercent;
LargeYear1= Years[counter];
LargeYear2= Years[counter+1];

This code isn't going to work because Java parameters are passed as a copy - ie when you call this method passing variables for size, currentPercent etc Java makes a copy of those values for you to use inside your method. When you assign new values to those in your method you are only changing the copy, not the original. When your method terminates the copies are discarded, leaving your original variables unchanged. (This isn't quite the same for arrays and Objects - these are not copied, so when you update them you update the original.)
This is what Peter Budo's point (3) was referring to - see his post for things you can do to fix it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.