Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS IS WHAT I HAVE SO FAR CAN YALL PLZ HELP I WILL RATE! import java.io.File; i

ID: 641056 • Letter: T

Question

THIS IS WHAT I HAVE SO FAR CAN YALL PLZ HELP I WILL RATE!

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Fileio {

    public static void main(String[] args) throws FileNotFoundException {
        // TODO Auto-generated method stub

        Scanner scan = new Scanner(new File("Fred.txt"));
       

        while (scan.hasNextLine()) {
            String scan1 = scan.next();

            scan1 = scan1.replaceAll("[\| ]", " ");

            String scan2 = scan.next();
           
            String[] parts = scan.nextLine().split("|",2);
           
                 for(String p: parts) {
                  System.out.println(p);
            }
           
           
               
          
            scan2 = scan2.replaceAll("[\|]", " ");
           

            String scan3 = scan.next();

            scan3 = scan3.replaceAll("[\| ]", ",");

            System.out.println(">>"+scan1 + "," + scan2 + "," + scan3);


        }
        scan.close();

    }

}

Explanation / Answer

hope it may helps you...

import java.util.regex.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
  
public class fileRead
{
public static void main(String[] args)
{
File file = new File("filename");
StringBuilder sb = new StringBuilder();
String line=null;
  
  
try
{
BufferedReader reader = new BufferedReader(new FileReader(file));
String text = null;
  
// repeat until all lines is read
while ((line = reader.readLine()) != null)
{
if(!line.trim().equals(""))
{
if(line.startsWith("begin:"))
{
String[] data = sb.toString().trim().split(" ");
  
for(String chunk : data)
{
String[] parts = chunk.split(":");
System.out.println(parts[0] + " : " + parts[1]);
}
  
sb.setLength(0);
}
else
{
sb.append(line).append(" ");
}
}
/*String splitarray[] = line.split(":");
String firstentry = splitarray[0];
String secondentry = splitarray[2];
System.out.println(firstentry + " " + secondentry);*/


}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
System.out.println("Out");
}
}

// show file contents here
//System.out.println(str.toString());
}