Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with out put

Status
Not open for further replies.

tberger1

MIS
Sep 12, 2006
16
US
I am trying to write a program where the user enters in the average and actual rainfall amounts for 12 months. Then the system will display the month, actual, average and the difference in a table layout -

Month Actual Average Difference
Jan 5 3 2
Feb 2 5 3

and then a new line for every month.

I have it set up to get the amounts and do the difference calculation but cannot figure out the display. There are two classes of code - the rainfall calc and the rainfall test which contains main. I am including both hoping that somone can help me get it to display in the above layout.

import java.util.Scanner;

public class RainfallCalc
{
private int [] [] rainfall;
private int startMonth;

public RainfallCalc(String monthNum)
{
if(monthNum == null)
{
System.err.println("Empty Month Number");
System.exit(1);
}
startMonth = Integer.parseInt(monthNum);

if (startMonth < 1 || startMonth >12)
{
System.err.println("Invalid month number");
System.exit(1);
}

rainfall = new int[12] [4];

//MAKE CALL POPULATE ARRAY WITH startMonth
this.popMonth(startMonth);

}
private void computeDiff()
{
for (int i = 0; i < rainfall.length; i++)
rainfall[3] = rainfall[2] - rainfall[1];
}

public void getInput()
{
Scanner keyboard = new Scanner(System.in);

for (int i = rainfall.length - 1; i >=0; i--)
{
System.out.println("Please enter the average rainfall " + "for " + numToMonth(rainfall[0]) + ": ");

int num = keyboard.nextInt();
if (num < 0)
{
System.out.println("Negative Rainfall, Sorry!");
System.exit(1);
}

rainfall[1] = num;

//Collect the Actual Rainfall Amount

System.out.println("Please enter the actual rainfall "+ "for " + numToMonth(rainfall[0]) + ": ");
num = keyboard.nextInt();

if (num < 0)
{
System.out.println("Negative Rainfall, Sorry!");
System.exit(1);
}

rainfall[2] = num;

}

this.computeDiff();

}

private String numToMonth(int num)
{
switch (num)
{
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "";
}
}

private void popMonth(int start)
{
//int month = start;
for(int i = rainfall.length - 1; i >= 0; i--)
{
rainfall[0] = start;
start--;

if(start <1)
start = 12;
}
}

public String toString()
{
String result;
result = "Month \t \t Average \t\t Actual \t\t Difference \t\t\n";
return result;
}
}

Here is where main resides:

import java.util.Scanner;

public class RainTest
{

public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

while (true)
{
System.out.println("Please enter the current month: \n"
+ "1 - 12 = Jan to Dec\n" + "Q to stop calculating");

String currMonth = keyboard.next();

if (currMonth.charAt(0) == 'Q' || currMonth.charAt(0) == 'q')
break;

RainfallCalc calc = new RainfallCalc(currMonth);
calc.getInput();

System.out.println(calc);

}
}
}
 
I would build an array, 12x4, where i,0 is the month (jan, feb, etc), i,1 is average for that month, i,2 is difference, and i,3 is actual. Then loop through the rows of the array using the same string construction used in toString{}: result = "Month \t \t Average \t\t Actual \t\t Difference \t\t\n";

_________________
Bob Rashkin
 
While you are at it, you could replace the switch statement that recodes the months with an array of month names, then just use month number -1 to look them up...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for the responses.

Bob,

This array is where I am struggling. Do I call the methods for getting the month, getting the data and calcualting into the subsets of the array? This should probably be the easy part but it is just not clicking.
 
I think it would be best to make the array a class variable. That way, all the methods can access it without passing it back and forth. I'd initialize it in the main method, putting the month names in the first column. Then you can call methods to input or calculate whatever needs to go in the other columns. Then, when the data is collected, call a method to format each row and print it.

I don't know if there is any compelling reason you're using standard input and output. There's certainly nothing wrong with it but for similar applications I've used a window-structure. You can have a pull-down (Choice) widget already populated with the month names, and textfields for the users to enter the different parameters. Then a button to update the array with the data from the widgets. You can also have a textarea to display the output. It doesn't really change anything but the user can controll the order of data entry rather than relying on screen prompts.

Just a thought.

_________________
Bob Rashkin
 
I have to set it up this way because it is an assignment. I am suppose to make the toString method print out the column headings and then populate the data that follows. I guess my biggest problem is how to get the data from another method into the different subsets of the array. I have tried to call the various functions like this
i[o].getInput();
Definitely not working. Your suggestions sound good, I just can't figure out how to tie it all together. Is there any code sample you share?

TJ
 
Well now, I think this forum is assiduously opposed to working on homework, so let me forget you said that. I'm certainly no Java expert but maybe I can suggest a few things that will help.

You're going to have a class:
Code:
class someClass {
}

Then, before any methods, you can define class data:
Code:
class someClass {
   String dataArray [12][4];
   public static void main(String args[]) throws IOException {
      dataArray[0][0]="January";
      dataArray[1][0]="February";
//etc.
   }
}
Now, dataArray is a String array so any numbers that are input or computed need to be converted to strings before putting them in the array. On the other hand, no casting is necessary upon output.

Something I like to do for columnar formatting is replace pieces of a string so everything lines up (tabs can be erratic). I declare a character array large enough to hold all the columns. Lets say each column will be 12 characters, with, say 2 characters in between, so the character array will be 4x12+3x2=54 elements. When I want to add a string, say "400.12 in." (10 characters) to, say, the 3rd column, I know that the 3rd column starts at element 2*14+1 or 29. So I loop from 29 to <39 taking each character from the string into the array. Then I print the array as a string:
Code:
class someClass {
   String dataArray [12][4];
   char[] ttl = new char[54]; 
   public static void main(String args[]) throws IOException {
      dataArray[0][0]="January";
      dataArray[1][0]="February";
//etc.
   }
//          ...
   public static void otherMethod() {
      int wordLen=dataArray[4][2].length();
      for (int j=0;j<wordLen; j++) {
         ttl[j+29]=dataArray[4][2].charAt(j);
      }
      //....
      System.out.println(new String(ttl));
   }
}
or something like that.

_________________
Bob Rashkin
 
Thank you so much for your words of wisdom. I actually figured it out - there was an existing array at the beginning of my class that I was able to call in. I always look at things the hard way.

Also I should probably just point out that I am taking a workshop to learn java - it is a no credit week long training session. I am suppose to be brushing up my skills.

Thanks again for your suggestions and you time.
 
If you have the same problem, then you should probably do the same thing, no?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top