0
    File RaptorsFile = new File("raptors.txt");
    File tempFile = new File("raptorstemp.txt");
    BufferedWriter output = new BufferedWriter (new FileWriter ("raptorstemp.txt"));
    System.out.println("Please enter the following infomation");
    System.out.println("Please enter the Player's Name"); 
    String PlayerName=sc.nextLine();
    System.out.println("Please enter the Player's FG%"); 
    String FieldGoal=sc.nextLine();
    System.out.println("Please  enter the Player's 3P%"); 
    String ThreePointer=sc.nextLine();
    System.out.println("Please enter the Player's FT%"); 
    String FreeThrow=sc.nextLine();
    System.out.println("Please enter the Player's REB"); 
    String Rebounds=sc.nextLine();
    System.out.println("Please enter the Player's AST"); 
    String Assists=sc.nextLine();
    System.out.println("Please enter the Player's Points"); 
    String Points=sc.nextLine();
    String currentLine;
    while((currentLine = Raptors.readLine()) != null) {
        // trim newline when comparing with lineToRemove
        String trimmedLine = currentLine.trim();
        if(trimmedLine.equals(PlayerName+"," +FieldGoal+"," +ThreePointer+"," +FreeThrow+"," +Rebounds+"," +Assists+"," +Points)) continue;
        output.write(currentLine + System.getProperty("line.separator"));
    }

    boolean successful = tempFile.renameTo(RaptorsFile);
    output.close();
    System.out.println("The CarsTemp file will have the new list"); 
    options(Raptors,s);

I am having a problem i just want the user to input only player's name, for example if he inputs Kyle Lowry i want it to delete all of Kyle Lowry's stats. Below is sample of text field.

Lou Williams,41.1,36.3,87.6,60,53,508
Kyle Lowry,44.9,35.1,81.3,160,260,702
Patrick Patterson,48.8,46.0,75.0,177,61,286
Terrence Ross,42.9,38.7,87.9,119,30,411
Jonas Valanciunas,54.2,0.0,79.2,283,16,414
BackSlash
  • 21,927
  • 22
  • 96
  • 136
Charan
  • 3
  • 1
  • 8
  • Basically if the user enters Kyle Lowry, i want it to delete the whole line, basically deleting Kyle Lowry,44.9,35.1,81.3,160,260,702 and leaving the rest – Charan Jan 08 '15 at 20:52
  • Is `sc` a `Scanner`? – Ascalonian Jan 08 '15 at 20:53
  • @Ascalonian yes it is, Scanner sc= new Scanner (System.in); – Charan Jan 08 '15 at 20:55
  • You would use a temporary file for this. Here is an example: http://stackoverflow.com/questions/6477762/java-delete-line-from-text-file-by-overwriting-while-reading-it or http://www.javadb.com/remove-a-line-from-a-text-file/ – Ascalonian Jan 08 '15 at 20:56
  • @Ascalonian, i don't get it, it's confusing me – Charan Jan 08 '15 at 20:59
  • public static void removeNthLine(String f, int toRemove) throws IOException { RandomAccessFile raf = new RandomAccessFile(f, "rw"); for (int i = 0; i < toRemove; i++) raf.readLine(); long writePos = raf.getFilePointer(); raf.readLine(); long readPos = raf.getFilePointer(); byte[] buf = new byte[1024]; int n; while (-1 != (n = raf.read(buf))) { raf.seek(writePos); raf.write(buf, 0, n); readPos += n; writePos += n; raf.seek(readPos); } raf.setLength(writePos); raf.close(); } – Charan Jan 08 '15 at 21:01
  • do i do this? and d i need t put String , intoRemove? – Charan Jan 08 '15 at 21:02

2 Answers2

0

You can either use the String.startsWith() method or match to a regular expression.

Solution 1

 while ((currentLine = Raptors.readLine()) != null) {
    if (!currentLine.startsWith(PlayerName + ",")) { // you could use continue as well, but imho it is more readable to put the output.write inside the condition
        output.write(currentLine);
        output.newLine();
    }
 }

This solution has better performance but can result in false positives

Solution 2

 while ((currentLine = Raptors.readLine()) != null) {
    if (!currentLine.matches(PlayerName + ",\\d[\\d.+],\\d[\\d.+],\\d[\\d.+]),\\d+\\d+,\\d+\\s*") { 
        output.write(currentLine);
        output.newLine();
    }
 }

This solution exactly matches your criteria but has worse performance and looks uglier.

I think Solution 1 should work well. Please note, that I have not tested the above pieces of code.

PS: it is always a good idea to adhere to the official coding conventions, such as variable names should start with lowercase characters and so on.

PS2: You could use a PrintWriter instead of BufferedWriter, which gives you additional methods, such as println, which combines write(…) and newLine().

David Frank
  • 5,918
  • 8
  • 28
  • 43
  • do i need this File tempFile = new File("raptorstemp.txt");? cuz i don't wanna make it confusing cuz this is my final project – Charan Jan 08 '15 at 21:06
  • Also, i don't wanna use System.getPropert because idk how to use it, i asked my classmate – Charan Jan 08 '15 at 21:07
  • You have two options: you can either read the whole file content into memory, then you do not need a temp file. Or you can leave it as it is now, so you read from one file and writing to another file in parallel, and finally you remove the old file and rename the temp file. I updated my answer and removed System.getProperty(…). – David Frank Jan 08 '15 at 21:14
  • i wanna do option one and can u please help me i have been stuck here for long time – Charan Jan 08 '15 at 21:14
  • can i do this? or is option 1 easier http://stackoverflow.com/questions/1377279/find-a-line-in-a-file-and-remove-it – Charan Jan 08 '15 at 21:16
  • The above solution completely builds upon your (ie. SingleShot's) code. I have only changed the condition. – David Frank Jan 08 '15 at 21:18
0

So basically you loop through the file and check the beginning of each line of text to see if it starts with the user input. You can use the String method startsWith() to determine if the line starts with the user input, if it returns false, write the line to a temporary file, if it returns true, skip to the next line in the input file. When you've scanned the entire input file, close them both, rename the original to a backup file and rename the temporary file to the original file. Not a very complicated problem.

Jim Nutt
  • 1,736
  • 12
  • 10
  • Is it easier than mine cuz i dont' wanna work with while((currentLine = Raptors.readLine()) != null) { // trim newline when comparing with lineToRemove String trimmedLine = currentLine.trim(); if(trimmedLine.equals(PlayerName+"," +FieldGoal+"," +ThreePointer+"," +FreeThrow+"," +Rebounds+"," +Assists+"," +Points)) continue; output.write(currentLine + System.getProperty("line.separator")); } boolean successful = tempFile.renameTo(RaptorsFile); output.close(); System.out.println("The CarsTemp file will have the new list"); – Charan Jan 08 '15 at 21:09
  • David's answer pretty well covers it, I'm not sure I understand what is causing confusion. – Jim Nutt Jan 08 '15 at 21:12