Jump to content
SingaporeBikes.com Telegram Now LIVE! Join NOW for the Last Reviews, News, Promotions & Offers in Singapore! ×
  • Join SingaporeBikes.com today! Where Singapore Bikers Unite!

    Thank you for visiting SingaporeBikes.com - the largest website in Singapore dedicated to all things related to motorcycles and biking in general.

    Join us today as a member to enjoy all the features of the website for FREE such as:

    Registering is free and takes less than 30 seconds! Join us today to share information, discuss about your modifications, and ask questions about your bike in general.

    Thank you for being a part of SingaporeBikes.com!

Recommended Posts

Posted

Hi i need some help with my program, i can't don't know whats wrong every time i save my data to arrays its ok. But the problem comes when im loading the data from .dat file to arrays the error are the first data will alaways be loaded in but the rest of the value is still null.

public static void data_save()

throws IOException

 

import java.io.*;

 

public class Customer

{

static int delay = 101;

static int delay2 = 100;

static String data_rev_name;

static String data_save_name;

static int count = 0;

static String name[] = new String[100];

static String id[] = new String[100];

static int contact[] = new int[100];

static double salary[] = new double[100];

static BufferedReader g = new BufferedReader(new InputStreamReader(System.in));

 

public static void input()

throws IOException

{

System.out.println("Welcome to Add New Customer Menu");

System.out.println("PLease Enter Customer Name ");

name[count] = g.readLine();

System.out.println("Please Enter Customer Ic");

id[count] = g.readLine();

System.out.println("Please Enter Customer Contact Number");

contact[count] = Integer.parseInt(g.readLine());

System.out.println("PLease Enter customer Salary ");

salary[count] = Double.parseDouble(g.readLine());

count++;

}

public static void show()

{

for (int x = 0; x

{

 

System.out.println("The customer name is " + name[x]);

System.out.println("The customer Ic number is " + id[x]);

System.out.println("The customer contact number is " + contact[x]);

System.out.println("The Customer Salary is " + salary[x]);

};

}

public static void search()

throws IOException

{

int choice = 0;

System.out.println("Enter Customer IC to be search");

String searchid = g.readLine();

for (int x = 0; x

if (searchid.equals(id[x]))

{

System.out.println("The customer name is " + name[x]);

System.out.println("The customer ic number is " + id[x]);

System.out.println("The customer contact number is " + contact[x]);

System.out.println("The customer salaty is " + salary[x]);

System.out.println("Do you wish to change to this record?");

System.out.println("1 Yes ");

System.out.println("2 No return to main menu");

choice = Integer.parseInt(g.readLine());

switch (choice)

{

case 1:

System.out.println("Enter New Customer Name");

String newname = g.readLine();

System.out.println("Enter New Customer IC");

String newid = g.readLine();

System.out.println("Enter New Customer Contact Number");

int newcontact = Integer.parseInt(g.readLine());

System.out.println("Enter New Customer Salary");

double newsalary = Double.parseDouble(g.readLine());

System.out.println("Do you wish to save the changes to this record?");

System.out.println("1 Yes and return to main menu ");

System.out.println("2 No ");

System.out.println("3 return to main menu");

choice = Integer.parseInt(g.readLine());

do

{

switch (choice)

{

case 1:

name[x]= newname;

id[x] = newid;

contact[x] = newcontact;

salary[x]=newsalary ;

data_save();

break;

case 2: search();

break;

case 3: mainmenu();

break;

}

} while (choice >= 3);

break;

case 2: mainmenu();

break;

 

}

}

else

{

 

System.out.println("Record not found");

System.out.println("1 to try again");

System.out.println("2 to go back main menu");

choice = Integer.parseInt(g.readLine());

switch (choice)

{

case 1: search();

break;

case 2: mainmenu();

break;

}

}

}

public static void data_del()

throws IOException

{

System.out.println("Enter id to be search");

String searchid = g.readLine();

for (int x = 0; x

if (searchid.equals(id[x]))

{

for (int y = x; y

{

name[x] = name[x + 1];

id[x] = id[x + 1];

salary[x] = salary[x + 1];

contact[x] = contact[x + 1];

};

count--;

System.out.println("Deleted");

}

}

public static void update()

throws IOException

{

 

int choice = 0;

System.out.println("Enter ic of the customer to be Edited");

String searchid = g.readLine();

for (int x = 0; x

if (searchid.equals(id[x]))

{

 

System.out.println("Record Found");

System.out.println("Enter New Customer Name");

String newname = g.readLine();

System.out.println("Enter New Customer IC");

String newid = g.readLine();

System.out.println("Enter New Customer Contact Number");

int newcontact = Integer.parseInt(g.readLine());

System.out.println("Enter New Customer Salary");

double newsalary = Double.parseDouble(g.readLine());

System.out.println("Do you wish to save the changes to this record?");

System.out.println("1 Yes and return to main menu ");

System.out.println("2 No ");

System.out.println("3 return to main menu");

choice = Integer.parseInt(g.readLine());

 

do

{

switch (choice)

{

case 1:

name[x]=newname;

id[x]=newid;

contact[x]=newcontact;

salary[x]=newsalary;

System.out.println("Record saved");

break;

case 2:

System.out.println("Recoard not saved returing to update menu");

update();

break;

}

}while (choice >=2);

}

else

{

 

System.out.println("Searching Database....");

 

 

 

delay++;

 

if (delay ==100)

{

System.out.println("Record not found");

System.out.println("1 to try again");

System.out.println("2 to go back main menu");

switch (choice)

{

case 1: search();

break;

case 2: mainmenu();

break;

}

}

}

}

 

 

 

public static void mainmenu()

throws IOException {

int choice = 0;

do

{

System.out.println("1 Add New Customer Data");

System.out.println("2 Show All Customer Record Data");

System.out.println("3 Search Customer Data");

System.out.println("4 Update Customer Data");

System.out.println("5 Delete Customer Data");

System.out.println("6 Save lasted changes to Database");

System.out.println("7 Exit");

System.out.println("Enter choice");

choice = Integer.parseInt(g.readLine());

switch (choice)

{

case 1: input();

break;

case 2: show();

break;

case 3: search();

break;

case 4: update();

break;

case 5: data_del();

break;

case 6: data_save();

break;

case 7: data_Exit();

break;

 

};

} while (choice != 7);

}

public static void data_save()

throws IOException

{

int choice = 0;

RandomAccessFile write = new

RandomAccessFile("Customer.dat", "rw");

write.writeInt(count);

for (int x = 0; x

{

write.writeUTF(name[x]);

write.writeUTF(id[x]);

write.writeInt(contact[x]);

write.writeDouble(salary[x]);

record_save();

}

 

}

public static void record_save()

throws IOException

{

int choice = 0;

System.out.println("Record Saved!u");

System.out.println("Record Saved! Press 1 to return to main menu");

choice = Integer.parseInt(g.readLine());

switch (choice)

{

case 1:

mainmenu();

break;

}

}

public static void data_retrieve()

throws IOException

{

RandomAccessFile read = new

RandomAccessFile("Customer.dat", "rw");

count = read.readInt();

for (int x = 0; x

{

id[x] = read.readUTF();

name[x] = read.readUTF();

contact[x] = read.readInt();

salary[x] = read.readDouble();

record_read();

};

 

}

public static void record_read()

throws IOException

{

int choice = 0;

System.out.println("Data Loaded Returning to Main menu");

System.out.println("Record Saved! Press 1 to return to main menu");

choice = Integer.parseInt(g.readLine());

switch (choice)

{

case 1:

mainmenu();

break;

}

}

public static void main(String args[])

throws IOException

{ int x=0;

int choice = 0;

System.out.println(" #======================================================================#");

System.out.println(" # @@ @@ @@ @@@@@@@@ #");

System.out.println(" # @@ @@ @@ @@@@@@@@ #");

System.out.println(" # @@ @@ @@ @@ #");

System.out.println(" # @@@@@@@ @@ @@ #");

System.out.println(" # @@@@@@@ @@ @@ #");

System.out.println(" # @@ @@ @@@@@@@@@ @@@@@@@@ #");

System.out.println(" # @@ @@ @@@@@@@@@ @@@@@@@@ #");

System.out.println(" #=============================== Welcome to Customer Program #");

System.out.println(" # Loading DataBase.... #");

 

System.out.println("Press 1 To Load Data and go to Mainmenu");

System.out.println("Press 2 To Create New Data and go to Mainmenu");

 

choice = Integer.parseInt(g.readLine());

 

switch (choice)

{

case 1: data_retrieve();

break;

case 2: mainmenu();

break;

 

}

 

}

public static void data_Exit()

{

System.out.println("Thank you for using Customer Program");

System.out.println("This Application System Designed For HLC.");

System.out.println("Design By: Seow Kwan Heng");

System.out.println("Student ID: 018800007164");

System.out.println("Copyright 2009 HLC Inc. All Rights Reserved.");

 

}

 

}

Class2B>TP on 22 May -Passed

Class 3>TP on 11 March -Passed

Class 2A>? start by 2009 may 22

Class 4>? start by 2009

http://www.singaporebikes.com/forums/attachment.php?attachmentid=92543&d=1235736836

I enjoy DIY:cheeky: always can reached 98475561/kwanheng"Jason"

  • 1 month later...
  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

Posted

Sorry old post but happens to see this and a little comment. Code too long. Break into different classes to be more OO. Very difficult to do tracing like this.

 

And for your public static void data_save() method, the following part

for (int x = 0; x the initial condition already force the loop to close even before entering. Hence, no record is saved at all.

 

eg. int x = 0, x is > -1 (exit for loop).

 

Apologies if i got it wrong. But code really too long and can't possibly go through every thing with the limited time I have.

"A government afraid of its citizens is a Democracy. Citizens afraid of government is tyranny!"

— Thomas Jefferson

 

:eek: :eek:

  • 3 weeks later...
Posted

TS seems to be doing C with Java. reminds me of my bad old days, when my stupid instructor thought that C and Java are interchangeable, forcing me to unlearn later. grr...

 

oh dear, TS left all his details in the program...

HELP FIND MY BIKE! Blue RXZ (New Model), completely stock FBB3668U Stolen from Block 502A MSCP Choa Chu Kang Street 51

Posted
TS seems to be doing C with Java. reminds me of my bad old days, when my stupid instructor thought that C and Java are interchangeable, forcing me to unlearn later. grr...

 

oh dear, TS left all his details in the program...

 

opps?:dot::dot:

Class2B>TP on 22 May -Passed

Class 3>TP on 11 March -Passed

Class 2A>? start by 2009 may 22

Class 4>? start by 2009

http://www.singaporebikes.com/forums/attachment.php?attachmentid=92543&d=1235736836

I enjoy DIY:cheeky: always can reached 98475561/kwanheng"Jason"

Posted

argh! my eyes!!!! studied java during my poly days and i dunno wtf am i doing..

Rat bike - A motorcycle not necessarily kept in pristine condition, often painted matt black. Minimal maintenance and mismatched parts often used.
Posted
argh! my eyes!!!! studied java during my poly days and i dunno wtf am i doing..

 

hmm so fast eye pain already... how about c programming ? i got some code need to test it out also heehee

Class2B>TP on 22 May -Passed

Class 3>TP on 11 March -Passed

Class 2A>? start by 2009 may 22

Class 4>? start by 2009

http://www.singaporebikes.com/forums/attachment.php?attachmentid=92543&d=1235736836

I enjoy DIY:cheeky: always can reached 98475561/kwanheng"Jason"

Posted

i only studied java.. cannot remember a single thing already.. other than system.out.println("hello world");

Rat bike - A motorcycle not necessarily kept in pristine condition, often painted matt black. Minimal maintenance and mismatched parts often used.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • DAIS_ShellBAU2024_Motorcycle_SingaporeBikesBanner_300x250.jpg

     
×
×
  • Create New...