我正在编写一个程序,它接受一个文件并尝试使用文件中的数据来创建输出。
这是该计划:
import java.util.Scanner;
import java.io.*;
public class WebberProjectTest
{
public static void main(String[] args) throws IOException
{
Scanner scanner = new Scanner(new File("portlandvip.txt"));
while (scanner.hasNext())
{
String firstName = scanner.next();
String lastName = scanner.next();
Integer number = scanner.nextInt();
String ticketType = scanner.next();
if(ticketType == "Court")
{
Integer a = 75 * number;
System.out.println(" " + firstName + " " + lastName + " " + a);
scanner.nextLine();
}
if(ticketType == "Box")
{
Integer a = 50 * number;
System.out.println(" " + firstName + " " + lastName + " " + a);
scanner.nextLine();
}
if(ticketType == "Club")
{
Integer a = 40 * number;
System.out.println(" " + firstName + " " + lastName + " " + a);
scanner.nextLine();
}
}
}
}
这是数据文件:
Loras Tyrell 5俱乐部
Margaery Tyrell 8 Box
Roslin Frey 2 Box
Sansa Stark 2 Club
Jon Snow 5 Club
Edmure Tully 3 Box
Joffrey Baratheon 20 Court
Stannis Baratheon 4 Club
Jaime Lannister 2 Box
Cersei Lannister 1 Court
Beric Dondarrion 8 Court
Balon Greyjoy 16 Box
Olenna Tyrell 4 Court
梅斯泰瑞尔5盒
Tyrion Lannister 2俱乐部
Sandor Clegane 2 Court
Gregor Clegane 6 Club
Samwell Tarly 3 Club
Petyr Baelish 6 Court
这个程序的目的是以输入文件和输出为例。
输入:Loras Tyrell 5 Court
输出:Loras Tyrell $ 375.00
但是,当我运行程序时,没有任何反应。我有一些想法为什么会发生这种情况,但我不知道如何解决它,任何帮助将不胜感激。
我还有关于printf语句的另一个问题。我改变了程序以便正确打印,但现在我必须将println语句更改为printf语句。这就是我改变程序的样子:
import java.util.Scanner;
import java.io.*;
public class WebberProjectTest
{
public static void main(String[] args) throws IOException
{
Scanner scanner = new Scanner(new File("portlandvip.txt"));
while(scanner.hasNext())
{
String line = scanner.nextLine();
String[] words = line.split(" ");
if(words[3].equals("Court"))
{
int a = 75 * Integer.parseInt(words[2]);
System.out.printf(" " + words[0] + " " + words[1] + " $%.2f\n ", a);
}
if(words[3].equals("Box"))
{
int a = 50 * Integer.parseInt(words[2]);
System.out.printf(" " + words[0] + " " + words[1] + " $%.2f\n", a);
}
if(words[3].equals("Club"))
{
int a = 40 * Integer.parseInt(words[2]);
System.out.printf(" " + words[0] + " " + words[1] + " $%.2f\n", a);
}
}
}
}
这就是打印出来的内容:
Loras Tyrell Loras Tyrell $java.util.IllegalFormatConversionException: f != java.lang.Integer
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at WebberProjectTest.main(WebberProjectTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
我不知道我在printf声明中做错了什么,谢谢你的帮助。
答案 0 :(得分:0)
立即尝试阅读行,然后拆分元素..像这样的东西
while(inp.hasNext()) {
String line = inp.nextLine();
String[] words = line.split(" ");
if(words[3].equals("Court")) {
int a = 75 * Integer.parseInt(words[2]);
System.out.println(" " + words[0] + " " + words[1] + " " + a);
}
// ....other if conditions
if(inp.hasNext()) //to skip over empty line
inp.nextLine();
}
答案 1 :(得分:0)
字符串应与equals()
方法而不是==
运算符进行比较。如,
ticketType.equals("Court") //instead of ==> ticketType == "Court"
答案 2 :(得分:0)
试试这段代码。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("test.txt"));
while (scanner.hasNext()){
String string = scanner.useDelimiter("\n").next();
if(!string.equals(" ") && !string.equals("\n") && !string.equals("") && !string.equals("\r") ){
StringTokenizer st = new StringTokenizer(string," ");
String firstName = "";
String lastName = "";
Integer number = 0;
String ticketType = "";
while (st.hasMoreElements()) {
firstName = st.nextElement().toString();
lastName = st.nextElement().toString();
number = Integer.parseInt(st.nextElement().toString());
ticketType = st.nextElement().toString().trim();
System.out.println(" " + firstName + " " + lastName + " " + number + " " +ticketType);
}
if(ticketType.equalsIgnoreCase("Court"))
{
Integer a = 75 * number;
System.out.println(" " + firstName + " " + lastName + " " + a);
}
else if(ticketType.equalsIgnoreCase("Box"))
{
Integer a = 50 * number;
System.out.println(" " + firstName + " " + lastName + " " + a);
}
else if(ticketType.equalsIgnoreCase("Club"))
{
Integer a = 40 * number;
System.out.println(" " + firstName + " " + lastName + " " + a);
}
}//if(!string.equals(" "))
}//while
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}