1
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.FileReader;
import java.util.Arrays;


public class Part2{
    public static void main(String[] args){
        String[] holdPosition =  new String[30];
        String FileToBeSearched = JOptionPane.showInputDialog("Enter the file name to be searched: ");
        String WordToBeSearched = JOptionPane.showInputDialog("Enter the word to be searched: ");
    try{
        FileReader searchedFile = new FileReader(FileToBeSearched);
        Scanner scannedFile = new Scanner(searchedFile);
        int i = 0;
        String NumOfLines = "";

            while(scannedFile.hasNext()){
                    String currentWord = scannedFile.next();
                    holdPosition[i] = currentWord;

                    if(currentWord.equalsIgnoreCase(WordToBeSearched)){
                        NumOfLines += holdPosition[i-1]+" "+currentWord+" "+scannedFile.next()+"\n";
                            Arrays.fill(holdPosition, null);
                            }
                                i++;
                                    }
                scannedFile.close();
                    JOptionPane.showMessageDialog(null,NumOfLines);
                }catch(Exception except){}

    System.exit(0);
                }
}

This is my current code. I can't figure out how to To test whether a given string represents a number or not, I need to have a try block in which I pass each string in the file to the Double.parseDouble(). I want to return each number in the text file, as well as the string before and after it.

lospejos
  • 1,976
  • 3
  • 19
  • 35
  • 1
    So, what is the problem? You should get what you want. – Nabin Nov 07 '16 at 15:33
  • 1
    I need to find whether there are doubles in my text file. My text file is as follows "Suppose we are slicing a cake to divide it between 5 people. I cut myself a big slice, consisting of 33.3 percent of the whole cake. Now it is your turn to cut a slice of cake. Will you also cut a 33.3 percent slice? Or will you be fairer and divide the remaining 66.6 percent of the cake into 4 even parts? How big a slice will you cut?" Then I need to print the word before each number the number and then the word after each number. –  Nov 07 '16 at 15:34
  • I'm in doubt if I understood your question correctly. It seems like you gave an answer. Look also here: http://stackoverflow.com/a/5769679/1828296 – lospejos Nov 07 '16 at 15:39
  • No, I need to use Double.parseDouble() to search for numbers in the text. In the answer I gave, I am checking for certain strings only –  Nov 07 '16 at 15:41
  • Possible duplicate of [How to find out if the value contained in a string is double or not](http://stackoverflow.com/questions/3133770/how-to-find-out-if-the-value-contained-in-a-string-is-double-or-not) – Markus Mitterauer Nov 07 '16 at 16:29
  • I appreciate the accept! – GhostCat Feb 03 '19 at 07:27

1 Answers1

1

I am not sure if I get your question, but could it be that you only need a little helper method such as:

public static boolean isDouble(String word) {
   boolean isDouble = false;
   try {
     Double.parseDouble(word);
     isDouble = true;
   } catch (NumberFormatException nfe) {
     // empty on purpose, as most of the time, input will not be a number!
   }
   return isDouble;
}

Now you can simply use that method to check each input string you receive from your scanner. If the method returns true, you know, you got a number.

If your code should also know that number you can slightly rework this to:

public static Double isDouble(String word) {
  Double isDouble = null;

... using null as indication to the caller that your input isn't a number. But returning null is not really god practice.

Hint: my code is just written down to get you going; beware of typos or subtle other errors. It is meant to get you going to solve your problem yourself.

GhostCat
  • 137,827
  • 25
  • 176
  • 248