the program runs but fails to read the double word states (e.g New Jersey).
am getting the error.java.lang.NumberFormatException: For input string: "Hampshire"

import java.text.DecimalFormat;
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
import java.nio.CharBuffer;
import java.awt.*;
//import java.awt.event.*;
import javax.lang.model.type.ArrayType;
import javax.swing.*;
import javax.swing.event.*;
public class PJ5 extends JFrame implements ListSelectionListener
{String []States = new String[100];
	double []statetax = new double[100];
double sales;
JPanel p1;
JList  StateList;



JTextField salesField;
private JTextField taxField;
public static void main(String []args)    
  {
  PJ5  x = new PJ5();
  x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  x.setTitle("Sales tax calculator");
  x.setSize(350, 250);
  x.setVisible(true);
  
  } // main

public PJ5() // constructor
{
String s;
StringTokenizer st;
int k=0;
try 
{
BufferedReader inFile = new BufferedReader(new FileReader("C:\\Users\\Owner\\Documents\\taxrates.txt"));
    while ((s= inFile.readLine()) != null)
	{
    	
    	
st = new StringTokenizer(s);
    	States[k]=(st.nextToken());
   	
	statetax[k] = Double.valueOf(st.nextToken());
			
	++k;
	} // while not EOF
  inFile.close();
} catch (Exception e) { System.err.println(e); }

    
    
JPanel p1 =new JPanel();

 p1.setLayout(new GridLayout(2,1));
 
p1.add(new JLabel("Sales amount:"));
salesField= new JTextField(5);
salesField.setEditable(true);
p1.add(salesField);


p1.add(new JLabel("tax:"));
taxField= new JTextField(4);
taxField.setEditable(false);
p1.add(taxField);


add(p1,BorderLayout.NORTH);
pack();
setVisible(true);

JPanel p = new JPanel();
StateList = new JList(States);  // create a JList object
StateList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
JScrollPane sp = new JScrollPane(StateList);  // adding scrolling capability
p.add(sp);
getContentPane().add(p, BorderLayout.SOUTH);
// Event registration
StateList.addListSelectionListener(this);
} // constructor

// Event handling
public void valueChanged(ListSelectionEvent event)
     {
	DecimalFormat dollar = new DecimalFormat("0.00");	
	double tax;
	 sales=Double.parseDouble(salesField.getText());
	 
tax=sales* (statetax[StateList.getSelectedIndex()]/100);
	 taxField.setText("$"+dollar.format(tax));
	 
     } // valueChanged
} // Lab22V3
Member Avatar for hfx642

"Jersey" is NOT a double... And neither is "Hampshire".
Instead of assuming the nextToken() is a double, do a countTokens(),
and process according to the number of tokens in your inputed string.

Be a part of the DaniWeb community

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