0

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.

  • Don't compare String values using `==`. Use `equals()`. – Alexis C. Apr 19 '14 at 16:22
  • i fixed it but there is still some errors. for example the program outputs the wrong answer. For example the first one is supposed to output Loras Tyrell 350, but instead of 350 it prints out 200. and i also get this at the end of the program: – user3479350 Apr 19 '14 at 16:28
  • java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at WebberProjectTest.main(WebberProjectTest.java:23) 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) – user3479350 Apr 19 '14 at 16:30

3 Answers3

0

Try reading line at once and then split the elements.. Something like this

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();
}
GoldRoger
  • 1,263
  • 7
  • 10
0

Strings should be compared with the equals() method instead of the == operator. e.g.,

ticketType.equals("Court") //instead of ==> ticketType == "Court"
Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
0

Try this code.

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();
        }

    }

}
JHDev
  • 974
  • 2
  • 26
  • 38