Hi everyone, I've been working on this for a couple of days now. I need to make sure that an error message is given if txtname has a numerical value in it, and I also need to make sure that an error message is given if txtscore has a non-numerical value in it.

Here is the code for my first class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class Assignment extends JFrame
	JTextField txtname, txtscore;
	JLabel lblmsg = new JLabel("            ");
	JTextArea txtContents = new JTextArea();
	JButton btnProcess;
	JPanel pnl=new JPanel();

	public Assignment()
		txtname = new JTextField(10);
		txtscore = new JTextField(10);

		btnProcess = new JButton("process Student information!");
		pnl.setLayout(new GridLayout(3,2));

		pnl.add(new JLabel("Enter Student name"));

		pnl.add(new JLabel("Enter Score"));


		btnProcess.addActionListener(new btnProcess_actionListener_class());

	public static void main(String a[])
		Assignment ec = new Assignment();

	class btnProcess_actionListener_class implements ActionListener
		public void actionPerformed(ActionEvent event)
			String name=txtname.getText();
			int score = Integer.parseInt(txtscore.getText());
			Student s1 = new Student();
			lblmsg.setText("Data OK!");

Here is the code for my second class:

public class Student
	private  String name;
	private int score;

	public Student()
		name = "zzz";

	public String toString()
		String rv = " ";
		rv="name=" + name + ", score=" + score;
		return rv;

	public void setName(String iname)
		name = iname;

	public String getName()
		return name;

	public void setScore(int iscore)
		score = iscore;

	public int getScore()
		return score;

Recommended Answers

easiest a safier way is

http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html just follows with Example about Number Instance

Jump to Post

All 2 Replies

So I figured out how to get my JTextField txtscore to spit out an error if the score was under 0 or over 100. Now the only problem I am having is if the user inputs non-numerical values for txtname an error message needs to show up in lblmsg.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.