Can't get this to compile.

This is why I HATE Java!

Page 1 of 1

8 Replies - 1138 Views - Last Post: 30 January 2010 - 06:35 PM Rate Topic: -----

#1 Mariko1222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 19-June 09

Can't get this to compile.

Post icon  Posted 29 January 2010 - 04:10 PM

Sure, change one little punctuation mark and go from 1 error to 14!! Some I could fix, some I just CANT find!!!
Here is my coding. I'm trying to have it open and collect data for two different files (Grad Student and Undergrad Student). Right now, I just can't get it to compile. PLEASE help me correct this.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CreateStudentFiles extends JFrame 
	  implements ActionListener
{
	private JLabel title =
	   new JLabel ("Create Student Files");
	private Font bigFont = new Font ("Arial", Font.BOLD, 24);
	private JLabel prompt = 
	   new JLabel ("Enter student data, then select one button:");
	private JTextField idField = new JTextField (5);
	private JTextField lastNameField = new JTextField (10);
	private JTextField firstNameField = new JTextField (10);
	private JLabel idLabel = new JLabel ("ID number");
	private JLabel lastNameLabel = new JLabel ("Last Name");
	private JLabel firstNameLabel = new JLabel ("First Name");
	private JButton gradStudentButton = 
	   new JButton ("Grad student");
	private JButton undergradStudentButton =
	   new JButton ("Undergrad student");
	private Container con = getContentPane ();

	DataOutputStream ostream;

	public CreateStudentFiles ()
	{
	  super ("Create Student Files");
	  try
	  {
		 ostream = new DataOutputStream
		  (new FileOutputStream ("StuData.dat"));
	  }
	  catch (IOException e)
	  {
		  System.err.println ("File not opened");
		  System.exit (1);
	  }

	  con.setLayout (new FlowLayout ());
	  title.setFont (bigFont);
	  con.add (title);
	  con.add (prompt);
	  con.add (idLabel);
	  con.add (idField);
	  con.add (lastNameLabel);
	  con.add (lastNameField);
	  con.add (firstNameLabel);
	  con.add (firstNameField);
	  con.add (gradStudentButton);
	  con.add (undergradStudentButton);
	  gradStudentButton.addActionListener (this);
	  undergradStudentButton.addActionListener (this);
	  setSize (320, 200);
	  setVisible (true);
	  setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent e1)
	{
	  int idNumber;
	  try
	  {
		  idNumber = Integer.parseInt (idNumber.getText());
		  ostream.writeUTF (lastName.getText());
		  ostream.writeUTF (firstName.getText());
		  ostream.writeInt (idNumber);
		  lastName.setText ("");
		  firstName.setText ("");
		  idNumber.setText ("");
	  }
	  catch (NumberFormatException e2)
	  {
		  System.err.println("Invalid ID number");
	  }
	  catch (IOException e3)
	  {
		  System.err.println("Error writing file");
		  System.exit(1);
	  }
	}
	public static void main (String[] args)
	{
	  CreateStudentFiles csf = new CreateStudentFiles();
	}
}



Thanks for your help, this is due by the end of the weekend!
Mariko

Is This A Good Question/Topic? 0
  • +

Replies To: Can't get this to compile.

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Can't get this to compile.

Posted 29 January 2010 - 04:12 PM

View PostMariko1222, on 29 Jan, 2010 - 05:10 PM, said:

I just can't get it to compile.


The errors are?
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Can't get this to compile.

Posted 29 January 2010 - 04:13 PM

please post whatever errors you get, it would be very helpful for anyone trying to help you :)
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Can't get this to compile.

Posted 29 January 2010 - 04:17 PM

You've been around here since June, which is long enough to know the rules. Describing your errors is rule #6 in the big yellow box before you post.
Was This Post Helpful? 0
  • +
  • -

#5 Mariko1222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 19-June 09

Re: Can't get this to compile.

Posted 29 January 2010 - 06:02 PM

Geeze, so sorry I forgot to post my errors
Yes, I have been here since June of Last year and I'm STILL trying to finish this damn course.
Here are my errors...
CreateStudentFiles.java:64 int cannot be dereferenced
idNumber = Integer.parseInt (idNumber.getText();
^
CreateStudentFiles.java:65: cannot find symbol
symbol : variable lastName
location: class CreateStudentFiles
ostream.writeUTF (lastName.getText());
^
CreateStudentFiles.java:66: cannot find symbol
symbol : variable firstName
location: class CreateStudentFiles
ostream.writeUTF (firstName.getText());
^
CreateStudentFiles.java:68: cannot find symbol
symbol : variable lastName
location: class CreateStudentFiles
lastName.setText ("");
^
CreateStudentFiles.java:69: cannot find symbol
symbol : variable firstName
location: class CreateStudentFiles
firstName.setText ("");
^
Create StudentFiles.java:70: int cannot be dereferenced
idNumber.setText ("");
^
6 errors.

Sorry again for forgetting the errors.
THanks.
Was This Post Helpful? 0
  • +
  • -

#6 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 19
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Can't get this to compile.

Posted 29 January 2010 - 06:05 PM

Quote

CreateStudentFiles.java:65: cannot find symbol
symbol : variable lastName
location: class CreateStudentFiles
ostream.writeUTF (lastName.getText());


This means that you are trying to ues a variable that does not exist. You are trying to use the variable lastName but you don't have a lastName variable. I think the variable you meant is lastNameField.

All the cannot find symbol lines mean you are accessing a variable that doesn't exist.

Quote

Create StudentFiles.java:70: int cannot be dereferenced
idNumber.setText ("");
^


You are trying to access a method on a primitive data type. You can't do that.

This post has been edited by chili5: 29 January 2010 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#7 Mariko1222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 19-June 09

Re: Can't get this to compile.

Posted 29 January 2010 - 06:38 PM

View Postchili5, on 29 Jan, 2010 - 05:05 PM, said:

Quote

Create StudentFiles.java:70: int cannot be dereferenced
idNumber.setText ("");
^


You are trying to access a method on a primitive data type. You can't do that.



Okay, so how do I fix it?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: Can't get this to compile.

Posted 29 January 2010 - 06:56 PM

I think you mean to invoke the method from idLabel (the JLabel), not idNumber (the int). Try making this change, and the error should go away.
Was This Post Helpful? 0
  • +
  • -

#9 Mariko1222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 19-June 09

Re: Can't get this to compile.

Posted 30 January 2010 - 06:35 PM

View Postmacosxnerd101, on 29 Jan, 2010 - 05:56 PM, said:

I think you mean to invoke the method from idLabel (the JLabel), not idNumber (the int). Try making this change, and the error should go away.

Thank you to all for helping me getting this to compile. You've all helped in me seeing my mistakes and correcting them. Now I'm on my way....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1