gpt4 book ai didi

java - 尝试使用字符串标记生成器读取状态名称时出错

转载 作者:行者123 更新时间:2023-11-28 10:07:17 24 4
gpt4 key购买 nike

该程序允许用户计算输入销售额并计算每个州的销售税。该程序在读取双字州(例如新泽西州)时出错。我需要带有部分的 helo。

    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
}

这是包含各州税率的文本文件。

Alabama 4.0
Alaska 0.
Arizona 5.6
Arkansas 6.0
California 8.25
Colorado 2.9
Connecticut 6.0
Delaware 2.07
Florida 6.0
Georgia 4.0
Hawaii 4.0
Idaho 6.0
Illinois 6.25
Indiana 7.0
Iowa 6.0
Kansas 5.3
Kentucky 6.0
Louisian, 4.0
Maine 5.0
Maryland 6.0
Massachusetts 6.25
Michigan 6.0
Minnesota 6.875
Mississippi 7.0
Missouri 4.225
Montana 0.
Nebraska 5.5
Nevada 6.85
New Hampshire 0.0
New Jersey 7.0
New Mexico 5.375
New York 4.0
North Carolina 4.5
North Dakota 5.0
Ohio 5.5
Oklahoma 4.5
Oregon 0.
Pennsylvania 6.0
Rhode Island 7.0
South Carolina 6.0
South Dakota 4.0
Tennessee 7.0
Texas 6.25
Utah 5.95
Vermont 6.0
Virginia 5.0
West Virginia 6.0
Wisconsin 5.0
Washington 6.5
Washington DC 5.75
Wyoming 4.0

最佳答案

这是因为,分词器将 New 解析为字符串,然后下一个代码将 Hampshire 解析为 double ,您可能需要重组代码

关于java - 尝试使用字符串标记生成器读取状态名称时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8082746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com