Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Need to print output without arrays (http://www.go4expert.com/forums/print-output-arrays-t20193/)

Stormstreet 25Nov2009 22:05

Need to print output without arrays
 
Code:

import java.io.*;

public class Cw01
{

    public static void main(String[] args) throws Exception
    {
     
      int w,r;
      String de,s,ic,st;
      float c;
     
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        do
        {
            System.out.println("=======================================");
            System.out.println("=========WELCOME TO TREKKER============");
            System.out.println("=======================================");
           
            System.out.println("ENTER ITEM CODE:");
            ic = br.readLine();           
           
            System.out.println("ENTER DESCRIPTION OF ITEM:");
            de = br.readLine();
           
            System.out.println("ENTER Weight OF ITEM IN GRAMS:");
            w = Integer.parseInt(s = br.readLine());
           
            System.out.println("ENTER COST OF ITEM ($):");
            c = Float.parseFloat(s = br.readLine());
           
            System.out.println("ENTER NUMBER OF ITEMS REQUIRED:");
            r = Integer.parseInt(s = br.readLine());
           
           
            System.out.println("**********************************************");
            System.out.println("  SUMMARY DATA FOR ITEM" +" "+ ic +" " + de);
            System.out.println("  TOTAL WEIGHT: " +" "+ w+" "+ "GRAMS");
            System.out.println("  TOTAL PRICE : "+" " + r*c );
            System.out.println("**********************************************");
           
            System.out.println("ENTER MORE DATA ? (Y/N)");
            st = br.readLine();
            st.charAt(0);
        }while (st.equalsIgnoreCase("y"));
           
            System.out.println("********************************************");
            System.out.println("********SUMMARY OF WHAT WAS ORDERED*********");
            System.out.println(r+" "+ de +" "+ ic+" "+"@"+" "+c+" "+ " TOTAL $" + r*c);
           
    }

}


Hi I need to print out a summary of all items that was input was by the user.When the program runs it only prints the last input and not all.How do i get it to print all items without using arrays

venami 25Nov2009 22:54

Re: Need to print output without arrays
 
You can declare a String variable and append the summary details(whatever it may be) to it for every item inside the loop.

Then you can print the variable outside the loop to view the summary of all the items.

Stormstreet 25Nov2009 23:15

Re: Need to print output without arrays
 
Didnt I do that already.

String de,s,ic,st;

Or can u elaborate a liitile on your suggestion cause i'm a bit confused thanks.

Stormstreet 25Nov2009 23:25

Re: Need to print output without arrays
 
When asked if to enter more data and no is selected the final summary should be like this:
eg:
5 Rope k23/1 @ $12.34 Total $61.70
50 Nail 12/ewr @ $2.23 Total $111.50

Grand Total weight :
Grand total cost:

but its just printing the last statement entered.

A little more info on my problem

venami 25Nov2009 23:29

Re: Need to print output without arrays
 
You have calculated the values and displayed them inside the loop. You have displayed the summary outside the loop, where the variables that you declared will hold only the final value.

To have the details of all the items, you can have another variable, say str.
For every loop, append the details(variables that you already declared) at the end of the variable 'str'.

So, at the end of the loop, the variable 'str' will have all the items' details and you can print it.

venami 25Nov2009 23:31

Re: Need to print output without arrays
 
If you could not understand yet, just let me know where you don't understand. I can explain.

Stormstreet 25Nov2009 23:39

Re: Need to print output without arrays
 
ok tell me if i'm catching on or that badly off lol. I should declare a new String variable eg. str.Then assign variables i want to print out
eg. str =( r+" "+ de +" "+ ic+" "+"@"+" "+c+" "+ " TOTAL $" + r*c)

Stormstreet 25Nov2009 23:40

Re: Need to print output without arrays
 
Sorry but its my first time with java so forgive me lol

venami 25Nov2009 23:51

Re: Need to print output without arrays
 
You got my point. But the way you are doing is wrong.

So you can very well search the internet on how to append variables to another variable. Or you can also refer a JAVA book for that. :)

This is the correct way for a beginner like you to get familiarized with any programming language.

Stormstreet 26Nov2009 03:48

Re: Need to print output without arrays
 
took your advice but still looking for how to append variables to a variable


All times are GMT +5.5. The time now is 05:07.