Hi All,

I have written an application for traffic Study which takes traffic load for seven days, inputting traffic load during morning,lunch,evening and night for each day. The output im getting is this

        Morning     Lunch       Evening Night       Average

Day 0 : 5 5 5 5 5
Day 1 : 5 5 5 5 5
Day 2 : 5 5 5 5 5
Day 3 : 5 5 5 5 5
Day 4 : 5 5 5 5 5
Day 5 : 5 5 5 5 5
Day 6 : 5 5 5 5 5
Average : 5 5 5 5

But i don't understand how im getting this output. Please explain

Here is my code :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package traffic;

import java.util.Scanner;


public class Traffic {

    private int[][] traffic = new int[7][4];
    private int[] dailyAvg = new int[7];
    private int[] timeAvg = new int[4];



    public void collectData(Scanner sc)
{
int row;
int col;
for (row = 0; row < traffic.length; row++)
{
for (col = 0; col < traffic[row].length; col++)
traffic[row][col] = sc.nextInt();
}
}
/**
Analyze data
*/
public void analyzeData()
{
int row;
int col;
int sum;
for (row = 0; row < traffic.length; row++)
{
sum = 0;
for (col = 0; col < traffic[row].length; col++)
sum = sum + traffic[row][col];
dailyAvg[row]
= Math.round(sum / traffic[row].length);

}
for (col = 0; col < timeAvg.length; col++)
{ sum = 0;
for (row = 0; row < traffic.length; row++)
    sum = sum + traffic[row][col];
timeAvg[col]
= Math.round(sum / traffic. length);
}
}
/**
Create study report as a String
@return study report
*/
public String createTrafficReport()
{
int row;
int col;
String str;
str =
"\t\tMorning\t\tLunch\t\tEvening\tNight\t\tAverage\n";
for (row = 0; row < traffic.length; row++)
{
str = str + "Day "+ row + "   :\t\t";
for (col = 0; col < traffic[row].length; col++)
str = str + traffic[row][col] + "\t\t";
str = str + dailyAvg[row]+ "\n";
}
str = str + "Average"+ " :\t";
for (row = 0; row < timeAvg.length; row++)
str = str + timeAvg[row] + "\t\t";
return str;
}

}




package trafficstudyapplication;

import traffic.Traffic;
import java.util.*;
import java.io.*;

public class TrafficStudyApplication
{
public static void main (String[] args) throws
FileNotFoundException, IOException
{
Scanner input = new Scanner(System.in);    
Traffic trafficStudy = new Traffic();
Scanner ScannedInfo
= new Scanner(new File("D:\\inputs\\Traffic.dat"));
PrintWriter output
= new PrintWriter(new FileWriter
("D:\\inputs\\report.dat"));
trafficStudy.collectData(ScannedInfo);
trafficStudy.analyzeData();
output.println(trafficStudy.createTrafficReport());
output.close();    
}
}

The thing i dontt understand is I initiated Str as First statement below .
and i used str in the following statements 2 to 6.

1. str = "\t\tMorning\t\tLunch\t\tEvening\tNight\t\tAverage\n";
2.str = str + "Day "+ row + "   :\t\t"    // output is looking like str = "Day "+ row + "   :\t\t"
3.str = str + traffic[row][col] + "\t\t"   // output is looking like str = traffic[row][col] + "\t\t"
4.str = str + dailyAvg[row]+ "\n"         // output is looking like str = dailyAvg[row]+ "\n"
5.str = str + "Average"+ " :\t"           // output is looking like str = "Average"+ " :\t"
6.str = str + timeAvg[row] + "\t\t"       // output is looking like str = timeAvg[row] + "\t\t"  

In output file why intiated value of str (1st statement) is missing for statements 2 to 6.

For instance in the output file for the second statement why didn't i get something like this,

morning lunch evening night average day 0 5 5 5 5 //for str = str + "day" + row + " :\t\t";

because "str" has been intiated with "\t\tMorning\t\tLunch\t\tEvening\tNight\t\tAverage\n";

how did i get in the output file like below in second row instead of above

day 0 5 5 5 5 5 ( why is the Str intiated value (morning evening night average ..) gone ?)

and this question goes to 3 to 6 statements as well.

You init it, and it appears at the start of the output. It won't appear on every line unless you append that string again at the start of every line

ok. Then why we are using statement like below

str = str + "Day "+ row + "   :\t\t"

instead of this one

str =  "Day "+ row + "   :\t\t"

what is the reason behind using "str" on the right hand side of all output statments.

Why cant i use statement like

str = "day "+ row + " :\t\t";

instead of

str = str + "day "+row+ "    :\t\t";

please explain the logic behind using "str" on the right hand side of all output statments in the program.

str = "hello";  // sets str to refer to the new string "hello"
str = " world";  // just sets str to refer to the new string " world"
but...
str = "hello";  // sets str to refer to the new string "hello"
str = str + "world"; // creates a new string by concatenating "hello" and " world"
                     // and sets str to refer to that new string, "hello world"

oh ok... so as per your above example str is concatenating with the previous initiated value.

so in my program when i call my Createtrafficreport() Method inside a print statement, it will print all the things which is coming on the right hand side of str =" ...." please tell me whether this understanding of mine is right or wrong...

so my first statement is

1. str = "\t\tMorning\t\tLunch\t\tEvening\tNight\t\tAverage\n"; \\1st intiated value

so my outfile becomes like below and a newline created,

        morning   lunch    evening   night   average

and then comes the first statement in outer forloop which is below

2.str = str + "Day "+ row + " :\t\t";

so now my output file has to be like below as per my understanding (concatenation of string)

                     morning  lunch   evening  night average
morning lunch evening night average day 0       

and then comes the thrid statement inside inner forloop (now the cuurent value of str is morning lunch evening night average day 0).

3.str = str + traffic[row][col] + "\t\t";

as per my understanding will make the outpfile mo become something like this,

                  morning   lunch   evening    night     average
morning lunch evening night average day 0 morning average evening day 0 5

so as the for loop continues till the last iteration this concetanation continues and finally output has to become something totally meaningless as per my understanding.

I know im missing the logic which actually makes a perfect output file, but i just cant understand where im missing that logic. please explain ......

You only have one str variable. Each time you re-use it you lose the earlier value(s).

Initially it refers to "\t\tMorning\t\tLunch\t\tEvening\tNight\t\tAverage\n", but then after
str = str + "Day "+ row + " :\t\t";
it no longer refers to that value. It now refers to
"morning lunch evening night average Day 0 " (ignoring the tabs for clarity)
you no longer have any reference to the orginal "Morning Lunch Evening Night Average\n"

then you execute
str = str + traffic[row][col] + "\t\t";
after which str refers to
"morning lunch evening night average Day 0 5"

In other words, each time you execute
str = str + xxx;
you just add xxx onto the end of whatever str was before.

yes, thats what im saying, so as per you said my 2nd line in outfile has to start with

"morning lunch evening night average Day 0 " right ?, i mean after the 1st statement in outer loop.

But in my file 2nd line starts with just " day 0 " . where is the previous value of str.

you said str + xxx ; will add xxx ontp the end of whatever str was before.

in my program Str value before is morning lunch evening night . so str + "day "+row +" :\t\t" has to become

"morning lunch evening night average Day 0 " as you said but my file only has day 0 in the second line. why ?

my file only has day 0 in the second line. why ?

because the "morning lunch evening night average\n" is on the first line.

Just work slowly through a simple example like this:

str = "abc";
str = str + " def"  // str is now "abc def"
str = str + " ghi"  // str is now "abc def ghi"

...

oh my god, I get it ..... finally i got it ...! previously what i thought was output.prinln statement will print each line of str.. but now i understand it only takes the final Str value which is returned by the last statement return str... i really know how string works but this file writing got me confused totally and for last night i didnt even sleep well, because once i get stuck with some doubt, it disturbs me till i get it completely sorted out.

Really Thanks for the assistance ..... Honestly i dont like praising people in their face, but you are really cool in making people understand the logics ...thanks again.

You're welcome
;)
J

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.