问题描述
我正在编写一个程序,该程序在其中接收文件并尝试使用文件中的数据来创建输出.
I am writing a program where it takes a file and tries to use data from the file in order to create an output.
这是程序:
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();
}
}
}
}
这是数据文件:
劳拉·泰瑞尔5俱乐部
玛格丽·泰瑞尔8盒
罗斯林·弗雷2盒
Sansa Stark 2 Club
Sansa Stark 2 Club
琼恩·雪诺5俱乐部
Edmure Tully 3盒
Edmure Tully 3 Box
Joffrey Baratheon 20法院
Joffrey Baratheon 20 Court
Stannis Baratheon 4俱乐部
Stannis Baratheon 4 Club
Jaime Lannister 2盒
Jaime Lannister 2 Box
Cersei Lannister 1法院
Cersei Lannister 1 Court
Beric Dondarrion 8法院
Beric Dondarrion 8 Court
Balon Greyjoy 16盒
Balon Greyjoy 16 Box
Olenna Tyrell 4法院
Olenna Tyrell 4 Court
梅斯·泰瑞(Mace Tyrell)5盒
Mace Tyrell 5 Box
提利昂·兰尼斯特2俱乐部
Tyrion Lannister 2 Club
Sandor Clegane 2法院
Sandor Clegane 2 Court
格雷戈·克莱根6俱乐部
Gregor Clegane 6 Club
Samwell Tarly 3俱乐部
Samwell Tarly 3 Club
Petyr Baelish 6 Court
Petyr Baelish 6 Court
该程序的目的是以输入File和输出为例.
The purpose of this program is to that the input File and output for example.
输入:Loras Tyrell 5 Court
Input: Loras Tyrell 5 Court
输出:Loras Tyrell $ 375.00
Output: Loras Tyrell $375.00
但是,当我运行该程序时,什么也没发生.我对为什么会这样有一些想法,但是我不知道如何解决它,任何帮助将不胜感激.
However, when i run the program, nothing happens. I have a few ideas on why this is happening, but i dont know how to fix it, any help would be appreciated.
我还有一个关于printf语句的问题.我更改了程序,以便正确打印,但是现在我必须将println语句更改为printf语句.这就是我将程序更改为现在的样子:
I also have another question about printf statements. I altered the program so that it prints correctly, but now i have to change the println statements to printf statements. this is what i changed the program to look like now:
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语句中做错了什么,谢谢您的帮助.
I dont know what i did wrong in the printf statement, thank you for any assistance.
推荐答案
尝试此代码.
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();
}
}
}
这篇关于程序输出printf时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!