Click here to Skip to main content
15,888,288 members
Home / Discussions / Java
   

Java

 
AnswerRe: java programming Pin
Richard MacCutchan29-Jun-15 3:38
mveRichard MacCutchan29-Jun-15 3:38 
GeneralRe: java programming Pin
vikkismarty29-Jun-15 4:55
vikkismarty29-Jun-15 4:55 
AnswerRe: java programming Pin
Member 117346789-Jul-15 1:40
Member 117346789-Jul-15 1:40 
Questionjava Pin
Shyam Narayan28-Jun-15 5:36
Shyam Narayan28-Jun-15 5:36 
AnswerRe: java Pin
Richard MacCutchan28-Jun-15 21:30
mveRichard MacCutchan28-Jun-15 21:30 
QuestionJVM process to execute a java program Pin
NCRK Rajput24-Jun-15 4:17
NCRK Rajput24-Jun-15 4:17 
AnswerRe: JVM process to execute a java program Pin
Richard MacCutchan24-Jun-15 22:21
mveRichard MacCutchan24-Jun-15 22:21 
QuestionRefactor Pin
Member 1151769915-Jun-15 13:05
Member 1151769915-Jun-15 13:05 
OK...I am new to programming and I am trying to use existing code for a project. Every time I try to run it, it comes abck saying there is "no main class" what am I doing worng and what should I do to fix it? Please help me and do it with "semi-retarded jargon" I will not benefit from your help if the responses are too technical.

Please see below:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;


class Donor
{
	private String donorName;
	private String nameOfCharity;
	private double amountPledged;

	public Donor(String donorName, String nameOfCharity, double amountPledged) {
		this.donorName = donorName;
		this.nameOfCharity = nameOfCharity;
		this.amountPledged = amountPledged;
	}

	public String getDonorName() {
		return donorName;
	}

	public void setDonorName(String donorName) {
		this.donorName = donorName;
	}

	public String getNameOfCharity() {
		return nameOfCharity;
	}

	public void setNameOfCharity(String nameOfCharity) {
		this.nameOfCharity = nameOfCharity;
	}

	public double getAmountPledged() {
		return amountPledged;
	}

	public void setAmountPledged(double amountPledged) {
		this.amountPledged = amountPledged;
	}
}

public class Charity extends JFrame implements ActionListener
{
	// JLabels
	private JLabel donorNameLabel;
	private JLabel nameOfCharityLabel;
	private JLabel amountPledgedLabel;

	// JTextFields
	private JTextField donorNameField;
	private JTextField nameOfCharityField;
	private JFormattedTextField amountPledgedField;

	// JButtons
	private JButton addButton;
	private JButton viewCharities;
	private JButton addNewCharity;

	// JPanel
	private JPanel firstPanel;
	private JPanel secondPanel;

	private NumberFormat amountNumberFormat;
	// Table
	private JTable charitiesTable;
	private JTableHeader tableHeader;
	private DefaultTableModel defaultTableModel;
	
	private InputStream fileInputStream;
	private DataInputStream dataInputStream;
	private Scanner fileInput;
	
        @SuppressWarnings("LeakingThisInConstructor")
	public Charity()
	{
		// setting frame size
		setSize(650, 700);
		// setting frame layout
		setLayout(new BorderLayout());
		// setting title
		setTitle("Charity");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		// instantiating JLabels
		donorNameLabel = new JLabel("Enter Donor Name");
		nameOfCharityLabel = new JLabel("Enter Charity Name");
		amountPledgedLabel = new JLabel("Enter Amount Pledged");

		// instantiating JTextFields
		donorNameField = new JTextField(25);
		nameOfCharityField = new JTextField(25);
		amountNumberFormat =  NumberFormat.getInstance();
		amountPledgedField = new JFormattedTextField(amountNumberFormat);
		amountPledgedField.setColumns(25);

		firstPanel = new JPanel(new GridLayout(6,1,0,10));
		
		// adding labels and fields
		firstPanel.add(donorNameLabel);
		firstPanel.add(donorNameField);
		firstPanel.add(nameOfCharityLabel);
		firstPanel.add(nameOfCharityField);
		firstPanel.add(amountPledgedLabel);
		firstPanel.add(amountPledgedField);
		
		// instantiating button objects
		addButton = new JButton("Add Charity");
		addButton.addActionListener(this);

		addNewCharity = new JButton("Add New Charity");
		addNewCharity.addActionListener(this);

		viewCharities = new JButton("View Charity List");
		viewCharities.addActionListener(this);

		// adding buttons
		secondPanel = new JPanel();
		secondPanel.add(addButton);
		secondPanel.add(addNewCharity);
		secondPanel.add(viewCharities);

		// making table
		defaultTableModel = new DefaultTableModel();
		defaultTableModel.addColumn(new String("Donor Name"));
		defaultTableModel.addColumn(new String("Charity Name"));
		defaultTableModel.addColumn(new String("Amount Pledged"));

		charitiesTable = new JTable(defaultTableModel);
		charitiesTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		tableHeader = charitiesTable.getTableHeader();
		tableHeader.setBackground(Color.yellow);
		JScrollPane scrollPane = new JScrollPane(charitiesTable);
		
		secondPanel.add(scrollPane);
		
		// adding components to frame window
		add(firstPanel,BorderLayout.NORTH);
		add(secondPanel,BorderLayout.CENTER);
	}

	public void actionPerformed(ActionEvent event) 
	{
		Object eventSource = event.getSource();
		
		String donorName;
		String charityName;
		String amountPledgedText;
		double amountPledged;
		
		Donor donor;

		boolean isValidInput = true;
		// if add charity button is pressed
		if(eventSource == addButton)
		{
			// getting values
			donorName = donorNameField.getText().trim();
			charityName = nameOfCharityField.getText().trim();
			amountPledgedText = amountPledgedField.getText().trim();
			// if anything is left blank, a message is shown
			if(donorName.equals(""))
			{
				JOptionPane.showMessageDialog(this, "Please enter donor's name.", "Error", JOptionPane.ERROR_MESSAGE);
				isValidInput = false;
			}
			if(charityName.equals(""))
			{
				JOptionPane.showMessageDialog(this, "Please enter charity name.", "Error", JOptionPane.ERROR_MESSAGE);
				isValidInput = false;
			}
			if(amountPledgedText.equals(""))
			{
				JOptionPane.showMessageDialog(this, "Please enter amount pledged.", "Error", JOptionPane.ERROR_MESSAGE);
				isValidInput = false;
			}
			// if all the values are valid, charity is added
			if(isValidInput)
			{
				amountPledged = ((Number)(amountPledgedField.getValue())).doubleValue();
				if(amountPledged<0.0)
				{
					JOptionPane.showMessageDialog(this, "Please enter a valid amount.", "Error", JOptionPane.ERROR_MESSAGE);
					return;
				}
				donor = new Donor(donorName, charityName, amountPledged);
				
				// adding donor objects to file
				writeToFile(donor);
				JOptionPane.showMessageDialog(this, "Charity has been successfully added.", "Charity Added", JOptionPane.INFORMATION_MESSAGE);
			}
		}
		// if Add New Charity button is pressed, fields are cleared
		else if(eventSource == addNewCharity)
		{
			donorNameField.setText("");
			nameOfCharityField.setText("");
			amountPledgedField.setText("");
		}
		// if view charity button is pressed, data is displayed in JTable
		else if(eventSource == viewCharities)
		{
			clearTable();
			Vector<Donor> donorList = readFile();
			int numberOfCharities = donorList.size();
			for(int i=0;i<numberOfCharities;i++)
			{
				donor = (Donor)donorList.elementAt(i);
				defaultTableModel.insertRow(charitiesTable.getRowCount(), new Object[]{donor.getDonorName(),donor.getNameOfCharity(),"$"+donor.getAmountPledged()});
			}
		}
	}
	
	// function clearing the table data, if any
	private void clearTable()
	{
		int numberOfRows = charitiesTable.getRowCount();
		while(numberOfRows>0)
		{
			defaultTableModel.removeRow(0);
			numberOfRows--;
		}
	}
	
	private boolean writeToFile(Donor donor)
	{
		// Create file 
		FileWriter fstream;
		try {
			String filePath = "Charity List.txt";
			fstream = new FileWriter(filePath,true);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(donor.getDonorName());
			out.write("\r\n");
			out.write(donor.getNameOfCharity());
			out.write("\r\n");
			out.write(""+donor.getAmountPledged());
			out.write("\r\n");
			//Closing the output stream
			out.close();
			return true;
		} 
		catch (IOException e1) 
		{
			e1.printStackTrace();
			return false;
		}
	}
	
	private Vector<Donor> readFile()
	{
		String charityDetails = "";
		String nameOfDonor = "";
		String nameOfCharity = "";
		String amount = "";
		
		Vector<Donor> donorList = new Vector<Donor>();
		
		try
		{
			fileInputStream = new FileInputStream("Charity List.txt");
		} 
		// if file is not found
		catch (FileNotFoundException e) 
		{
			JOptionPane.showMessageDialog(this, "Charity List not found.");
			return null;
		}
		// getting DataInputStream object
		dataInputStream = new DataInputStream(fileInputStream);
		fileInput = new Scanner(new InputStreamReader(dataInputStream));

		try
		{
			// if file is not found
			if(fileInputStream==null)
			{
				JOptionPane.showMessageDialog(this, "Charity List not found.");
				return null;
			}
			// if found, reading information
			while(fileInput.hasNext())
			{
				if(fileInput.hasNext())
				{
					nameOfDonor = fileInput.nextLine();
				}
				if(fileInput.hasNext())
				{
					nameOfCharity = fileInput.nextLine();
				}
				if(fileInput.hasNext())
				{
					amount = fileInput.nextLine();
				}
				Donor donor = new Donor(nameOfDonor, nameOfCharity, Double.parseDouble(amount));
				donorList.addElement(donor);
				
			}
			// closing input stream
			dataInputStream.close();

		}
		catch(FileNotFoundException e)
		{
			System.out.println("File not found.");
			return null;
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			return null;
		}
		return donorList;
	}	


	
	public static void main(String[] args) 
	{
		Charity charity = new Charity();
		charity.setVisible(true);
	}
}

AnswerRe: Refactor Pin
Richard MacCutchan15-Jun-15 21:47
mveRichard MacCutchan15-Jun-15 21:47 
AnswerRe: Refactor Pin
Member 1177254417-Jun-15 1:28
Member 1177254417-Jun-15 1:28 
AnswerRe: Refactor Pin
Member 117346789-Jul-15 1:42
Member 117346789-Jul-15 1:42 
Questionselenium webdriver Pin
harshad mudda12-Jun-15 20:56
harshad mudda12-Jun-15 20:56 
SuggestionRe: selenium webdriver Pin
Richard MacCutchan12-Jun-15 22:14
mveRichard MacCutchan12-Jun-15 22:14 
QuestionBanking Domain MutliThreading Pin
Member 1175557710-Jun-15 0:36
Member 1175557710-Jun-15 0:36 
AnswerRe: Banking Domain MutliThreading Pin
Sascha Lefèvre10-Jun-15 1:05
professionalSascha Lefèvre10-Jun-15 1:05 
Questionhow to develop the app using java platform like sms alert with voice call play Pin
Vijaya Kumar9-Jun-15 18:30
Vijaya Kumar9-Jun-15 18:30 
AnswerRe: how to develop the app using java platform like sms alert with voice call play Pin
Richard MacCutchan9-Jun-15 21:11
mveRichard MacCutchan9-Jun-15 21:11 
QuestionCan anyone recommend any good java tutorials? Pin
Adamdew933-Jun-15 11:52
Adamdew933-Jun-15 11:52 
AnswerRe: Can anyone recommend any good java tutorials? Pin
Richard MacCutchan3-Jun-15 21:41
mveRichard MacCutchan3-Jun-15 21:41 
GeneralRe: Can anyone recommend any good java tutorials? Pin
amitjfdi5-Jun-15 2:33
amitjfdi5-Jun-15 2:33 
GeneralRe: Can anyone recommend any good java tutorials? Pin
Member 1068598231-Aug-15 8:46
Member 1068598231-Aug-15 8:46 
GeneralRe: Can anyone recommend any good java tutorials? Pin
Richard MacCutchan31-Aug-15 20:29
mveRichard MacCutchan31-Aug-15 20:29 
AnswerRe: Can anyone recommend any good java tutorials? Pin
st3car21-Jun-15 10:22
st3car21-Jun-15 10:22 
Questionneed to Multilayer Perceptron Java Code Pin
Member 117346521-Jun-15 21:36
Member 117346521-Jun-15 21:36 
Questionhow to add voice recognition in java programming Pin
Member 1171170622-May-15 5:35
Member 1171170622-May-15 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.