Search code examples
javastringswingjtextfieldgettext

Unable to getText the value of string


I'm preparing a simple system as my assignment, And I'm facing a problem where I dont know how to getText the value of String. I know how to do it with integer, but not with string.

I use Integer.parseInt(t2.getText()); in my code if i want to get an integer value I have added my code into this.

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

public class tollRate extends JFrame implements ActionListener {

    JLabel L1 = new JLabel("Please insert your origin (in capital letter)");
    JTextField t1 = new JTextField(20);
    JLabel L2 = new JLabel("Please insert your destination (in capital letter)");
    JTextField t2 = new JTextField(20);
    JLabel L3 = new JLabel("Your vehicle class");
    JTextField t3 = new JTextField(1);
    JButton b1 = new JButton("Calculate");
    JButton b2 = new JButton("Exit");
    JLabel L4 = new JLabel("Class 0 : Motorcycles, bicycles, or vehicles with "
            + "2 or less wheels" + "\nClass 1 : Vehicles wit 2 axles and 3 "
            + "or 4 wheels excluding taxis" + "\nClass 2 : Vehicles with 2 "
            + "axles and 5 or 6 wheels excluding busses" + "\n Class 3 : "
            + "Vehicles with 3 or more axles" + "\nClass 4 : Taxis"
            + "\nClass 5 : Buses");
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    JPanel p5 = new JPanel();
    String i, j, k;

    tollRate() {
        JFrame a = new JFrame();
        setTitle("Highway Toll Rates System");
        setSize(600, 400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(5, 2));
        p1.setLayout(new GridLayout(1, 2));
        p1.add(L1);
        p1.add(t1);
        p2.setLayout(new GridLayout(1, 2));
        p2.add(L2);
        p2.add(t2);
        p3.setLayout(new GridLayout(1, 2));
        p3.add(L3);
        p3.add(t3);
        p4.setLayout(new FlowLayout());
        p4.add(b1);
        p4.add(b2);
        p5.setLayout(new FlowLayout());
        p5.add(L4);
        add(p1);
        add(p2);
        add(p3);
        add(p4);
        add(p5);
        b1.addActionListener(this);
        b2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent a) {
        Object source = a.getSource();
        if (source == b2) {
            this.dispose();
        } else if (source == b1) {
            i = Integer.parseInt(t1.getText());
            j = Integer.parseInt(t2.getText());
            k = Integer.parseInt(t3.getText());
        }
    }

    public static void main(String[] args) {
        tollRate a = new tollRate();
    }
}

Solution

  • First of all String i, j, k;

         i = Integer.parseInt(t1.getText());
         j = Integer.parseInt(t2.getText());
         k = Integer.parseInt(t3.getText());
    

    This is wrong. You are assigning String for int. Correct them first. and if you want int values it is better to use

    int i, j, k; and use trim() to avoid additional spaces.

         i = Integer.parseInt(t1.getText().trim());
         j = Integer.parseInt(t2.getText().trim());
         k = Integer.parseInt(t3.getText().trim());
    

    In your case use as follows

            i = t1.getText();
            j = t2.getText();
            k = t3.getText();