Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some help with a Java Assignment

Status
Not open for further replies.

Galdar

Technical User
Oct 21, 2002
1
AU
I need some help with a small Java assignment I am doing.
When I compile the code i get 2 errors they are

invalid method declaration: return type required
public CreateRandomFile() LINE 87
^
cannot resolve symbol
new CreateRandomFile(); LINE 112
^

Any help is greatly appreciated

**********CODE BEGINS HERE ***********************

import javax.swing.*;
import java.io.*;
import BreezySwing.*;


public class RandomTest extends GBFrame
{

JLabel accLabel = addLabel("Account Number",1,1,1,1);
IntegerField accField = addIntegerField(0,1,2,1,1);
JLabel firstLabel = addLabel("First Name",2,1,1,1);
JTextField firstField = addTextField("",2,2,1,1);
JLabel lastLabel = addLabel("Last Name",3,1,1,1);
JTextField lastField = addTextField("",3,2,1,1);
JTextArea resultArea = addTextArea("",4,1,2,3);
JButton acceptButton = addButton("Accept",7,1,2,1);
JButton accessButton = addButton("Access",8,1,2,1);
JButton clearButton = addButton("Clear",9,1,2,1);
private RandomAccessFile file;

public void buttonClicked(JButton buttonObj)
{
if (buttonObj == acceptButton)
{

}
else
if (buttonObj == accessButton)
{

}

else
{
accField.setNumber(0);
resultArea.setText("");
firstField.setText("");
lastField.setText("");
}
}
//---------------------------------------------------------------
private void openFile()
{
FileOutputStream foStream = new FileOutputStream ("acc.dat");
ObjectOutputStream doStream = new ObjectOutputStream (foStream);
if ("acc.dat" == null || "acc.dat".equals(""))
System.out.println("Invalid File Name");
else
{
try{
file = new RandomAccessFile ("acc.dat","rw");
}
catch (IOException e)
{
System.out.println("File does not exist or Invalid File Name");
}
}
}
//---------------------------------------------------------------
private Record getRecord()
{
Record record = new Record();
int accountNumber;
try{
accountNumber = Integer.parseInt(accField.getText());
if (accountNumber <1 || accountNumber >100)
{
System.out.println(&quot;Account doesn't exist&quot;);
return null;
}
file.seek((accountNumber -1)*Record.size());
record.read(file);
}
catch (NumberFormatException nfe)
{
System.out.println(&quot;Account does not exist&quot;);
System.out.println(&quot;Invalid Number Format&quot;);
}
catch (IOException io)
{
System.out.println(&quot;Error reading file&quot;);
}

return record;
}
//---------------------------------------------------------------
public CreateRandomFile() // <----- ERROR HERE
{
Record blank = new Record();
openFile();

try{
file = new RandomAccessFile(&quot;acc.dat&quot;, &quot;rw&quot;);
for (int i=0;i<100;i++)
blank.write(file);
System.exit(0);
}
catch (IOException e )
{
System.out.println(&quot;File doesn't exist&quot;);
System.out.println(&quot;Invalid File Name&quot;);
System.exit(1);
}
}
//----------------------------------------------------------------
public static void main(String args[])
{
JFrame tpo = new RandomTest();
tpo.setTitle(&quot;Assignment 1&quot;);
tpo.setSize(200,330);
tpo.setVisible(true);
new CreateRandomFile(); // <--- ERROR HERE
}
}
 
Hi Galdar,

The first problem looks to be that the CreateRandomFile method is not returning anything. It's okay if a method does not actually return a value, but you have to let Java know by using the void return type.

public void CreateRandomFile()
{
Record blank = new Record();
openFile();

try{
file = new RandomAccessFile(&quot;acc.dat&quot;, &quot;rw&quot;);
for (int i=0;i<100;i++)
blank.write(file);
System.exit(0);
}
catch (IOException e )
{
System.out.println(&quot;File doesn't exist&quot;);
System.out.println(&quot;Invalid File Name&quot;);
System.exit(1);
}
}

The second issue in the main method is because there is no class called CreateRandomFile. The class is RandomTest, so to call the CreateRandomFile method you first need to create a RandomTest and then call the CreateRandomFile method in the RandomTest instance. tpo is an instance of RandomTest.

public static void main(String args[]) {
JFrame tpo = new RandomTest();
tpo.setTitle(&quot;Assignment 1&quot;);
tpo.setSize(200, 330);
tpo.setVisible(true);
tpo.CreateRandomFile();
}

Usually method names begin with a lowerCase letter -createRandomFile().

Hope I have read this right.

regards,
Scrat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top