PDA

View Full Version : Help with Java programming.



Mystic Muse
2014-05-07, 01:15 PM
I'm having trouble with this assignment, and have basically hit a block. If somebody could help me get the program to compile, that'd be great.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;

public class Transfer extends JFrame implements ActionListener
{
//Declare Output stream
DataOutputStream output;

//construct two panels
JPanel firstRow = new JPanel();
JPanel secondRow = new JPanel();

//construct a panel for the fields and buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();


//construct labels and textboxes
JLabel nameLabel = new JLabel("Name");
JTextfield name = new JTextField(15);
JLabel idLabel = new JLabel("Student ID");
JTextfield id = new JTextField(15);
JLabel transferLabel = new JLabel("Transer Course Number");
JTextfield transfer = new JTextField(15);
JLabel localLabel = new JLabel("Local Course Number");
JTextfield local = new JTextField(15);

//construct buttons
JButton submitButton = new JButton("Submit");
JButton exitButton = new JButton("Exit");

public static void main (String[] args)
{
//set the look and feel of the interface
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "The UIManager could not set the look and Feel for this application", "Error", JOptionPane.INFORMATION_MESSAGE);
}

Transfer f = new Transfer();
f.setDefaultCLoseOperation(JFrame.DO_NOTHING_ON_CL OSE);
f.setSize(450,300);
f.setTitle("Transfer Course Substitutions");
f.setResizable(false);
f.setLocation(200,200);
f.setVisible(true);
}

public Transfer()
{
}
public void actionPerformed(ActionEvent e)
{
}

}

public Transfer()
{
Container c = getContentPange();
c.setLayout((newBorderLayout()));
fieldPanel.setLayout(new GridLayout(8,1));
FlowLayout rowSetup = new FlowLayout (FlowLayout.Left,5,3);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
thirdRow.setLayout(rowSetup);
fourthRow.setLayout(rowSetup);
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
}

public Transfer();
{



//add fields to rows

firstRow.add(nameLabel);
firstRow.add(name);

secondRow.add(idLabel);
secondRow.add(id);

thirdRow.add(transfercourseLabel);
thirdRow.add(transfercourse);

fourthRow.add(localLabel);
fourthRow.add(local);

//Add rows to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);

//Add buttons to panel
buttonPanel.add(submitButton);
buttonPanel.add(exitButton);

//Add panels to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);

//Add functionality to buttons
submtiButton.addActionListener(this);
exitButton.addActionListener(this);

try
{
output = new DataOutputStream(new FileOutputStream(transfer.dat));
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null,"", "Error", JOptionPane.INFORMATION_MESSAGE);
System.exit(1)
}

addWindowListener(
newWindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit and submit the file?" "File Submission", JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION)
System.exit(0);
}
}
);

public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();

if (checkFields())
{
try
{
output.writeUTF(name.getText());
output.writeUTF(id.getText());
output.writeUTF(transfercourse.getText());
output.writeUTF(local.getText());

JOptionPane.showMessageDialog(null, "","Submission Successful",JOptionPane.INFORMATION_MESSAGE
}
catch(IOException ex)
{
System.exit(1);
}
clearFields();
}
}

else //code to execute if the user clicks Exit
{
try
{
output.close();
}
catch(IOException c)
{
System.exit(1);
}
System.exit(0);
}

public void clearFields()
{
//Clear fields and reset the focus
name.setText("");
id.setText("");
transfer.setText("");
local.setText("");
name.requestFocus();
}


}

C:\Users\Caleb\Desktop\Transfer.java:77: 'class' or 'interface' expected
public Transfer()
^
C:\Users\Caleb\Desktop\Transfer.java:91: 'class' or 'interface' expected
{
^
2 errors

If anyone can help, that'd be great.

Whoracle
2014-05-07, 01:50 PM
Don't have a Java compiler with me atm, and my java is woefully outdated, but still:

Line 58: Why is that there?

public Transfer()
{
}

Line 80: Why does it have a ; at the end when it's a class?

public Transfer();
{
[...]
}

And generally: Why are there 3 THINGS called "public Transfer()" in your code, which are all different?
What are those supposed to be? Classes? Methods?

Best I can do for now is go and reformat your code to make it more readable (to me. Code readability is subjective, after all) and then try to understand what you're trying to accomplish. Sadly, the Board doesn't keep indents...

Edit: Got halfway through this with the indenting... You SURE that's the code in your class? From what I see, this shouldn't be compiling even that far... There's brackets missing or superfluous. I'll upload the final indented version somewhere where you can see and check the file, OK?

Edit 2: http://dl.lynxcore.org/transfer.html
This is the indented and slightly reformatted version. I didn't add/delete anything that isn't whitespace. The whole thing is kinda off.
So, what exactly is that thing supposed to do? Where did the code come from? Did you get a template for this or have you written it all by yourself? If the former, can we see the Template?

Keris
2014-05-07, 02:24 PM
*snip* Quick tip - if you post your code inside a
block, it'll preserve your whitespace.

From your 'Errors' spoiler, am I right in assuming you're using a regular text editor to write your code then compiling from command line? I would highly reccomend you look into an IDE. I personally use Eclipse (http://www.eclipse.org/), though I have friends who use NetBeans (https://netbeans.org/) and there are a number of others available (http://en.wikipedia.org/wiki/Template:Java_IDEs). BlueJ (http://bluej.org/) is one that was designed to be beginner friendly, though I haven't had any direct experience with it.

You seem to have made a fair number of typos in your code, which all generate errors. Most IDEs will catch those as you type and will look 'further ahead' than the command line compiler does, so you hopefully don't get in the situation where only after fixing one error does the next appear, and so on. Throwing your code into Eclipse found quite a few (http://i.imgur.com/AMpKR2a.png)...

EDIT:
After fixing all the typos and similar errors, plus stripping out a couple of references to stuff you don't seem to have implemented yet ('transfer.dat' and 'checkFields()'), I got it do this (http://i.imgur.com/hoexOQH.png). Is that the sort of thing you're aiming for?



Sadly, the Board doesn't keep indents... It actually does, it's just that your browser will collapse the all whitespace characters. If you quote Mystic Muse's post you'll see the indentation in the editor.

Whoracle
2014-05-07, 02:30 PM
It actually does, it's just that your browser will collapse the all whitespace characters. If you quote Mystic Muse's post you'll see the indentation in the editor.

Yeah, found that out. Put the code up with proper indentation and highlighting anyways. Maybe that way someone can help.

ChristianSt
2014-05-07, 02:32 PM
If you post code you should really consider using the
-tag.

Because reading code without indentation is really something some people (maybe most people) really don't want to do normally.

Something like this:

ddWindowListener(
newWindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit and submit the file?" "File Submission", JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION)
System.exit(0);
}
}
);

is really hard to read. With whitespace it turns into something like this and is much easier to read:


addWindowListener(
newWindowAdapter()
{
public void windowClosing(WindowEvent e)
{
int answer = JOptionPane.showConfirmDialog(null,"Are you sure you want to exit and submit the file?" "File Submission", JOptionPane.YES_NO_OPTION);
if (answer == JOptionPane.YES_OPTION)
System.exit(0);
}
}
);

Mystic Muse
2014-05-08, 10:31 PM
Sorry for the trouble. It kept the indenting when I was pasting it, didn't realize it had screwed things up after it had been posted because of having to leave for final stuff, and not being able to check until, well, now.

Thanks for the help. I'll keep that in mind if I have to do any java in the future.

I was working from a set of directions and a template but didn't have time to post them at the time,