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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: URGENT! Please help me. Thank You!

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

    Post URGENT! Please help me. Thank You!

    Okay, i got this coding from a friend. He is my old old old senior. Since i am really a total newbie in the course im taking at the moment, I asked him for a help. But he's not in the same college as i am anymore (he further his study away from our place) and he's been busy with his on project and group project, so he gave me this coding as a guide for my homework..because it is almost the same.

    The things is I keep getting this on my command prompt(cmd),
    "Note: Question5.java uses or overrides a deprecated API.
    Note: Recompile with =Xlint:deprecation for details"

    What is it actually? How can i fix it? Where to fix it? Please provide a step-by-step instruction for me for it is hard for me to understand this new subject

    This are the codes..


    import java.io.*;

    public class Question5{

    public static DataInputStream k = new DataInputStream(System.in);
    public static String IsbNo[] = new String[100];
    public static String name[] = new String[100];
    public static String category[] = new String[100];
    public static int x=1;


    public static void main(String[] args)throws IOException {
    menu();

    }

    public static void menu()throws IOException {

    spacing();
    System.out.println("Welcome to My Library");
    System.out.println("MENU");
    System.out.println("[1]-Add\n[2]-Edit\n[3]-Delete\n[4]-Search\n[5]-View\n[6]-Exit");

    System.out.print("Select a menu: ");
    int m = Integer.parseInt(k.readLine());

    switch(m) {

    case 1: //ADD
    addRecord();
    break;
    case 2: //EDIT
    editRec();
    break;
    case 3: //DELETE
    DeleteRecord();
    break;
    case 4: //SEARCH
    SearchRecord();
    break;
    case 5: //VIEW
    viewRecord();
    break;
    case 6: //EXIT
    break;
    default:
    break;

    }

    }

    public static void DeleteRecord()throws IOException {

    boolean retbol = false;
    int indx=0;

    spacing();
    System.out.print("-->DELETE RECORDS<--\n\n\n");
    System.out.print("Search ISBNo.: ");
    String sisbno = k.readLine();

    for(int i=1;i<IsbNo.length;i++) {
    if(IsbNo[i] !=null) {

    if(sisbno.equalsIgnoreCase(IsbNo[i])) {
    retbol = true;
    indx = i;
    }

    }
    }

    if(retbol==true) {

    System.out.print("ISBNo.: "+IsbNo[indx].toUpperCase()+ " ");
    System.out.print("Name: "+name[indx].toUpperCase()+ " ");
    System.out.print("Category: "+category[indx].toUpperCase()+ " ");
    System.out.println("");

    System.out.print("Save this record?[y]-y/[n]-no: ");
    String Q = k.readLine();

    if(Q.equalsIgnoreCase("y")) {

    IsbNo[indx] = null;
    name[indx] = null;
    category[indx] = null;


    for(int j=indx+1;j<IsbNo.length;j++) {

    IsbNo[j-1] = IsbNo[j];
    name[j-1] = name[j];
    category[j-1] = category[j];

    }

    System.out.println("Record succesfully deleted!");
    System.out.println("\n\n\n<Press Enter>");
    String Q1 = k.readLine();
    spacing();
    menu();

    }else {

    spacing();
    menu();
    }

    }else {

    System.out.print("ISBNo. Number not found!");
    System.out.println("\n\n\n<Press Enter>");
    String Q = k.readLine();
    spacing();
    menu();

    }


    }


    public static void SearchRecord()throws IOException {
    boolean retbol = false;
    int indx=0;

    spacing();
    System.out.print("-->SEARCH RECORDS<--\n\n\n");
    System.out.print("Search ISBNo. : ");
    String sisbno = k.readLine();

    for(int i=1;i<IsbNo.length;i++) {
    if(IsbNo[i] !=null) {

    if(sisbno.equalsIgnoreCase(IsbNo[i])) {
    retbol = true;
    indx = i;
    }

    }
    }

    if(retbol==true) {

    System.out.print("ISBNo.: "+IsbNo[indx].toUpperCase()+ " ");
    System.out.print("Name: "+name[indx].toUpperCase()+ " ");
    System.out.print("Category: "+category[indx].toUpperCase()+ " ");
    System.out.println("");

    System.out.println("\n\n\n<Press Enter>");
    String Q = k.readLine();
    spacing();
    menu();

    }else {

    System.out.print("ISBNo. Number not found!");
    System.out.println("\n\n\n<Press Enter>");
    String Q = k.readLine();
    spacing();
    menu();

    }
    }

    public static void addRecord()throws IOException {

    spacing();
    System.out.print("-->ADD RECORDS<--\n\n\n");
    System.out.print("ISBNo.: ");
    String sisbno = k.readLine();

    System.out.print("Book NAME: ");
    String sname = k.readLine();

    System.out.print("Category: ");
    String scategory = k.readLine();

    System.out.print("Are you sure to save this record?[y]-y/[n]-no: ");
    String Q = k.readLine();

    if(Q.equalsIgnoreCase("y")) {

    IsbNo[x] = sisbno;
    name[x] = sname;
    category[x] = scategory;
    x++;
    spacing();
    menu();
    }else if(Q.equalsIgnoreCase("n")) {
    spacing();
    menu();

    }

    }

    public static void spacing() {
    System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n");
    }

    public static void viewRecord()throws IOException {
    boolean ctr=false;

    spacing();
    System.out.print("-->VIEW RECORDS<--\n\n\n");

    for(int i=1;i<IsbNo.length;i++) {

    if(IsbNo[i] !=null) {
    System.out.print("ISBNo.: "+IsbNo[i].toUpperCase()+ " ");
    System.out.print("Name: "+name[i].toUpperCase()+ " ");
    System.out.print("Category: "+category[i].toUpperCase()+ " ");
    System.out.println("");
    ctr=true;
    }

    }

    if(ctr==false) {

    System.out.print("\nEMPTY DATABASE NO RECORDS FOUND!");

    }


    System.out.println("\n\n\n<Press Enter>");
    String Q = k.readLine();
    spacing();
    menu();

    }

    public static void editRec()throws IOException {
    boolean retbol = false;
    int indx=0;

    spacing();
    System.out.print("-->EDIT RECORDS<--\n\n\n");
    System.out.print("Search ISBNo. Num: ");
    String sisbno = k.readLine();

    for(int i=1;i<IsbNo.length;i++) {
    if(IsbNo[i] !=null) {

    if(sisbno.equalsIgnoreCase(IsbNo[i])) {
    retbol = true;
    indx = i;
    }

    }
    }

    if(retbol==true) {

    System.out.print(IsbNo[indx] + " " +name[indx]+ " " +category[indx]+ " \n\n\n\n");


    System.out.print("Book Name: ");
    String snameedit = k.readLine();

    System.out.print("Category: ");
    String scategoryedit = k.readLine();

    System.out.print("Are you sure to update this record?[y]-y/[n]-no: ");
    String Q = k.readLine();

    if(Q.equalsIgnoreCase("y")) {


    name[indx] = snameedit;
    category[indx] = scategoryedit;

    spacing();
    menu();
    }


    }else {

    System.out.print("ISBNo. not found!");
    System.out.println("\n\n\n<Press Enter>");
    String Q = k.readLine();
    spacing();
    menu();
    }

    }

    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: URGENT! Please help me. Thank You!

    I suggest that you avoid borrowing code, that instead you write it yourself. Sure it's fine to borrow ideas, but if you don't write it from the ground up, especially at this level, you'll never understand it and won't learn programming.

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

    Default Re: URGENT! Please help me. Thank You!

    I'm not borrowing. He want me to use it as a guide. So i wanna see how this code of his work to get the idea. But when i try to run his code, i get that error instead. I didnt change anything, i just merely run it on cmd. That's it.

    --- Update ---

    And this is my actually works...which happen to have errors.

    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;

    import java.awt.GridLayout;
    import java.awt.BorderLayout;

    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.Scanner;

    public class DrinksSystem
    {
    JFrame fraMain;
    JLabel lblDRINKS_ID, lblDRINKS_NAME, lblDRINKS_PRICE;
    JTextField txtDRINKS_ID, txtDRINKS_NAME, txtDRINKS_PRICE;
    JTextArea txtaOutput;
    JButton btnAdd, btnDisplay, btnDelete, btnSearch, btnSave, btnTransaction;
    JPanel buttonJPanel, cenPanel;

    public DrinksSystem()
    {
    fraMain = new JFrame ("Coffee Bean Drinks" );
    fraMain.setLayout(new BorderLayout());

    cenPanel = new JPanel();
    cenPanel.setLayout(new GridLayout(6,2,6,2)); // 6rows 2columns

    buttonJPanel = new JPanel();
    buttonJPanel.setLayout(new GridLayout(1,5,2,2)); //1row5columns

    lblDRINKS_ID = new JLabel("Drinks ID :", JLabel.LEFT);
    lblDRINKS_NAME = new JLabel("Drinks Name :" , JLabel.LEFT);
    lblDRINKS_PRICE = new JLabel("Drinks Price: $" , JLabel.LEFT);

    txtDRINKS_ID = new JTextField(18);
    txtDRINKS_NAME = new JTextField(20);
    txtDRINKS_PRICE= new JTextField(25);
    txtaOutput = new JTextArea(10,30);

    btnAdd = new JButton("Add");
    btnDisplay = new JButton("Display");
    btnDelete = new JButton("Delete");
    btnSearch = new JButton("Search");
    btnSave = new JButton("Save");
    btnTransaction = new JButton("Transaction");
    btnSave.setEnabled(false);

    buttonJPanel.add(btnAdd);
    buttonJPanel.add(btnDisplay);
    buttonJPanel.add(btnDelete);
    buttonJPanel.add(btnSearch);
    buttonJPanel.add(btnSave);
    buttonJPanel.add(btnTransaction);

    cenPanel.add(lblDRINKS_ID);
    cenPanel.add(txtDRINKS_ID);
    cenPanel.add(lblDRINKS_NAME);
    cenPanel.add(txtDRINKS_NAME);
    cenPanel.add(lblDRINKS_PRICE);
    cenPanel.add(txtDRINKS_PRICE);

    fraMain.add(cenPanel, BorderLayout.CENTER);
    fraMain.add(new JScrollPane(txtaOutput), BorderLayout.EAST);
    fraMain.add(buttonJPanel, BorderLayout.SOUTH);

    btnAdd.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    addDrinks();
    }

    catch(Exception e )
    {
    }
    }
    }
    );

    btnSave.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    }

    catch(Exception e )
    {
    }
    }
    }
    );

    btnSearch.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    searchDrinks();
    }

    catch(Exception e )
    {
    }
    }
    }
    );

    btnDisplay.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    displayDrinks();
    }

    catch(Exception e )
    {
    }
    }
    }
    );

    btnDelete.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    deleteDrinks();
    }

    catch(Exception e )
    {
    }
    }
    }
    );

    fraMain.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    fraMain.setSize( 500, 280 );
    fraMain.setVisible( true );
    }

    public void ClearForm()
    {
    txtDRINKS_ID.setText("");
    txtDRINKS_NAME.setText("");
    txtDRINKS_PRICE.setText("");
    }

    public static void main(String []args)
    {
    DrinksSystem Drinks = new DrinksSystem();
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT! Please help me. Thank You!

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    happen to have errors.
    Please copy the full text of the error messages and paste them here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    And the errors are...

    DrinksSystem.java:81: error: cannot find symbol
    addDrinks();
    DrinksSystem.java:112: error: cannot find symbol
    searchDrinks();
    DrinksSystem.java:128: error: cannot find symbol
    displayDrinks();
    DrinksSystem.java:144: error: cannot find symbol
    deleteDrinks();

    symbol: method deleteDrinks()
    4 errors

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT! Please help me. Thank You!

    The "cannot find symbol" error message means that the compiler can not find a definition for the methods listed in the error messages. You need to provide definitions for the missing methods.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    So, i add this for the 4errors thingy..
    public void searchDrinks() throws Exception
    {
    	int DRINKS_ID= 0, searchDRINKS_ID = 0, found = 0;
    	String DRINKS_NAME = "";
    	double DRINKS_PRICE = 0.0;
    	String output = "";
     
    	File sfile;
    	Scanner input, searchInput;
     
    	sfile = new File("DrinksSystem.txt");
    	input = new Scanner(sfile);
     
    	if(!sfile.exists())//if Files does not exist
    	{
    		System.out.println("\nThe file\"" + sfile.getName() + "\" cannot be opened\n");
    		System.exit(0);
    	}
     
    	else
    	{
    		searchDRINKS_ID = Integer.parseInt( txtDRINKS_ID.getText() ) ;
     
    		//the useDelimiter will read everything till the "," and "\n"
    		input.useDelimiter("[,\\n]");
     
    		//Read data from a File
    		while(input.hasNext() && found == 0){
    		DRINKS_ID = input.nextInt();
    		DRINKS_NAME = input.next();
    		DRINKS_PRICE = Double.parseDouble(input.next());
     
    		if(searchDRINKS_ID == DRINKS_ID)
    		found = 1;
    	}
     
    	if(found == 1)
    	{		
    		txtDRINKS_NAME.setText( DRINKS_NAME );
    		txtDRINKS_PRICE.setText( Double.toString( DRINKS_PRICE ));
    		output = output + DRINKS_ID + " \t" + DRINKS_NAME + "\t" + "$"+DRINKS_PRICE + "\n";
    		txtaOutput.setText("DRINKS_ID"+"\t"+"DRINKS_NAME"+"\t"+"DRINKS_PRICE" +"\n" +output);
     
    	}
     
    	else 
    	{
    		txtaOutput.setText("Record Not Found!");
    	}
     
    	input.close(); //close the file
    }
     
    public void displayDrinks() throws Exception
    {
     	ClearForm();
    	int DRINKS_ID = 0;
    	String DRINKS_NAME;
    	double DRINKS_PRICE;
    	String output = "";
     
    	File Sfile;
    	Scanner input;
     
    	Sfile = new File("DrinksSystem.txt");
    	input = new Scanner(Sfile);
     
    	if(!Sfile.exists())//if Files does not exist
    	{
    		System.out.println("\nThe file\"" + Sfile.getName()+ "\" cannot be opened\n");
    		System.exit(0);	
    	}
     
    	else
    	{
    		//the useDelimiter will read everything till the "," and "\n"
    		input.useDelimiter("[,\\n]");
    		output = "DRINKS_ID \tDRINKS_NAME \tDRINKS_PRICE\n";
     
    		//Read data from a file
    	    while(input.hasNext())
    		{
    			DRINKS_ID = input.nextInt();
    			DRINKS_NAME = input.next();
    			DRINKS_PRICE = Double.parseDouble(input.next());
    			output = output + ALBUM_ID + " \t" + ALBUM_NAME + "\t" +"$"+ ALBUM_PRICE + "\n";
    		}
        }
     
    	input.close(); //close the file
    	txtaOutput.setText( output );
    }
     
     
    public void addDrinks() throws Exception
    {
    	int DRINKS_ID = 0;
    	String DRINKS_NAME;
    	double DRINKS_PRICE;
    	File Sfile;
    	PrintWriter output;
     
    	Sfile = new File("DrinksSystem.txt");
     
    	if( !Sfile.exists())
    	{
    		//to create a new file if the file does not exists
    		Sfile.createNewFile();
    		System.out.println("\nNew file\"" + Sfile.getName()+"\" has been created to the\n");
    		System.out.println("current directory\n");
    		output = new PrintWriter(Sfile);
    		DRINKS_ID = 1; //set the DRINKS_ID to 0001
    	}
     
    	else
    	{
    		Scanner finput = new Scanner(Sfile);
     
    		//the useDelimiter will read everything till the "," and "\n"
    		finput.useDelimiter("[,\\n]");
     
    		//Read data from a file
    		while(finput.hasNext())
    		{
    			DRINKS_ID = finput.nextInt();
    			DRINKS_NAME = finput.next();
    			DRINKS_PRICE = Double.parseDouble(finput.next());
    		}
     
    		DRINKS_ID = DRINKS_ID + 1;
    		boolean append = true;
     
     
    		//create a file for appending
    		FileWriter fwriter = new FileWriter(Sfile, append);
    		output = new PrintWriter(fwriter);
     
    	}
     
    	DRINKS_NAME = txtDRINKS_NAME.getText();
    	DRINKS_PRICE = Double.parseDouble(txtDRINKS_PRICE.getText());
     
    	output.println(DRINKS_ID + "," + DRINKS_NAME + "," + DRINKS_PRICE);//Sent the data to the DrinksSystem.txt
    	output.close(); //Close the file
    	ClearForm();
    	displayDrinks();	 
    }
     
    public void deleteDrinks() throws Exception
    {	
      	int DRINKS_ID = 0, searchDRINKS_ID = 0, found = 0;
       	String DRINKS_NAME = " ";
       	double DRINKS_PRICE = 0.0;
       	String diplaying = "";
     
       	File sfile, tempFile;
       	PrintWriter output;
       	Scanner input, searchInput;
     
       	sfile = new File("RockMusicSystem.txt");
       	input = new Scanner(sfile);
     
       	tempFile = new File ("temp.txt");// to create new Temporary 
       	output = new PrintWriter(tempFile);
     
       	if(!sfile.exists())
    	{
       		System.out.println("\nThe File \""+ sfile.getName()+"\"cannot be opened\n");
       	}
     
    	else
    	{
        	searchDRINKS_ID = Integer.parseInt( txtDRINKS_ID.getText() ) ;
     
        	// the useDelimiter will read everything till the "," and "\n"
     
        	input.useDelimiter("[,\\n]");
     
        	//Read data from a File
     
        	while(input.hasNext())
    		{
        		DRINKS_ID = input.nextInt();
        		DRINKS_NAME = input.next();
        		DRINKS_PRICE = Double.parseDouble(input.next());
     
     			if(searchDRINKS_ID !=DRINKS_ID)
    			{
        			output.println(DRINKS_ID+ "," +DRINKS_NAME+ ","+DRINKS_PRICE);
        		}
     
        		else
    			{
    				found = 1;
    				txtDRINKS_NAME.setText( DRINKS_NAME );
    				txtDRINKS_PRICE.setText( Double.toString( DRINKS_PRICE ));
    				diplaying = DRINKS_ID + " \t" + DRINKS_NAME + "\t" + "$"+DRINKS_PRICE + "\n";
    				txtaOutput.setText("DRINKS_ID"+"\t"+"DRINKS_NAME"+"\t"+"DRINKS_PRICE" +"\n" +diplaying+"\n"+"Records Deleted!");
        		}	
        	}		    
     
    		// If record are not found
        	if(found == 0)
    		{
        		txtaOutput.setText("Record Not Found!");
        	}	
     
    		else 
        	{		
        		System.out.println("");
        	}
     
        	//close the both input and output files
        	input.close();
        	output.close();
     
        	sfile.delete(); // Delete DrinksSystem.txt
        	tempFile.renameTo(sfile); // Rename temp.txt to DrinksSystem.txt
     
        }  	   
    }
    Last edited by Norm; November 26th, 2012 at 09:15 PM. Reason: added code tags

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT! Please help me. Thank You!

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: URGENT! Please help me. Thank You!

    It's somewhat of an intelligence test -- to see if the original poster can follow simple orders.

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    So all in all, this is my codes. BUT, i get a LOT of errors instead...been doing this for 2 weeks (along with the other homework for Java Script in creating website which i already completed it) and i couldnt understand where or how this and that.

     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;
     
    import java.awt.GridLayout;
    import java.awt.BorderLayout;
     
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.Scanner;
     
    public class DrinksSystem
    {
    JFrame fraMain;
    JLabel lblDRINKS_ID, lblDRINKS_NAME, lblDRINKS_PRICE;
    JTextField txtDRINKS_ID, txtDRINKS_NAME, txtDRINKS_PRICE;
    JTextArea txtaOutput;
    JButton btnAdd, btnDisplay, btnDelete, btnSearch, btnSave, btnTransaction;
    JPanel buttonJPanel, cenPanel;
     
    public DrinksSystem()
    {
    fraMain = new JFrame ("Coffee Bean Drinks" );
    fraMain.setLayout(new BorderLayout());
     
    cenPanel = new JPanel();
    cenPanel.setLayout(new GridLayout(6,2,6,2)); // 6rows 2columns
     
    buttonJPanel = new JPanel();
    buttonJPanel.setLayout(new GridLayout(1,5,2,2)); //1row5columns
     
    lblDRINKS_ID = new JLabel("Drinks ID :", JLabel.LEFT);
    lblDRINKS_NAME = new JLabel("Drinks Name :" , JLabel.LEFT);
    lblDRINKS_PRICE = new JLabel("Drinks Price: $" , JLabel.LEFT);
     
    txtDRINKS_ID = new JTextField(18);
    txtDRINKS_NAME = new JTextField(20);
    txtDRINKS_PRICE= new JTextField(25);
    txtaOutput = new JTextArea(10,30);
     
    btnAdd = new JButton("Add");
    btnDisplay = new JButton("Display");
    btnDelete = new JButton("Delete");
    btnSearch = new JButton("Search");
    btnSave = new JButton("Save");
    btnTransaction = new JButton("Transaction");
    btnSave.setEnabled(false);
     
    buttonJPanel.add(btnAdd);
    buttonJPanel.add(btnDisplay);
    buttonJPanel.add(btnDelete);
    buttonJPanel.add(btnSearch);
    buttonJPanel.add(btnSave);
    buttonJPanel.add(btnTransaction);
     
    cenPanel.add(lblDRINKS_ID);
    cenPanel.add(txtDRINKS_ID);
    cenPanel.add(lblDRINKS_NAME);
    cenPanel.add(txtDRINKS_NAME);
    cenPanel.add(lblDRINKS_PRICE);
    cenPanel.add(txtDRINKS_PRICE);
     
    fraMain.add(cenPanel, BorderLayout.CENTER);
    fraMain.add(new JScrollPane(txtaOutput), BorderLayout.EAST);
    fraMain.add(buttonJPanel, BorderLayout.SOUTH);
     
    btnAdd.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    addDrinks();
    }
     
    catch(Exception e )
    {
    }
    }
    }
    );
     
    btnSave.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    }
     
    catch(Exception e )
    {
    }
    }
    }
    );
     
    btnSearch.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    searchDrinks();
    }
     
    catch(Exception e )
    {
    }
    }
    }
    );
     
    btnDisplay.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    displayDrinks();
    }
     
    catch(Exception e )
    {
    }
    }
    }
    );
     
    btnDelete.addActionListener( new ActionListener ()
    {
    public void actionPerformed( ActionEvent event)
    {
    try
    {
    deleteDrinks();
    }
     
    catch(Exception e )
    {
    }
    }
    }
    );
     
    fraMain.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    fraMain.setSize( 500, 280 );
    fraMain.setVisible( true );
    }
     
    public void ClearForm()
    {
    txtDRINKS_ID.setText("");
    txtDRINKS_NAME.setText("");
    txtDRINKS_PRICE.setText("");
    }
     
    public static void main(String []args)
    {
    DrinksSystem Drinks = new DrinksSystem();
    }
    }
     
    public void searchDrinks() throws Exception
    {
    int DRINKS_ID= 0, searchDRINKS_ID = 0, found = 0;
    String DRINKS_NAME = "";
    double DRINKS_PRICE = 0.0;
    String output = "";
     
    File sfile;
    Scanner input, searchInput;
     
    sfile = new File("DrinksSystem.txt");
    input = new Scanner(sfile);
     
    if(!sfile.exists())//if Files does not exist
    {
    System.out.println("\nThe file\"" + sfile.getName() + "\" cannot be opened\n");
    System.exit(0);
    }
     
    else
    {
    searchDRINKS_ID = Integer.parseInt( txtDRINKS_ID.getText() ) ;
     
    //the useDelimiter will read everything till the "," and "\n"
    input.useDelimiter("[,\\n]");
     
    //Read data from a File
    while(input.hasNext() && found == 0){
    DRINKS_ID = input.nextInt();
    DRINKS_NAME = input.next();
    DRINKS_PRICE = Double.parseDouble(input.next());
     
    if(searchDRINKS_ID == DRINKS_ID)
    found = 1;
    }
     
    if(found == 1)
    {	
    txtDRINKS_NAME.setText( DRINKS_NAME );
    txtDRINKS_PRICE.setText( Double.toString( DRINKS_PRICE ));
    output = output + DRINKS_ID + " \t" + DRINKS_NAME + "\t" + "$"+DRINKS_PRICE + "\n";
    txtaOutput.setText("DRINKS_ID"+"\t"+"DRINKS_NAME"+ "\t"+"DRINKS_PRICE" +"\n" +output);
     
    }
     
    else 
    {
    txtaOutput.setText("Record Not Found!");
    }
     
    input.close(); //close the file
    }
     
    public void displayDrinks() throws Exception
    {
    ClearForm();
    int DRINKS_ID = 0;
    String DRINKS_NAME;
    double DRINKS_PRICE;
    String output = "";
     
    File Sfile;
    Scanner input;
     
    Sfile = new File("DrinksSystem.txt");
    input = new Scanner(Sfile);
     
    if(!Sfile.exists())//if Files does not exist
    {
    System.out.println("\nThe file\"" + Sfile.getName()+ "\" cannot be opened\n");
    System.exit(0);	
    }
     
    else
    {
    //the useDelimiter will read everything till the "," and "\n"
    input.useDelimiter("[,\\n]");
    output = "DRINKS_ID \tDRINKS_NAME \tDRINKS_PRICE\n";
     
    //Read data from a file
    while(input.hasNext())
    {
    DRINKS_ID = input.nextInt();
    DRINKS_NAME = input.next();
    DRINKS_PRICE = Double.parseDouble(input.next());
    output = output + ALBUM_ID + " \t" + ALBUM_NAME + "\t" +"$"+ ALBUM_PRICE + "\n";
    }
    }
     
    input.close(); //close the file
    txtaOutput.setText( output );
    }
     
     
    public void addDrinks() throws Exception
    {
    int DRINKS_ID = 0;
    String DRINKS_NAME;
    double DRINKS_PRICE;
    File Sfile;
    PrintWriter output;
     
    Sfile = new File("DrinksSystem.txt");
     
    if( !Sfile.exists())
    {
    //to create a new file if the file does not exists
    Sfile.createNewFile();
    System.out.println("\nNew file\"" + Sfile.getName()+"\" has been created to the\n");
    System.out.println("current directory\n");
    output = new PrintWriter(Sfile);
    DRINKS_ID = 1; //set the DRINKS_ID to 0001
    }
     
    else
    {
    Scanner finput = new Scanner(Sfile);
     
    //the useDelimiter will read everything till the "," and "\n"
    finput.useDelimiter("[,\\n]");
     
    //Read data from a file
    while(finput.hasNext())
    {
    DRINKS_ID = finput.nextInt();
    DRINKS_NAME = finput.next();
    DRINKS_PRICE = Double.parseDouble(finput.next());
    }
     
    DRINKS_ID = DRINKS_ID + 1;
    boolean append = true;
     
     
    //create a file for appending
    FileWriter fwriter = new FileWriter(Sfile, append);
    output = new PrintWriter(fwriter);
     
    }
     
    DRINKS_NAME = txtDRINKS_NAME.getText();
    DRINKS_PRICE = Double.parseDouble(txtDRINKS_PRICE.getText());
     
    output.println(DRINKS_ID + "," + DRINKS_NAME + "," + DRINKS_PRICE);//Sent the data to the DrinksSystem.txt
    output.close(); //Close the file
    ClearForm();
    displayDrinks();	
    }
     
    public void deleteDrinks() throws Exception
    {	
    int DRINKS_ID = 0, searchDRINKS_ID = 0, found = 0;
    String DRINKS_NAME = " ";
    double DRINKS_PRICE = 0.0;
    String diplaying = "";
     
    File sfile, tempFile;
    PrintWriter output;
    Scanner input, searchInput;
     
    sfile = new File("DrinksSystem.txt");
    input = new Scanner(sfile);
     
    tempFile = new File ("temp.txt");// to create new Temporary 
    output = new PrintWriter(tempFile);
     
    if(!sfile.exists())
    {
    System.out.println("\nThe File \""+ sfile.getName()+"\"cannot be opened\n");
    }
     
    else
    {
    searchDRINKS_ID = Integer.parseInt( txtDRINKS_ID.getText() ) ;
     
    // the useDelimiter will read everything till the "," and "\n"
     
    input.useDelimiter("[,\\n]");
     
    //Read data from a File
     
    while(input.hasNext())
    {
    DRINKS_ID = input.nextInt();
    DRINKS_NAME = input.next();
    DRINKS_PRICE = Double.parseDouble(input.next());
     
    if(searchDRINKS_ID !=DRINKS_ID)
    {
    output.println(DRINKS_ID+ "," +DRINKS_NAME+ ","+DRINKS_PRICE);
    }
     
    else
    {
    found = 1;
    txtDRINKS_NAME.setText( DRINKS_NAME );
    txtDRINKS_PRICE.setText( Double.toString( DRINKS_PRICE ));
    diplaying = DRINKS_ID + " \t" + DRINKS_NAME + "\t" + "$"+DRINKS_PRICE + "\n";
    txtaOutput.setText("DRINKS_ID"+"\t"+"DRINKS_NAME"+ "\t"+"DRINKS_PRICE" +"\n" +diplaying+"\n"+"Records Deleted!");
    }	
    }	
     
    // If record are not found
    if(found == 0)
    {
    txtaOutput.setText("Record Not Found!");
    }	
     
    else 
    {	
    System.out.println("");
    }
     
    //close the both input and output files
    input.close();
    output.close();
     
    sfile.delete(); // Delete DrinksSystem.txt
    tempFile.renameTo(sfile); // Rename temp.txt to DrinksSystem.txt
     
    } 
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT! Please help me. Thank You!

    i get a LOT of errors
    Post any error messages you want help with.

    The code needs to be formatted. All the statements should not start in the first column. The formatting was lost when the code was copied and pasted. Try again to get properly formatted code,
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    Yea, perhaps simple for some people but not me *sigh* This course is a whole different one. I mean i just get in a college and take this course just 2months ago, after completing my A Level, and im stuck with this kind of work which the lecturer expect us to understand it already without knowing there is a few of us like me who feel it so hard to understand.

  13. #13
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: URGENT! Please help me. Thank You!

    I see two important issues:

    1. Most importantly, you're ignoring your exceptions by having empty catch blocks. At least print the stack trace via e.printStackTrace(). Otherwise what you're doing is the programming equivalent of driving a car with blindfolds on.
    2. Your code is all left justified. Certainly your instructor is asking you to submit formatted code so that he can be able to read it. Since you're asking us for free help, it's not too much for us to ask this of you as well.


    Also, you'll want to leave all mention about "urgent" or "asap" out of your posts here since it isn't our urgency, and your question is no more important than any other question here.

  14. #14
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    I copy paste this directly from my notepad+. It isnt like that in my notepad+. I dont know why it became that when i posted up here.

    --- Update ---

    So, i try to post up again. I hope it looks the same as what i did in notepad+

     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;
     
    import java.awt.GridLayout;
    import java.awt.BorderLayout;
     
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.Scanner;
     
    public class DrinksSystem
    {
       JFrame fraMain;
       JLabel lblDRINKS_ID, lblDRINKS_NAME, lblDRINKS_PRICE;
       JTextField txtDRINKS_ID, txtDRINKS_NAME, txtDRINKS_PRICE;
       JTextArea txtaOutput;
       JButton btnAdd, btnDisplay, btnDelete, btnSearch, btnSave, btnTransaction;
       JPanel buttonJPanel, cenPanel;
     
       public DrinksSystem()
       {
    		fraMain = new JFrame ("Coffee Bean Drinks" );
    		fraMain.setLayout(new BorderLayout());
     
    		cenPanel = new JPanel();
    		cenPanel.setLayout(new GridLayout(6,2,6,2)); // 6rows 2columns
     
    		buttonJPanel = new JPanel();
    		buttonJPanel.setLayout(new GridLayout(1,5,2,2)); //1row5columns
     
    		lblDRINKS_ID = new JLabel("Drinks ID :", JLabel.LEFT);
    		lblDRINKS_NAME = new JLabel("Drinks Name :" , JLabel.LEFT);
    		lblDRINKS_PRICE = new JLabel("Drinks Price: $" , JLabel.LEFT);
     
    		txtDRINKS_ID = new JTextField(18);
    		txtDRINKS_NAME = new JTextField(20);
    		txtDRINKS_PRICE= new JTextField(25);
    		txtaOutput = new JTextArea(10,30);
     
    		btnAdd = new JButton("Add");
    		btnDisplay = new JButton("Display");
    		btnDelete = new JButton("Delete");
    		btnSearch = new JButton("Search");
    		btnSave = new JButton("Save");
    		btnTransaction = new JButton("Transaction");
    		btnSave.setEnabled(false);
     
    		buttonJPanel.add(btnAdd);
    		buttonJPanel.add(btnDisplay);
    		buttonJPanel.add(btnDelete);
    		buttonJPanel.add(btnSearch);
    		buttonJPanel.add(btnSave);
    		buttonJPanel.add(btnTransaction);
     
    		cenPanel.add(lblDRINKS_ID);
    		cenPanel.add(txtDRINKS_ID);
    		cenPanel.add(lblDRINKS_NAME);
    		cenPanel.add(txtDRINKS_NAME);
    		cenPanel.add(lblDRINKS_PRICE);
    		cenPanel.add(txtDRINKS_PRICE);
     
    		fraMain.add(cenPanel, BorderLayout.CENTER);
    		fraMain.add(new JScrollPane(txtaOutput), BorderLayout.EAST);
    		fraMain.add(buttonJPanel, BorderLayout.SOUTH);
     
    		btnAdd.addActionListener( new ActionListener ()
    	    {
    			public void actionPerformed( ActionEvent event)
    			{
    			    try
    				{
    					addDrinks();
    				}
     
    				catch(Exception e )
    				{
    				}
    			}
    		}
    	);
     
    	btnSave.addActionListener( new ActionListener ()
    	{
    		public void actionPerformed( ActionEvent event)
    		{
    			try
    			{
    			}
     
    			catch(Exception e )
    			{
    			}
    		}
    	}
    	);
     
    	btnSearch.addActionListener( new ActionListener ()
    	{
    	    public void actionPerformed( ActionEvent event)
    		{
    		    try
    			{
    				searchDrinks();
    			}
     
    			catch(Exception e )
    			{
    			}
    		}
    	}
    	);
     
    	btnDisplay.addActionListener( new ActionListener ()
        {
            public void actionPerformed( ActionEvent event)
    		{
    			try
    			{
    			   displayDrinks();
    			}
     
    			catch(Exception e )
    			{
    			}
    		}
    	}
    	);
     
    	btnDelete.addActionListener( new ActionListener ()
    	{
    		public void actionPerformed( ActionEvent event)
    		{
    			try
    			{
    			   deleteDrinks();
    			}
     
    			catch(Exception e )
    			{
    			}
    		}
    	}
    	);
     
    	fraMain.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	fraMain.setSize( 500, 280 );
    	fraMain.setVisible( true );
    }
     
    public void ClearForm()
    {
       txtDRINKS_ID.setText("");
       txtDRINKS_NAME.setText("");
       txtDRINKS_PRICE.setText("");
    }
     
    public static void main(String []args)
    {
       DrinksSystem Drinks = new DrinksSystem();
    }
     
    public void searchDrinks() throws Exception
    {
    	int DRINKS_ID= 0, searchDRINKS_ID = 0, found = 0;
    	String DRINKS_NAME = "";
    	double DRINKS_PRICE = 0.0;
    	String output = "";
     
    	File sfile;
    	Scanner input, searchInput;
     
    	sfile = new File("DrinksSystem.txt");
    	input = new Scanner(sfile);
     
    	if(!sfile.exists())//if Files does not exist
    	{
    		System.out.println("\nThe file\"" + sfile.getName() + "\" cannot be opened\n");
    		System.exit(0);
    	}
     
    	else
    	{
    		searchDRINKS_ID = Integer.parseInt( txtDRINKS_ID.getText() ) ;
     
    		//the useDelimiter will read everything till the "," and "\n"
    		input.useDelimiter("[,\\n]");
     
    		//Read data from a File
    		while(input.hasNext() && found == 0){
    		DRINKS_ID = input.nextInt();
    		DRINKS_NAME = input.next();
    		DRINKS_PRICE = Double.parseDouble(input.next());
     
    		if(searchDRINKS_ID == DRINKS_ID)
    		found = 1;
    	}
     
    	if(found == 1)
    	{		
    		txtDRINKS_NAME.setText( DRINKS_NAME );
    		txtDRINKS_PRICE.setText( Double.toString( DRINKS_PRICE ));
    		output = output + DRINKS_ID + " \t" + DRINKS_NAME + "\t" + "$"+DRINKS_PRICE + "\n";
    		txtaOutput.setText("DRINKS_ID"+"\t"+"DRINKS_NAME"+"\t"+"DRINKS_PRICE" +"\n" +output);
     
    	}
     
    	else 
    	{
    		txtaOutput.setText("Record Not Found!");
    	}
     
    	input.close(); //close the file
    }
     
    public void displayDrinks() throws Exception
    {
     	ClearForm();
    	int DRINKS_ID = 0;
    	String DRINKS_NAME;
    	double DRINKS_PRICE;
    	String output = "";
     
    	File Sfile;
    	Scanner input;
     
    	Sfile = new File("DrinksSystem.txt");
    	input = new Scanner(Sfile);
     
    	if(!Sfile.exists())//if Files does not exist
    	{
    		System.out.println("\nThe file\"" + Sfile.getName()+ "\" cannot be opened\n");
    		System.exit(0);	
    	}
     
    	else
    	{
    		//the useDelimiter will read everything till the "," and "\n"
    		input.useDelimiter("[,\\n]");
    		output = "DRINKS_ID \tDRINKS_NAME \tDRINKS_PRICE\n";
     
    		//Read data from a file
    	    while(input.hasNext())
    		{
    			DRINKS_ID = input.nextInt();
    			DRINKS_NAME = input.next();
    			DRINKS_PRICE = Double.parseDouble(input.next());
    			output = output + ALBUM_ID + " \t" + ALBUM_NAME + "\t" +"$"+ ALBUM_PRICE + "\n";
    		}
        }
     
    	input.close(); //close the file
    	txtaOutput.setText( output );
    }
     
    public void addDrinks() throws Exception
    {
    	int DRINKS_ID = 0;
    	String DRINKS_NAME;
    	double DRINKS_PRICE;
    	File Sfile;
    	PrintWriter output;
     
    	Sfile = new File("DrinksSystem.txt");
     
    	if( !Sfile.exists())
    	{
    		//to create a new file if the file does not exists
    		Sfile.createNewFile();
    		System.out.println("\nNew file\"" + Sfile.getName()+"\" has been created to the\n");
    		System.out.println("current directory\n");
    		output = new PrintWriter(Sfile);
    		DRINKS_ID = 1; //set the DRINKS_ID to 0001
    	}
     
    	else
    	{
    		Scanner finput = new Scanner(Sfile);
     
    		//the useDelimiter will read everything till the "," and "\n"
    		finput.useDelimiter("[,\\n]");
     
    		//Read data from a file
    		while(finput.hasNext())
    		{
    			DRINKS_ID = finput.nextInt();
    			DRINKS_NAME = finput.next();
    			DRINKS_PRICE = Double.parseDouble(finput.next());
    		}
     
    		DRINKS_ID = DRINKS_ID + 1;
    		boolean append = true;
     
     
    		//create a file for appending
    		FileWriter fwriter = new FileWriter(Sfile, append);
    		output = new PrintWriter(fwriter);
     
    	}
     
    	DRINKS_NAME = txtDRINKS_NAME.getText();
    	DRINKS_PRICE = Double.parseDouble(txtDRINKS_PRICE.getText());
     
    	output.println(DRINKS_ID + "," + DRINKS_NAME + "," + DRINKS_PRICE);//Sent the data to the DrinksSystem.txt
    	output.close(); //Close the file
    	ClearForm();
    	displayDrinks();	 
    }
     
    public void deleteDrinks() throws Exception
    {	
      	int DRINKS_ID = 0, searchDRINKS_ID = 0, found = 0;
       	String DRINKS_NAME = " ";
       	double DRINKS_PRICE = 0.0;
       	String diplaying = "";
     
       	File sfile, tempFile;
       	PrintWriter output;
       	Scanner input, searchInput;
     
       	sfile = new File("DrinksSystem.txt");
       	input = new Scanner(sfile);
     
       	tempFile = new File ("temp.txt");// to create new Temporary 
       	output = new PrintWriter(tempFile);
     
       	if(!sfile.exists())
    	{
       		System.out.println("\nThe File \""+ sfile.getName()+"\"cannot be opened\n");
       	}
     
    	else
    	{
        	searchDRINKS_ID = Integer.parseInt( txtDRINKS_ID.getText() ) ;
     
        	// the useDelimiter will read everything till the "," and "\n"
     
        	input.useDelimiter("[,\\n]");
     
        	//Read data from a File
     
        	while(input.hasNext())
    		{
        		DRINKS_ID = input.nextInt();
        		DRINKS_NAME = input.next();
        		DRINKS_PRICE = Double.parseDouble(input.next());
     
     			if(searchDRINKS_ID !=DRINKS_ID)
    			{
        			output.println(DRINKS_ID+ "," +DRINKS_NAME+ ","+DRINKS_PRICE);
        		}
     
        		else
    			{
    				found = 1;
    				txtDRINKS_NAME.setText( DRINKS_NAME );
    				txtDRINKS_PRICE.setText( Double.toString( DRINKS_PRICE ));
    				diplaying = DRINKS_ID + " \t" + DRINKS_NAME + "\t" + "$"+DRINKS_PRICE + "\n";
    				txtaOutput.setText("DRINKS_ID"+"\t"+"DRINKS_NAME"+"\t"+"DRINKS_PRICE" +"\n" +diplaying+"\n"+"Records Deleted!");
        		}	
        	}		    
     
    		// If record are not found
        	if(found == 0)
    		{
        		txtaOutput.setText("Record Not Found!");
        	}	
     
    		else 
        	{		
        		System.out.println("");
        	}
     
        	//close the both input and output files
        	input.close();
        	output.close();
     
        	sfile.delete(); // Delete DrinksSystem.txt
        	tempFile.renameTo(sfile); // Rename temp.txt to DrinksSystem.txt
     
        }  	   
    } 
    }
    }

    hey, it worked! lol damn, im happy...pfftt

    And just so you know, i havent done the Transaction Part. I wanted to do the simple one first (lecture said it simple not me), add delete search and display, which i get the errors. If this is okay, then i proceed to that Transaction Part.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: URGENT! Please help me. Thank You!

    Where are the error messages?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: URGENT! Please help me. Thank You!

    And you're still ignoring your exceptions (and our advice that you not do this). I guess fixing this wasn't all that "urgent" after all.

  17. #17
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    Error.jpg

    there, thats the error..sorry..having discussion with a partner as well..

  18. #18
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    Okay, tell me what exactly please. I mean, do i have to delete that throws exceptions or what? I dont understand thats why.

  19. #19
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: URGENT! Please help me. Thank You!

    Please don't paste an image here. Instead paste the actual text held in the console. Since that's a Windows CMD console, simply right-click it, then click on Select All, and then press <enter>. Your text will have been copied to the clipboard. Then post the actual text here.

    --- Update ---

    Again, you should at least print out stacktraces:

    try {
       // ... some code in here
    } catch (SomeException e) {   // *** obviously you'll be catching a specific exception, not this
      e.printStackTrace();  //  ****** add this here ******
    }

  20. #20
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    I did delete that as you said, it works. Well, at least form 15errors to 9errors..

    Error2.jpg

  21. #21
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    Wow. I didnt know that. Thanks for the tip. And wait, i'll that coding first.

    --- Update ---

    Okay, i still have the same errors even after i add that one.


    C:\Drinks>javac DrinksSystem.java
    DrinksSystem.java:229: error: illegal start of expression
    public void displayDrinks()
    ^
    DrinksSystem.java:229: error: illegal start of expression
    public void displayDrinks()
    ^
    DrinksSystem.java:229: error: ';' expected
    public void displayDrinks()
    ^
    DrinksSystem.java:269: error: illegal start of expression
    public void addDrinks()
    ^
    DrinksSystem.java:269: error: illegal start of expression
    public void addDrinks()
    ^
    DrinksSystem.java:269: error: ';' expected
    public void addDrinks()
    ^
    DrinksSystem.java:323: error: illegal start of expression
    public void deleteDrinks()
    ^
    DrinksSystem.java:323: error: illegal start of expression
    public void deleteDrinks()
    ^
    DrinksSystem.java:323: error: ';' expected
    public void deleteDrinks()
    ^
    9 errors

  22. #22
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: URGENT! Please help me. Thank You!

    OK, can you repost your entire DrinksSystem class, but with well-formatted code? I think that the formatting will help us and you see just where the error may be. It looks like these methods are being placed where they don't belong.

  23. #23
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    When you said well-formatted code, is it like this?

     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JPanel;
     
    import java.awt.GridLayout;
    import java.awt.BorderLayout;
     
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.util.Scanner;
     
    public class DrinksSystem
    {
       JFrame fraMain;
       JLabel lblDRINKS_ID, lblDRINKS_NAME, lblDRINKS_PRICE;
       JTextField txtDRINKS_ID, txtDRINKS_NAME, txtDRINKS_PRICE;
       JTextArea txtaOutput;
       JButton btnAdd, btnDisplay, btnDelete, btnSearch, btnSave, btnTransaction;
       JPanel buttonJPanel, cenPanel;
     
       public DrinksSystem()
       {
    		fraMain = new JFrame ("Coffee Bean Drinks" );
    		fraMain.setLayout(new BorderLayout());
     
    		cenPanel = new JPanel();
    		cenPanel.setLayout(new GridLayout(6,2,6,2)); // 6rows 2columns
     
    		buttonJPanel = new JPanel();
    		buttonJPanel.setLayout(new GridLayout(1,5,2,2)); //1row5columns
     
    		lblDRINKS_ID = new JLabel("Drinks ID :", JLabel.LEFT);
    		lblDRINKS_NAME = new JLabel("Drinks Name :" , JLabel.LEFT);
    		lblDRINKS_PRICE = new JLabel("Drinks Price: $" , JLabel.LEFT);
     
    		txtDRINKS_ID = new JTextField(18);
    		txtDRINKS_NAME = new JTextField(20);
    		txtDRINKS_PRICE= new JTextField(25);
    		txtaOutput = new JTextArea(10,30);
     
    		btnAdd = new JButton("Add");
    		btnDisplay = new JButton("Display");
    		btnDelete = new JButton("Delete");
    		btnSearch = new JButton("Search");
    		btnSave = new JButton("Save");
    		btnTransaction = new JButton("Transaction");
    		btnSave.setEnabled(false);
     
    		buttonJPanel.add(btnAdd);
    		buttonJPanel.add(btnDisplay);
    		buttonJPanel.add(btnDelete);
    		buttonJPanel.add(btnSearch);
    		buttonJPanel.add(btnSave);
    		buttonJPanel.add(btnTransaction);
     
    		cenPanel.add(lblDRINKS_ID);
    		cenPanel.add(txtDRINKS_ID);
    		cenPanel.add(lblDRINKS_NAME);
    		cenPanel.add(txtDRINKS_NAME);
    		cenPanel.add(lblDRINKS_PRICE);
    		cenPanel.add(txtDRINKS_PRICE);
     
    		fraMain.add(cenPanel, BorderLayout.CENTER);
    		fraMain.add(new JScrollPane(txtaOutput), BorderLayout.EAST);
    		fraMain.add(buttonJPanel, BorderLayout.SOUTH);
     
    		btnAdd.addActionListener( new ActionListener ()
    	    {
    			public void actionPerformed( ActionEvent event)
    			{
    			    try
    				{
    					addDrinks();
    				}
     
    				catch(Exception e )
    				{
    					e.printStackTrace();
    				}
    			}
    		}
    	);
     
    	btnSave.addActionListener( new ActionListener ()
    	{
    		public void actionPerformed( ActionEvent event)
    		{
    			try
    			{
    			}
     
    			catch(Exception e )
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    	);
     
    	btnSearch.addActionListener( new ActionListener ()
    	{
    	    public void actionPerformed( ActionEvent event)
    		{
    		    try
    			{
    				searchDrinks();
    			}
     
    			catch(Exception e )
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    	);
     
    	btnDisplay.addActionListener( new ActionListener ()
        {
            public void actionPerformed( ActionEvent event)
    		{
    			try
    			{
    			   displayDrinks();
    			}
     
    			catch(Exception e )
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    	);
     
    	btnDelete.addActionListener( new ActionListener ()
    	{
    		public void actionPerformed( ActionEvent event)
    		{
    			try
    			{
    			   deleteDrinks();
    			}
     
    			catch(Exception e )
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    	);
     
    	fraMain.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	fraMain.setSize( 500, 280 );
    	fraMain.setVisible( true );
    }
     
    public void ClearForm()
    {
       txtDRINKS_ID.setText("");
       txtDRINKS_NAME.setText("");
       txtDRINKS_PRICE.setText("");
    }
     
    public static void main(String []args)
    {
       DrinksSystem Drinks = new DrinksSystem();
    }
     
    public void searchDrinks()
    {
    	int DRINKS_ID= 0, searchDRINKS_ID = 0, found = 0;
    	String DRINKS_NAME = "";
    	double DRINKS_PRICE = 0.0;
    	String output = "";
     
    	File sfile;
    	Scanner input, searchInput;
     
    	sfile = new File("DrinksSystem.txt");
    	input = new Scanner(sfile);
     
    	if(!sfile.exists())//if Files does not exist
    	{
    		System.out.println("\nThe file\"" + sfile.getName() + "\" cannot be opened\n");
    		System.exit(0);
    	}
     
    	else
    	{
    		searchDRINKS_ID = Integer.parseInt( txtDRINKS_ID.getText() ) ;
     
    		//the useDelimiter will read everything till the "," and "\n"
    		input.useDelimiter("[,\\n]");
     
    		//Read data from a File
    		while(input.hasNext() && found == 0){
    		DRINKS_ID = input.nextInt();
    		DRINKS_NAME = input.next();
    		DRINKS_PRICE = Double.parseDouble(input.next());
     
    		if(searchDRINKS_ID == DRINKS_ID)
    		found = 1;
    	}
     
    	if(found == 1)
    	{		
    		txtDRINKS_NAME.setText( DRINKS_NAME );
    		txtDRINKS_PRICE.setText( Double.toString( DRINKS_PRICE ));
    		output = output + DRINKS_ID + " \t" + DRINKS_NAME + "\t" + "$"+DRINKS_PRICE + "\n";
    		txtaOutput.setText("DRINKS_ID"+"\t"+"DRINKS_NAME"+"\t"+"DRINKS_PRICE" +"\n" +output);
     
    	}
     
    	else 
    	{
    		txtaOutput.setText("Record Not Found!");
    	}
     
    	input.close(); //close the file
    }
     
    public void displayDrinks()
    {
     	ClearForm();
    	int DRINKS_ID = 0;
    	String DRINKS_NAME;
    	double DRINKS_PRICE;
    	String output = "";
     
    	File Sfile;
    	Scanner input;
     
    	Sfile = new File("DrinksSystem.txt");
    	input = new Scanner(Sfile);
     
    	if(!Sfile.exists())//if Files does not exist
    	{
    		System.out.println("\nThe file\"" + Sfile.getName()+ "\" cannot be opened\n");
    		System.exit(0);	
    	}
     
    	else
    	{
    		//the useDelimiter will read everything till the "," and "\n"
    		input.useDelimiter("[,\\n]");
    		output = "DRINKS_ID \tDRINKS_NAME \tDRINKS_PRICE\n";
     
    		//Read data from a file
    	    while(input.hasNext())
    		{
    			DRINKS_ID = input.nextInt();
    			DRINKS_NAME = input.next();
    			DRINKS_PRICE = Double.parseDouble(input.next());
    			output = output + ALBUM_ID + " \t" + ALBUM_NAME + "\t" +"$"+ ALBUM_PRICE + "\n";
    		}
        }
     
    	input.close(); //close the file
    	txtaOutput.setText( output );
    }
     
    public void addDrinks()
    {
    	int DRINKS_ID = 0;
    	String DRINKS_NAME;
    	double DRINKS_PRICE;
    	File Sfile;
    	PrintWriter output;
     
    	Sfile = new File("DrinksSystem.txt");
     
    	if( !Sfile.exists())
    	{
    		//to create a new file if the file does not exists
    		Sfile.createNewFile();
    		System.out.println("\nNew file\"" + Sfile.getName()+"\" has been created to the\n");
    		System.out.println("current directory\n");
    		output = new PrintWriter(Sfile);
    		DRINKS_ID = 1; //set the DRINKS_ID to 0001
    	}
     
    	else
    	{
    		Scanner finput = new Scanner(Sfile);
     
    		//the useDelimiter will read everything till the "," and "\n"
    		finput.useDelimiter("[,\\n]");
     
    		//Read data from a file
    		while(finput.hasNext())
    		{
    			DRINKS_ID = finput.nextInt();
    			DRINKS_NAME = finput.next();
    			DRINKS_PRICE = Double.parseDouble(finput.next());
    		}
     
    		DRINKS_ID = DRINKS_ID + 1;
    		boolean append = true;
     
     
    		//create a file for appending
    		FileWriter fwriter = new FileWriter(Sfile, append);
    		output = new PrintWriter(fwriter);
     
    	}
     
    	DRINKS_NAME = txtDRINKS_NAME.getText();
    	DRINKS_PRICE = Double.parseDouble(txtDRINKS_PRICE.getText());
     
    	output.println(DRINKS_ID + "," + DRINKS_NAME + "," + DRINKS_PRICE);//Sent the data to the DrinksSystem.txt
    	output.close(); //Close the file
    	ClearForm();
    	displayDrinks();	 
    }
     
    public void deleteDrinks()
    {	
      	int DRINKS_ID = 0, searchDRINKS_ID = 0, found = 0;
       	String DRINKS_NAME = " ";
       	double DRINKS_PRICE = 0.0;
       	String diplaying = "";
     
       	File sfile, tempFile;
       	PrintWriter output;
       	Scanner input, searchInput;
     
       	sfile = new File("DrinksSystem.txt");
       	input = new Scanner(sfile);
     
       	tempFile = new File ("temp.txt");// to create new Temporary 
       	output = new PrintWriter(tempFile);
     
       	if(!sfile.exists())
    	{
       		System.out.println("\nThe File \""+ sfile.getName()+"\"cannot be opened\n");
       	}
     
    	else
    	{
        	searchDRINKS_ID = Integer.parseInt( txtDRINKS_ID.getText() ) ;
     
        	// the useDelimiter will read everything till the "," and "\n"
     
        	input.useDelimiter("[,\\n]");
     
        	//Read data from a File
     
        	while(input.hasNext())
    		{
        		DRINKS_ID = input.nextInt();
        		DRINKS_NAME = input.next();
        		DRINKS_PRICE = Double.parseDouble(input.next());
     
     			if(searchDRINKS_ID !=DRINKS_ID)
    			{
        			output.println(DRINKS_ID+ "," +DRINKS_NAME+ ","+DRINKS_PRICE);
        		}
     
        		else
    			{
    				found = 1;
    				txtDRINKS_NAME.setText( DRINKS_NAME );
    				txtDRINKS_PRICE.setText( Double.toString( DRINKS_PRICE ));
    				diplaying = DRINKS_ID + " \t" + DRINKS_NAME + "\t" + "$"+DRINKS_PRICE + "\n";
    				txtaOutput.setText("DRINKS_ID"+"\t"+"DRINKS_NAME"+"\t"+"DRINKS_PRICE" +"\n" +diplaying+"\n"+"Records Deleted!");
        		}	
        	}		    
     
    		// If record are not found
        	if(found == 0)
    		{
        		txtaOutput.setText("Record Not Found!");
        	}	
     
    		else 
        	{		
        		System.out.println("");
        	}
     
        	//close the both input and output files
        	input.close();
        	output.close();
     
        	sfile.delete(); // Delete DrinksSystem.txt
        	tempFile.renameTo(sfile); // Rename temp.txt to DrinksSystem.txt
     
        }  	   
    } 
    }
    }

  24. #24
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: URGENT! Please help me. Thank You!

    Quote Originally Posted by Luly_Marty View Post
    When you said well-formatted code, is it like this?
    Almost. Your indentations are still a bit wild and instead need to be uniform, so that our brains can understand better the levels of code blocks that the indentations signify. I suggest that you use 3 spaces per indentation, that you take extreme care to line them up correctly (or using an IDE have it format your code), because it will pay you back dividends, especially dealing with a large class like this. If you did your indentations correctly and neatly, you'd see in an instance that you're trying to declare a class before the code of another previous class has been fully closed. Line the code blocks up with good indentations and you'll see exactly what I mean. And then you'll be able to solve some of your errors (... you're missing a closing brace).

    You would in fact see this:

       public void searchDrinks() {
          int DRINKS_ID = 0, searchDRINKS_ID = 0, found = 0;
          String DRINKS_NAME = "";
          double DRINKS_PRICE = 0.0;
          String output = "";
     
          File sfile;
          Scanner input, searchInput;
     
          sfile = new File("DrinksSystem.txt");
          input = new Scanner(sfile);
     
          if (!sfile.exists())// if Files does not exist
          {
             System.out.println("\nThe file\"" + sfile.getName()
                   + "\" cannot be opened\n");
             System.exit(0);
          }
     
          else {
             searchDRINKS_ID = Integer.parseInt(txtDRINKS_ID.getText());
     
             // the useDelimiter will read everything till the "," and "\n"
             input.useDelimiter("[,\\n]");
     
             // Read data from a File
             while (input.hasNext() && found == 0) {
                DRINKS_ID = input.nextInt();
                DRINKS_NAME = input.next();
                DRINKS_PRICE = Double.parseDouble(input.next());
     
                if (searchDRINKS_ID == DRINKS_ID)
                   found = 1;
             }
     
             if (found == 1) {
                txtDRINKS_NAME.setText(DRINKS_NAME);
                txtDRINKS_PRICE.setText(Double.toString(DRINKS_PRICE));
                output = output + DRINKS_ID + " \t" + DRINKS_NAME + "\t" + "$"
                      + DRINKS_PRICE + "\n";
                txtaOutput.setText("DRINKS_ID" + "\t" + "DRINKS_NAME" + "\t"
                      + "DRINKS_PRICE" + "\n" + output);
     
             }
     
             else {
                txtaOutput.setText("Record Not Found!");
             }
     
             input.close(); // close the file
          }    // ************** you're missing a closing brace here ****************
     
     
       public void displayDrinks() {
          ClearForm();
          int DRINKS_ID = 0;
          String DRINKS_NAME;
          double DRINKS_PRICE;
          String output = "";
     
          File Sfile;
          Scanner input;
     
          Sfile = new File("DrinksSystem.txt");
          input = new Scanner(Sfile);
     
          if (!Sfile.exists())// if Files does not exist
          {
             System.out.println("\nThe file\"" + Sfile.getName()
                   + "\" cannot be opened\n");
             System.exit(0);
          }
     
          else {
             // the useDelimiter will read everything till the "," and "\n"
             input.useDelimiter("[,\\n]");
             output = "DRINKS_ID \tDRINKS_NAME \tDRINKS_PRICE\n";
     
             // Read data from a file
             while (input.hasNext()) {
                DRINKS_ID = input.nextInt();
                DRINKS_NAME = input.next();
                DRINKS_PRICE = Double.parseDouble(input.next());
                output = output + ALBUM_ID + " \t" + ALBUM_NAME + "\t" + "$"
                      + ALBUM_PRICE + "\n";
             }
          }
     
          input.close(); // close the file
          txtaOutput.setText(output);
       }


    --- Update ---

    There are more errors, but fixing this will get you closer to your solution.

  25. #25
    Junior Member
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: URGENT! Please help me. Thank You!

    Sorry, i just finished my class. Umm okay..i try to fix like you said that missing closing bracket and all. Yes, i got few errors. Fix them. Got another, fix them. And then, i got this. What is that suppose to mean? What should i be doing? Thanks=)

    C:\Drinks>javac DrinksSystem.java
    DrinksSystem.java:189: error: unreported exception FileNotFoundException; must b
    e caught or declared to be thrown
    input = new Scanner(sfile);
    ^
    DrinksSystem.java:248: error: unreported exception FileNotFoundException; must b
    e caught or declared to be thrown
    input = new Scanner(Sfile);
    ^
    DrinksSystem.java:289: error: unreported exception IOException; must be caught o
    r declared to be thrown
    Sfile.createNewFile();
    ^
    DrinksSystem.java:292: error: unreported exception FileNotFoundException; must b
    e caught or declared to be thrown
    output = new PrintWriter(Sfile);
    ^
    DrinksSystem.java:298: error: unreported exception FileNotFoundException; must b
    e caught or declared to be thrown
    Scanner finput = new Scanner(Sfile);
    ^
    DrinksSystem.java:316: error: unreported exception IOException; must be caught o
    r declared to be thrown
    FileWriter fwriter = new FileWriter(Sfile, append);
    ^
    DrinksSystem.java:342: error: unreported exception FileNotFoundException; must b
    e caught or declared to be thrown
    input = new Scanner(sfile);
    ^
    DrinksSystem.java:345: error: unreported exception FileNotFoundException; must b
    e caught or declared to be thrown
    output = new PrintWriter(tempFile);
    ^
    8 errors

    --- Update ---

    And, is the IDE a Java Software? I did google it. So i need to download those?

    --- Update ---

    Hey, guess what? I did it. YAY!! The last step was adding the 'throws Exception' right? It worked. Thank you so much. You're a big help. Thank you thank you

Page 1 of 2 12 LastLast

Similar Threads

  1. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  2. its urgent
    By talha07 in forum Java Networking
    Replies: 3
    Last Post: January 22nd, 2011, 01:44 PM
  3. Urgent!!!!!!!!!!!!!!!!
    By dinesh03188 in forum JDBC & Databases
    Replies: 3
    Last Post: May 14th, 2010, 01:38 AM
  4. Urgent Help Please :)
    By Jamie94 in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 12th, 2010, 06:34 AM
  5. Replies: 1
    Last Post: May 4th, 2009, 06:30 AM