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

I have this java class that reads data (mixture of words and numbers lines) from

ID: 3631317 • Letter: I

Question

I have this java class that reads data (mixture of words and numbers lines) from a file. How can I modify it so it reads only the words?

File input:

Sentence

int1 double1

int2 double2

Sentence

int1 double1

int2 double2

Output:

Sentence

Sentence

I need it to read only the sentences.

Thanks!

import java.io.File;
import java.util.Scanner;
import javax.swing.JFileChooser;

public class Data
{
    public static void main(String[] args)
    {
        Scanner in = getScanner(args);
       
        while ( in.hasNext() )
        {
            String input = in.nextLine();
            System.out.println(input);
        }

    }

    private static Scanner getScanner(String[] args)
    {
        File file = null;
        try
        {
            if (args.length == 0)
            {
                JFileChooser chooser = new JFileChooser(".");
                if(chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
                    System.exit(0);
                file = chooser.getSelectedFile();        
            }
            else
                file = new File(args[0]);
            return new Scanner(file);  
        }              
        catch (Exception ex)
        {
            ex.printStackTrace();
            System.exit(0);
            return null;
        }
    }
}

Explanation / Answer

// Please rate me! Thank you. // import java.io.File; import java.util.Scanner; import javax.swing.JFileChooser; public class junk{ public static void main(String[] args){ Scanner in = getScanner(args); while (in.hasNext()){ String input = in.nextLine(); input = stringManipulation(input); System.out.println(input); } } public static String stringManipulation(String myString){ String fixedString=""; int length = myString.length(); for(int i = 0; i