Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Code Complete?

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Code Complete?

    Hello, I am currently doing an assignment where i was given 3 java codes that was similarly to my assignment. I changed those codes to fit my needs and i was wondering if anyone had any better ideas that i can do with my code.
    We had to make a .txt file that bascially defined a game you put in .
    We had to include price, manufacturer, quantity, discount price, discount quantity, then two other programs to read and pull up the .txt file.
    So.... here is my code thus far , I am open to suggestions to include more things in this code to better define the game. Also, I was wondering why the .txt file when i create it it reads a bunch of weird characters for instance "¬í sr EmployeeÔ½ªÿŸˆ; D QOHD QOH2I idNumD priceD price2L namet Ljava/lang/String;L name2q ~ L name3q ~ xp@. @. @N @K€ t Lawlst Lawlst C" this was the file it made when i viewed it from notepad using Lawls as game name and manufacturer just for example. I thought it would just list the names, prices, quantities in the txt file. If someone could explain this to me that would be cool.
    import java.io.*;
    public class Employee implements Serializable
    {
       int idNum;
       String name;
       String name2;
       String name3;
       double price;
       double price2;
       double QOH;
       double QOH2;
       public Employee(int num, String name, String name2, String name3, double price, double price2, double QOH, double QOH2)
       {
          idNum = num;
          this.name = name;
          this.name2 = name2;
          this.name3 = name3;
          this.price = price;
          this.price2 = price2;
          this.QOH = QOH;
          this.QOH2 = QOH2;
       }
       public void display()
       {
    	   System.out.println("serial # " + idNum );
    	   System.out.println(" Game name:" + name);
    	   System.out.println(" Manufacturer/publisher: " + name2);
    	   System.out.println(" Original price: " + price );
    	   System.out.println(" Quantity: " + QOH );
    	   System.out.println(" Discounted price: " + price2);
    	   System.out.println( " Quantity: " + QOH2);
    	   System.out.println(" Game rating: " + name3);
       }
    }
    import java.io.*;
    import java.util.*;
    public class CreateEmployeeObjectFile
    {
       public static void main(String[] args) throws IOException
       {
          ObjectOutputStream output =
             new ObjectOutputStream
             (new FileOutputStream("game.txt"));
          Employee emp;
          int num = 0;
          final int QUIT = 1337;
          String name;
          String name2;
          String name3;
          double price;
          double QOH;
          double price2;
          double QOH2;
          Scanner in = new Scanner(System.in);
          System.out.print("Assign the games serial number: " + QUIT + " to quit ");
          num = in.nextInt();
          while(num != QUIT)
          { 
              System.out.print("Enter the game name: ");   
              name = in.next();
              System.out.print("Enter the game manufacturer/publisher: ");   
              name2 = in.next();
              System.out.print("Please type the rating of the game (C,E,E+,T,M,Ao,RP): ");   
              name3 = in.next();
              System.out.print("Enter the price of the game: ");
              price = in.nextDouble();
              System.out.print("Enter how many are available in stock: ");
              QOH = in.nextDouble();
              System.out.print("Enter the discounted price of the game: ");
              price2 = in.nextDouble();
              System.out.print("Enter how many are available in stock discounted: ");
              QOH2 = in.nextDouble();
             emp = new Employee(num, name, name2, name3, price, price2, QOH, QOH2);
             output.writeObject(emp);
             System.out.print("Assign the game a number: " + QUIT + " to quit ");
             num = in.nextInt();
          }
          output.close();
       }
    }
    import java.io.*;
    public class ReadEmployeeObjectFile 
    {
       public static void main(String[] args)
          throws IOException, ClassNotFoundException
       {
            ObjectInputStream in = new
             ObjectInputStream
             (new FileInputStream("Game.txt"));
          Employee emp;
          try
          {
            while(true)
            {
               emp = (Employee)in.readObject();
               emp.display();
            }
          }
          catch(EOFException e)
          {
            in.close();
          }
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Code Complete?

    why the .txt file when i create it it reads a bunch of weird characters
    The contents of the file is not text. It is what is inside of a class object that was written using the writeObject() method.

    If you want text you can read with notepad, you need to use a class and methods that write text like PrintWriter.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2017, 11:11 PM
  2. Complete beginner (college student) - code not working!!
    By javanewbie085 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2013, 09:19 AM
  3. How to print each letter of string in reverse order in separate line?
    By Razzle in forum Loops & Control Statements
    Replies: 3
    Last Post: October 31st, 2012, 05:32 PM
  4. How to import code in NetBeans and make it complete.
    By Chemical91 in forum Java IDEs
    Replies: 3
    Last Post: June 28th, 2012, 07:07 AM
  5. How to complete code
    By Shay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2010, 01:59 PM