Question

Help check why the exception exist do some change but be sure to use the printwriter...

Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability
Input.txt format like this: Joe sam, thd, 9, 4, 20

import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.Scanner;

public class Main1 {

private static final Scanner scan = new Scanner(System.in);
private static String[] player = new String[622];
private static String DATA = " ";
private static int COUNTS = 0;

public static void main(String[] args) throws IOException {
System.out.println(title);
driver();
}
public static void driver() throws IOException {
boolean q = false;
  
ScanInput();
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name with .txt");
String filename = scan.nextLine();

while (!q) {
q = processDetail(scan, filename);
}
scan.close();
System.out.print("Hope you enjoy this program! /nHave a nice day!!");
}

private static boolean processDetail(Scanner scan, String filename) throws IOException {
boolean q = false;
System.out.println("Enter Yor Q; Y for playing; Q for quiting ");
String input = scan.nextLine().trim();
if (input.equalsIgnoreCase("y") ) {
System.out.println("Enter the player's full name:");
String fullname= scan.nextLine().trim();
String sss=fullname;
player=sss.split(" ");
  
double percent= ProcessInput(player[0]);
if (percent <= 0) {
System.out.println("Sorry! The player may not shot, or the player wasn't in the league\n");
return q;
} else {
COUNTS++; // increment the counts when data are found in the file
String result =
"The eFG% for "+ player[0] + "is " +percent+"in the team "+player[1] ;
DATA += (COUNTS + ". " + result + "\n");

System.out.println(result + " will be stored in " + filename + "\n");
System.out.println(DATA);
// myFileWriter(filename, DATA);
return q;
}
} else if (input.equalsIgnoreCase("q") ) {
q = true;
return q;
} else {
System.out.println("WARNNING: please enter a valid input\n");
return q;
}
}

private static double ProcessInput(String fullname) {

int FGM = -1;
int threePM = -1;
int FGA = -1;
for (int i = 0; i < player.length; i++) {
String[] data = player[i].split(",");
String player = data[0];
if (fullname.equalsIgnoreCase(player)) {
  
FGM = Integer.parseInt(data[2]);
threePM = Integer.parseInt(data[3]);
FGA = Integer.parseInt(data[4]);
}
}
if (FGM <= 0 || threePM <= 0 || FGA <= 0) {
return -1;
} else
return efgCalculator(FGM, threePM, FGA);
}


private static double efgCalculator(int FGM, int threePM, int FGA) {
double efg = 100 * ((FGM +(threePM) * 0.5) / FGA);
double efgPercent = Math.round(efg);
return efgPercent;
}

private static void ScanInput() throws FileNotFoundException {

try {
String Greeting = "Hi, here are your searching history!!\n\n";
File file = new File("input.txt");
Scanner scan = new Scanner(file);
PrintWriter pw = new PrintWriter(file,"UTF-8");
int index = 0;
while (scan.hasNextLine()) {
player[index] = scan.nextLine();
index++;
}
pw.print(Greeting);
pw.close();
scan.close();
} catch (FileNotFoundException e1) {
System.out.println("File not found or have some typo! Checked again!");
}catch (IOException e) {
System.out.println("I/O error occurs!!!");
}
}
  
}

JAVA

0 0
Add a comment Improve this question Transcribed image text
Answer #1

with Scanner you need to check if there is a next line with hasNextLine()

so the loop becomes

while(sc.hasNextLine()){
    str=sc.nextLine();
    //...
}
Add a comment
Know the answer?
Add Answer to:
Help check why the exception exist do some change but be sure to use the printwriter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming)...

    Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException {    if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...

  • import java.io.*; import java.util.Scanner; public class CrimeStats { private static final String INPUT_FILE = "seattle-crime-stats-by-1990-census-tract-1996-2007.csv"; private...

    import java.io.*; import java.util.Scanner; public class CrimeStats { private static final String INPUT_FILE = "seattle-crime-stats-by-1990-census-tract-1996-2007.csv"; private static final String OUTPUT_FILE = "crimes.txt"; public static void main(String[] args) { int totalCrimes = 0; // read all the rows from the csv file and add the total count in the totalCrimes variable try { Scanner fileScanner = new Scanner(new File(INPUT_FILE)); String line = fileScanner.nextLine(); // skip first line while (fileScanner.hasNext()) { String[] tokens = fileScanner.nextLine().split(","); if (tokens.length == 4) { totalCrimes +=...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • JAVA PROBLEMS 1. Update the below method to throw an IndexOutOfBoundsException: public E get(int index) {...

    JAVA PROBLEMS 1. Update the below method to throw an IndexOutOfBoundsException: public E get(int index) {         if (index < 0 || index > numItems) {             System.out.println("Get error: Index "                         + index + " is out of bounds.");             return null;         }         return array[index]; } Hint: Update both the method signature and the method body! 2. Update the below method to include a try-catch block rather than throwing the exception to...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • Write a program to create a file named integerFile.txt if it does not exist. Write 100...

    Write a program to create a file named integerFile.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. The random integers should be in the range 0 to 100 (including 0 and 100). Integers should be separated by spaces in the file. Read the data back from the file and display the data in increasing order This is what I have so far: import java.io.File; import java.util.Scanner; import java.io.PrintWriter; public class integerFile {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT