473,796 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Homework] Getting data from a file to another

7 New Member
Im having trouble getting the input from one file to another.

Main file:
Expand|Select|Wrap|Line Numbers
  1. package assigment3;
  2.  
  3. /**
  4.  *
  5.  * @author mguer017
  6.  */
  7. public class Main {
  8.  
  9.     /** Creates a new instance of Main */
  10.     public Main() {
  11.     }
  12.  
  13.     public static void main(String[] args) {
  14.             Cashier c = new Cashier();
  15.         c.add ("Bread", 2.99); //adds string and double
  16.         c.add ("Chicken", 6.79); // adds string and double
  17.         c.add ("Egg", 3.07); // adds string and double
  18.         c.average();    // calculate the average price
  19.         c.tendered(20); // Twenty dollars were tendered
  20.  
  21.         System.out.println(c);    // print Cashier()
  22.  
  23.     }
  24.  
  25. }
  26.  
Cashier file: (Calculations file)
Expand|Select|Wrap|Line Numbers
  1. package assigment3;
  2.  
  3. /**
  4.  *
  5.  * @author mguer017
  6.  */
  7. public class Cashier {
  8.   /* Working on it
  9.     private static final double DOLLAR = 1.00;
  10.     private static final double QUARTER = 0.25;
  11.     private static final double DIME = 0.10;
  12.     private static final double NICKEL = 0.05;
  13.     private static final double PENNY = 0.01;
  14.  */
  15.     private String Item;
  16.     private int Total_Items, Tendered;
  17.     private double Price, average, Total;
  18.  
  19.     /** Creates a new instance of Cashier */
  20.  
  21.  
  22.     public Cashier() {
  23.     System.out.println(getItem() + " ......... $" + getPrice() + "\n" + "----------" + "\n" + getTotal());
  24.     System.out.println("Amount tendered: $" + getTendered());
  25.     System.out.println("The change is: $" + getChange());
  26.     System.out.println("There were " + getTotal_Items() + " items");
  27.     System.out.println("Average price is: $" + average);
  28.     System.out.println("Your change: $" + getChange());
  29.  
  30.  
  31.     }
  32.        public void add(String It, double Prc) {
  33.         this.Item = It;
  34.         this.Price = Prc; 
  35.  
  36.    } 
  37.  
  38.         public void tendered(int Amount) { 
  39.             Tendered = Amount;  
  40.  
  41.         }
  42.  
  43.    public double average() {
  44.        return average = Total_Items / Total;
  45.  
  46.     }
  47.  
  48.     public String getItem() {
  49.         return Item;
  50.     }
  51.  
  52.     public double getPrice() {
  53.         return Price;
  54.     }
  55.  
  56.     public double getTotal() {
  57.         return Total = Total + Price;
  58.     }
  59.  
  60.     public double getTendered() {
  61.         return Tendered;
  62.     }
  63.  
  64.     public int getTotal_Items() {
  65.         return Total_Items;
  66.     }
  67.  
  68.     public double getChange() {
  69.         return Tendered - Total;
  70.     }
  71.  
  72. }
  73.  
The output comes with 0's and null on the strings.
Expand|Select|Wrap|Line Numbers
  1. null ......... $0.0
  2. ----------
  3. 0.0
  4. Amount tendered: $0.0
  5. The change is: $0.0
  6. There were 0 items
  7. Average price is: $0.0
  8. Your change: $0.0
  9.  
Any idea what I am doing wrong?
The assignment gave me the code of the main file, so I only need to mess with the Cashier file.
Thanks,
Miguel Guerin
Mar 11 '08 #1
12 2005
BigDaddyLH
1,216 Recognized Expert Top Contributor
Consider two lines of your code:

Expand|Select|Wrap|Line Numbers
  1. Cashier c = new Cashier();
  2. c.add ("Bread", 2.99); //adds string and double
Before you can get to the second line, you must execute the first, which calls:

Expand|Select|Wrap|Line Numbers
  1. public Cashier() {
  2.     System.out.println(getItem() + " ......... $" + getPrice() + "\n" + "----------" + "\n" + getTotal());
  3.     System.out.println("Amount tendered: $" + getTendered());
  4.     System.out.println("The change is: $" + getChange());
  5.     System.out.println("There were " + getTotal_Items() + " items");
  6.     System.out.println("Average price is: $" + average);
  7.     System.out.println("Your change: $" + getChange());
  8. }
Clearly, the constructor is the last place to do that!
Mar 11 '08 #2
miguelguerin
7 New Member
Yes, I was thinking on calling each one on the main file, but the assignment gave me the code for the main file.
Expand|Select|Wrap|Line Numbers
  1. ... Here is a typical test class.
  2.  
  3. class TestCashier
  4. {
  5.     public static void main(String[] arg)
  6.     {
  7.         Cashier c = new Cashier();
  8.         c.add("Bread", 2.99);
  9.         c.add ("Chicken", 6.79);
  10.         c.add("Egg", 3.07);
  11.         c.average();
  12.         c.tendered(20); // Tenty dollars were tendered
  13.         System.out.println(c);    
  14.     }
  15. } ...
Is there a way to do it this way, or the professor is messing with us =p
This is my first semester in java so I don't have a lot of experience with the language.
Mar 11 '08 #3
BigDaddyLH
1,216 Recognized Expert Top Contributor
What do you think the last line should accomplish:

Expand|Select|Wrap|Line Numbers
  1. System.out.println(c); 
Mar 11 '08 #4
miguelguerin
7 New Member
Sorry I don't get what you mean with that. I believe that is calling the Cashier() from the Cashier.java file because I create a new Cashier() with the variable "c".
As I stated in my last post I do not have a lot of experience.
Mar 11 '08 #5
BigDaddyLH
1,216 Recognized Expert Top Contributor
Sorry I don't get what you mean with that. I believe that is calling the Cashier() from the Cashier.java file because I create a new Cashier() with the variable "c".
As I stated in my last post I do not have a lot of experience.
I appreciate you don't have much experience, but if you don't know what a line of code does, and you are just typing it in because your instructor told you to do so, you've crossed the line that separates logic from voodoo.

That statement causes Cashier's toString method to be called. Have you discussed toString methods?
Mar 11 '08 #6
miguelguerin
7 New Member
When I add this line:
Expand|Select|Wrap|Line Numbers
  1. public String toString() { 
  2. return Item + "........ $" + Price;
  3. }
It only shows the last input. Egg ..... $3.07
and on "toString() " it warns me about a "Add@overri de annotation" What does this means?
Mar 11 '08 #7
BigDaddyLH
1,216 Recognized Expert Top Contributor
When I add this line:
Expand|Select|Wrap|Line Numbers
  1. public String toString() { 
  2. return Item + "........ $" + Price;
  3. }
It only shows the last input. Egg ..... $3.07
and on "toString() " it warns me about a "Add@overri de annotation" What does this means?
You compiler is suggesting you do this:

Expand|Select|Wrap|Line Numbers
  1.     @Override
  2.     public String toString() {
  3.        ...
  4.     }
That @Override is an example of an annotation. It indicates that you are attempting to override a method. A compiler warning is just that, a warning and not an error, but the compiler has your best interests at heart. When you learn about annotations you'll see this is a safety function.

More on annotations: http://java.sun.com/j2se/1.5.0/docs/...notations.html
Mar 11 '08 #8
miguelguerin
7 New Member
Any idea on why is only showing the last input? I been reading about the toString() but I still have problems getting the concept.
Mar 11 '08 #9
BigDaddyLH
1,216 Recognized Expert Top Contributor
Any idea on why is only showing the last input? I been reading about the toString() but I still have problems getting the concept.
If your code hasn't changed much from the initial post, fields item and price can only record what was passed by the last call to method add. If you want Cashier to store multiple items and their prices, you are going to write more code: to define a class that represents an order item and store instances of it in an array or collection within Cashier.
Mar 11 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2425
by: Tek9_AK | last post by:
I need to find a way to transfer all the values of an array inside a function out of the fuction into another array. IE function splice($filename){ if(file_exists($filename)){ $value=array("0"); // clear the array. $rows=file($filename); // break text file into a rows array for($num=0;$num < count($rows); ++$num){ $column=split(";",$rows); $value=$column;}}
7
1772
by: tjshadyluver | last post by:
ok i have gotten this damn project almost done but i am frustrated on this one step. getting 18-35 together and 36-50 and so on. here is my code how can i get them combined in one line instead of writing it out a million times. please help me. i have to write a program that you type in the age and it stores it in a text file then i have to catagorize them and when i hit the read button it shows how many times 18-35 was typed in, then how...
6
2248
by: melanieab | last post by:
Hi, Easy question. It seems to me that I'm following the examples correctly, but apparently I'm not. I'm trying to retrieve the data from a row called "row" and a column called "File". This is what I have: (xFile is the int value in column File and tCat is the table) First I try: xFile = int.Parse((tCat.Rows).ToString()); Another example I found:
6
1746
by: wandafoda | last post by:
Sorry guys....another homework question and if you don't wanna help out..i understand. just to say..im' a chemical engineer and we are required at will to do whatever...thast why i have to take C++ and would like to also understand what im' doing.....to further my career.. i'll include the program after the description So my teacher gave the class an algorithm we have to manipulate......its a struct and i understand what is going on in...
33
11867
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon (NT_AUTHORITY\SYSTEM). I understand that when the service starts, no user may be logged in, but that's ok, as the app I am monitoring can only be run by a logged in user. Do I need to use WMI to get the user context of Explorer.exe or is there a...
5
1604
by: jerry | last post by:
Ok, I have a feed that I need to get the data from. The file is an html file that contains items. I need to step through the file, getting the first item, doing whatever, then the second item, and so on. Any thoughts on the best way to to this? <Feed> <Item> <ID>Data I need</ID> <Title>Data I need</Title> </Item>
17
1877
by: joebenjamin | last post by:
This is a problem I was trying to help a few friends figure out for fun. I am not sure how to go about this, but there is something I am missing here. Here is what we want the output to be: Need to read in a time period from the keyboard (for example 1.2 seconds, 3.4 seconds, or 8.37 seconds). Once the time has been read in, we want to print out the word “TICK” and then wait the designated time period and print the word “TICK” again....
8
29171
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below. Card.Java This is a simple class that represents a playing card. Card has two attributes: • rank which is a String that represents the value...
185
7137
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will return the file name given a FILE *. Questions: What would be the best name for this function?
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.