This question already has an answer here:
- How do I compare strings in Java? 23 answers
I am writing a program where it takes a file and tries to use data from the file in order to create an output.
this is the program:
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();
}
}
}
}
This is the data file:
Loras Tyrell 5 Club
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
Mace Tyrell 5 Box
Tyrion Lannister 2 Club
Sandor Clegane 2 Court
Gregor Clegane 6 Club
Samwell Tarly 3 Club
Petyr Baelish 6 Court
The purpose of this program is to that the input File and output for example.
Input: Loras Tyrell 5 Court
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.
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);
}
}
}
}
and this is what prints out:
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)
I dont know what i did wrong in the printf statement, thank you for any assistance.