Results 1 to 6 of 6
  1. #1
    jagger is offline Member
    Join Date
    Oct 2014
    Posts
    1
    Rep Power
    0

    Default I'm not sure how to delete this.

    so here's what I did instead.
    Last edited by jagger; 10-19-2014 at 04:37 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Variable Fields are not holding information through constructor from user input

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Variable Fields are not holding information through constructor from user input

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Variable Fields are not holding information through constructor from user input

    Well, this thread has become a no-op (in more ways than one).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Variable Fields are not holding information through constructor from user input

    For the record, here's the original content.

    db

    Quote Originally Posted by jagger View Post
    Variable Fields are not holding information through constructor from user input

    So I have a couple of classes that hold attributes for a sandwich, I then try to combine them in the constructor for the Sandwich Class. After that I use my main method to ask the user for their sandwich attributes in which then it's suppose to transfer it into the sandwich class and allow me to print out the data using the Sandwich class. It's not following through. When I try to run it the current print out is "The null . It should be "The (name of sandwich user inputed) ." I am not sure what I did wrong. If anyone has any suggestions I would appreciate it! Here is my code below(sorry if its unsightly, I'm just starting out programming.) :
    Java Code:
     import java.util.*;
     import java.lang.*;
     public class SandwichBuilder
    
     {
     public static void main(String[] args)
     { 
     Scanner inputDevice = new Scanner(System.in);
     System.out.println("What is the name of your sandwich?");
     String sandName = inputDevice.next();
     System.out.println("What is the price of the sandwich? Ex: If price is four dollars input: 4.00");
     String sandPrice = inputDevice.next();
     System.out.println("What type of bread would you like?");
     String breadType = inputDevice.next();
     System.out.println("How many calories per slice?");
     int numberOfCaloriesPerSlice = inputDevice.nextInt();
     System.out.println("What kind of filling would you like?"); 
     String filling = inputDevice.next();
     System.out.println("How many calories per serving?"); 
     int caloriesPerServing = inputDevice.nextInt();
     Sandwich order = new Sandwich(sandName,sandPrice,breadType,numberOfCalo riesPerSlice,filling,caloriesPerServing);
     System.out.println("The " + order.getSandwichName()+".");
    
     }}
    
     class Bread
     { 
     private String breadType; 
     private int numberOfCaloriesPerSlice; 
    
     public String getBreadType() 
     { 
     return breadType;
     }
    
     public void setBreadType(String breadType) 
     {
     this.breadType = breadType;
     }
    
     public int getNumberOfCaloriesPerSlice()
     { 
     return numberOfCaloriesPerSlice;
     }
    
     public void setNumberOfCaloriesPerSlice(int breadCals)
     {
     this.numberOfCaloriesPerSlice = breadCals;
     }
    
     public Bread()
     {
     this.breadType = "";
     this.numberOfCaloriesPerSlice = 0;
     }
    
     public Bread(String breadType, int numberOfCaloriesPerSlice) 
     {
     setBreadType(breadType);
     setNumberOfCaloriesPerSlice(numberOfCaloriesPerSli ce);
     }
     }
    
     class Filling 
    
     { 
     private String fillingType; 
     private int caloriesPerServing; 
    
    
     public String getFillingType()
     {
     return fillingType;
     }
    
     public void setFillingType(String filling) 
     {
     this.fillingType = filling;
     }
    
     public int getCaloriesPerServing() 
     {
     return caloriesPerServing;
     }
    
     public void setCaloriesPerServing(int servingCals) 
     {
     this.caloriesPerServing = servingCals;
     }
    
     public Filling()
     {
     this.fillingType = "";
     this.caloriesPerServing = 0;
     }
    
     public Filling(String filling, int cals) 
     { 
     setFillingType(filling);
     setCaloriesPerServing(cals);
     }
    
     }
    
     class Sandwich
    
     { 
     private String sandwichName;
     private String sandwichPrice; 
    
     public String getSandwichName()
     {
     return sandwichName;
     }
     public void setSandwichName(String sandName)
     {
     this.sandwichName = sandName;
     }
    
     public String getSandwichPrice()
     { 
     return sandwichPrice;
     } 
    
     public void setSandwichPrice(String sandPrice)
     {
     this.sandwichPrice = sandPrice;
     }
    
     Bread mBread = new Bread(); 
    
     public String getmBreadType()
     { mBread.getBreadType();
     return this.mBread.getBreadType();
     }
    
     public void setmBreadType(String breadType)
     {
     mBread.setBreadType(breadType);
     }
    
     public int getmBreadCals()
     {
     mBread.getNumberOfCaloriesPerSlice();
     return this.mBread.getNumberOfCaloriesPerSlice();
     }
     public void setmBreadCals(int numberOfCaloriesPerSlice)
     {
     mBread.setNumberOfCaloriesPerSlice(numberOfCalorie sPerSlice);
     }
    
     Filling mFilling = new Filling();
    
     public String getmFillingType()
     { 
     mFilling.getFillingType();
     return this.mFilling.getFillingType();
     }
    
     public void setmFillingType(String filling)
     {
     mFilling.setFillingType(filling);
     }
     public int getmFillingCals()
     {
     mFilling.getCaloriesPerServing();
     return this.mFilling.getCaloriesPerServing();
     }
     public void setmFillingCals(int servingCals)
     { 
     mFilling.setCaloriesPerServing(servingCals);
     }
    
     public Sandwich(String sandName, String sandPrice, String breadType, int numberOfCaloriesPerSlice, String filling, int servingCals)
     { 
     sandName = sandName;
     sandPrice = sandPrice;
     breadType = breadType; 
     numberOfCaloriesPerSlice = numberOfCaloriesPerSlice;
     filling = filling;
     servingCals = servingCals;
     }
    
     public int getTotalCals(int numberOfCaloriesPerSlice, int servingCals)
     {
     int totalCalories; 
     totalCalories = (numberOfCaloriesPerSlice * 2) + servingCals;
     return totalCalories;
     }
    
     }
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Variable Fields are not holding information through constructor from user input

    Also for the record, jagger reported my post above with the comment

    Quote Originally Posted by jagger View Post
    Problematic, I tried to delete my post but could not, said that on edited post and still they insist on posting it. Also rude.
    Ayup. Removing content from a public forum is rude.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 07-11-2014, 10:18 AM
  2. User input in constructor bad practice?
    By kkid in forum New To Java
    Replies: 1
    Last Post: 10-23-2013, 05:09 AM
  3. help passing user input to constructor parameters
    By foxity.cf in forum New To Java
    Replies: 14
    Last Post: 02-02-2013, 06:19 PM
  4. Have user input custom variable name
    By kkid in forum New To Java
    Replies: 7
    Last Post: 11-14-2012, 12:41 AM
  5. Replies: 0
    Last Post: 11-03-2011, 08:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •