Need to print output without arrays

Discussion in 'Java' started by Stormstreet, Nov 25, 2009.

  1. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    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.
     
  3. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  5. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    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.
     
  6. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    If you could not understand yet, just let me know where you don't understand. I can explain.
     
    shabbir likes this.
  7. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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)
     
  8. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Sorry but its my first time with java so forgive me lol
     
  9. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    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.
     
  10. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    took your advice but still looking for how to append variables to a variable
     
  11. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    could explain to me a little more thanks
     
  12. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Ok. Here is the way you can append/concatenate two variables values:

    Code:
    str="";
    str1="";
    x="value of x";
    y="value of y";
    
    str = str + x + y;
    str1 = str1 + x + "\n" + y;
    
    System.out.println("str value: " + str);
    System.out.println("str1 value: " + str1);
    
    The output of the above code will be:
    Code:
    str value: value of xvalue of y
    str1 value: value of x
    value of y
    
     
    shabbir likes this.
  13. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    str = str+"\n"+r+" "+ de +" "+ ic+" "+"@"+" "+c+" "+ " TOTAL $" + r*c +"\n"+;

    I USED THIS AND IT WORKED LIKE A CHARM THANKS ALOT. ONE MORE THING THOUGH LETS SAY I WANNA ADD UP THE GRAND TOTAL OF WEIGHT OR TOTAL COST OF ALL ITEMS CAN I DO THAT????
     
  14. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    i actually am geeting through
    will post any other problems thanks u really r gr8!!!!!!!!!!!!!!!!!!!!1
     
  15. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Its really simple. Just try it out and come back :)
     
  16. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    U rock u should become a teacher lol
     
  17. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    can i nest these instead of having all these do while loops or should i leave them??

    //code
    do{
    System.out.println("ENTER Weight OF ITEM IN GRAMS:");
    s = br.readLine();
    w = Integer.parseInt(s);
    }while( !isValidIw(w));


    do{
    System.out.println("ENTER COST OF ITEM ($):");
    s = br.readLine();
    c = Float.parseFloat(s);
    }while( !isValidIc(c));


    do{
    System.out.println("ENTER NUMBER OF ITEMS REQUIRED:");
    s = br.readLine();
    r = Integer.parseInt(s);
    }while( !isValidIr(r));
    //code
     
  18. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    You should not nest the WHILE loops, instead you can combine the three loops to have a single loop alone.
     
  19. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    think i'll leave it as that thanks though
     
  20. Stormstreet

    Stormstreet New Member

    Joined:
    Nov 25, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    i decided to use vectors here it the code

    code :
    Code:
    import java.io.*;
    import java.util.*;
    
    public class Cw1{ 
        
    	//Section 1: global variables
    	
    	public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        static String input,code,desc, weight,cost,amount;
    	static float TotalWeight = 0.0f,TotalCost = 0.0f,grandTotalWeight=0.0f,grandTotalCost=0.0f;
    	static boolean isItemCode = false,isItemDesc = false,isItemWeight = false,isItemCost = false,isItemAmount = false;
    	
    	public static void main(String[ ] args) throws Exception{
    	// Section 2 - Welcome message
    	System.out.println("*****************************");
    	System.out.println("** Welcome to  Trekker     **");
    	System.out.println("*****************************");
    	
    	// Section 3 - Loop that prompts for order information,
        // tests if it is valid, prints it in a summary	
    	// and stores it in a vector until user is done
    	
    	Vector v = new Vector();
    	do{
    		System.out.println("Enter Item code");	
     	
    		input = br.readLine();
    		code = input;						
    	
    	System.out.println("Enter Item Description"); 
     	do{
    		input = br.readLine();
    		desc = input;
    		isItemDesc = isDescCorrect(input);		
    	}
    	while(!isItemDesc);
    
    	System.out.println("Enter Item weight");
     	do{
    		input = br.readLine();	
    		weight = input;
    		isItemWeight = isItemWeightCorrect(input);	
    	}
    	while(!isItemWeight);
    		
    	System.out.println("Enter Item cost");
     	do{
    		input = br.readLine();
    		cost = input;
    		isItemCost = isItemCostCorrect(input);
    	}
    	while(!isItemCost);
    	
    		System.out.println("Enter Item amount");
     	do{
    		input = br.readLine();
    		amount = input;
    		isItemAmount = isItemAmountCorrect(input);
    	}
    	while(!isItemAmount);
    	
        Items myItems = new Items(code, desc, Float.parseFloat(weight), Float.parseFloat(cost), Integer.parseInt(amount),TotalCost);
    	// print summary for item
    	System.out.println("SUMMARY DATA FOR ITEM "+myItems.getItemCode()+", "+desc);
    	TotalWeight = calculateTotalWeight(Float.parseFloat(weight),Integer.parseInt(amount));
    	System.out.println("TOTAL WEIGHT: "+TotalWeight+" grams");
    	TotalCost = calculateTotalCost(Float.parseFloat(cost),Integer.parseInt(amount));
    	System.out.println("TOTAL COST: $"+TotalCost);
    	
    	// Section 5 - store user input in vector
    	v.add(myItems);
    		
    	grandTotalWeight=grandTotalWeight+TotalWeight;
    	grandTotalCost=grandTotalCost+TotalCost;
    	
    	System.out.println("");
    	System.out.println("Would you like to add another item?");
    	System.out.println("Please type Y or y for yes");
    		input = br.readLine();
    		if(input.equals("Y")||input.equals("y")){
    			System.out.println("");
    		}else{
    			System.out.println("You have ordered");
    			for(int i =0; i<v.size(); i++){
    				System.out.print(((Items)v.elementAt(i)).getItemAmount()+" ");
    				System.out.print(((Items)v.elementAt(i)).getItemDesc()+" ");
    				System.out.print("("+((Items)v.elementAt(i)).getItemCode()+") ");
    				System.out.print("@"+" ");
    				System.out.print("$"+((Items)v.elementAt(i)).getItemCost()+" ");
    				System.out.println("TOTAL COST $"+((Items)v.elementAt(i)).getItemTotalCost());
    			}//close for loop
    			System.out.println("GRAND TOTAL WEIGHT:"+grandTotalWeight+" grams");
    			System.out.println("GRAND TOTAL COST: $"+grandTotalCost);
    			System.out.println("Good bye");
    			break;
    		}//close else
    	}//close outer do loop
    	while(true);
    	System.exit(0);
       	}//close main
        	
    	// SECTION 4 METHODS
    
    	
    	public static boolean isDescCorrect(String itemDescription){
    		if(itemDescription.equals("")||itemDescription.equals(" ")){
    			System.out.println("Invalid Item,please re-enter Item.");				
    			return false;
    		}else{			
    			return true;
    		}
    	}
    		
    	public static float calculateTotalWeight(float itemWeight, int itemAmount){
    		float totalWeight = itemWeight * itemAmount;
    		return totalWeight;
    	}
    		
    	public static float calculateTotalCost(float itemCost, int itemAmount){
    		float totalCost = itemCost * itemAmount;
    		return totalCost;
    	}
    	public static boolean isItemWeightCorrect(String ItemWeight){
    			float ItemWt=Float.parseFloat(ItemWeight);
    			if(ItemWt <= 0){
    				System.out.println("Please re-enter weight,cannot be less than or equal to 0");				
    				return false;
    			}else{			
    				return true;
    			
    		}
    	}
    	public static boolean isItemCostCorrect(String ItemCost){
    			float ItemCst=Float.parseFloat(ItemCost);
    			if(ItemCst <= 0){
    				System.out.println("Please re-enter cost,cannot be less than or equal to 0");				
    				return false;
    			}else{			
    				return true;
    		    }
    	}
    	public static boolean isItemAmountCorrect(String ItemAmount){
    			int ItemAmt=Integer.parseInt(ItemAmount);
    			if(ItemAmt <= 0){
    				System.out.println("Please re-enter items required,cannot be less than or equal to zero");				
    				return false;
    			}else{			
    				return true;
    			}
    	}
    }
    
    only problem is in the grand total cost for the first item it prints zero for the second item it prints the first grand total. what am i doing wrong?
     
    Last edited by a moderator: Jan 11, 2010

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice