Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Why wont this work?

  • 13-08-2008 2:31pm
    #1
    Closed Accounts Posts: 75 ✭✭


    basically just creating and reading binary files using the class Worker.
    It compiles however i get null 0.0 instead of ben 500.0
    i know its annoyong to look through longish code but any pointers would be a help


    import java.io.*;

    class Worker {
    private String name;
    private double wage;

    Worker() {
    }

    Worker(String s, double w) {
    name = s; wage = w;
    }

    void Put(DataOutputStream dos) {
    // write name & age attributes to dos (at file pointer)
    try {

    dos.writeUTF(name);dos.writeDouble(wage);

    } catch (IOException e) {
    }
    }

    void get(DataInputStream dis) {
    // read name & age attributes from file dis (at file pointer,
    // which is assumed < file size)
    try {

    dis.readUTF();
    dis.readDouble();
    } catch (IOException e) {
    }
    }

    void put(){ // display on screen
    System.out.println(name + " " + wage);
    }
    }


    class Wages {

    public static void main(String[] args) {
    try {
    // Create a file called "wages.dat" with records for Ben
    // who earns 203.31, Mary who earns 187.56, & Fred who
    // earns 203.31
    DataOutputStream out = new DataOutputStream(new FileOutputStream("wages.dat"));
    Worker w = (new Worker("ben",500.00));
    w.Put(out);
    out.close();




    } catch (IOException e) {
    }
    }
    }
    import java.io.*;

    class Wages1{

    public static void main(String[] args) {

    try {
    DataInputStream in = new DataInputStream(new FileInputStream("wages"));

    Worker w = new Worker();
    while(in.available() > 0)
    {
    w.get(in);
    w.put();
    }

    in.close();

    // Display the contents of the file

    } catch (IOException e) {
    }


    }
    }


Comments

  • Registered Users, Registered Users 2 Posts: 4,287 ✭✭✭NotMe


    Use [ code][ /code] tags.

    Anyway this part you're reading the values from the file but not assigning them to variables.
    ie. name = dis.readUTF();
    try {
       dis.readUTF();
       dis.readDouble();
    } catch (IOException e) {
    }
    
    


  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    try {
    DataInputStream in = new DataInputStream(new FileInputStream("wages"));
    
    should that be wages.dat ?


  • Registered Users, Registered Users 2 Posts: 26,584 ✭✭✭✭Creamy Goodness


    any console errors?

    they usually tell you what and where you are going wrong.


  • Closed Accounts Posts: 75 ✭✭d6


    NotMe was spot on cheers.
    say i added a couple more names accomanied by their wage how wud i go about to print out the name of the highest earner?


  • Closed Accounts Posts: 81 ✭✭dzy


    While reading in the workers, keep track of the current highest wage and the name of the worker who earns that wage. At the end, print out the name.


  • Advertisement
  • Closed Accounts Posts: 75 ✭✭d6


    do i create a method or should i try do it the main method?


  • Closed Accounts Posts: 75 ✭✭d6


    ok got it thanks 4 the help


  • Registered Users, Registered Users 2 Posts: 4,276 ✭✭✭damnyanks


    Hey I'm not sure what you're using to write this code. I would however suggest using eclipse or netbeans. You can then stick in some breakpoints and step through the code to find logic problemss quickly.


Advertisement