Skip to content

Instantly share code, notes, and snippets.

@ChrisLundquist
Created February 5, 2012 19:30

Revisions

  1. ChrisLundquist revised this gist Feb 5, 2012. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions Week05Assig
    Original file line number Diff line number Diff line change
    @@ -67,6 +67,9 @@ public class Week05 {
    search = JOptionPane.showInputDialog("For what would you like to search?");
    searchLength = search.length();

    String newFile = path + "." + "txt";
    FileWriter write = new FileWriter(newFile);
    PrintWriter print = new PrintWriter(write);

    FileReader freader = new FileReader(path);
    BufferedReader breader = new BufferedReader(freader);
    @@ -89,6 +92,7 @@ public class Week05 {
    if (position > 0)
    {
    foundIt = true;
    write.writeLine("found a match");
    JOptionPane.showMessageDialog(null,"Your string was found at position " + position + ", line " + (i));
    }

    @@ -130,9 +134,6 @@ public class Week05 {
    public static void createFile() throws IOException
    {

    String newFile = path + "." + "txt";
    FileWriter write = new FileWriter(newFile);
    PrintWriter print = new PrintWriter(write);

    print.println("I have opened " + newFile + "\r\nto inform you of the results.");

  2. @invalid-email-address Anonymous created this gist Feb 5, 2012.
    183 changes: 183 additions & 0 deletions Week05Assig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,183 @@
    /*This program was created by Hadossa Oudean.*/

    import java.io.*;
    import javax.swing.*;

    public class Week05 {
    static File select;
    static String path;
    static boolean okay;
    static boolean foundIt;
    static int i;
    static int j;
    static int position;
    static int position2;
    static int searchLength;
    static String answer;
    static String answer2;
    static String search;
    static String aLine;

    public static void main(String[] args) throws IOException
    {
    JOptionPane.showMessageDialog(null,"Welcome to Hadossa Oudean's Week05 Assignment.");
    JOptionPane.showMessageDialog(null,"Choose a file to search.\r\nThen you can decide if you want the search to be case sensitive.\r\nFinally, you can tell me what to search for. \r\nI'll do my best!");
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    select = chooser.getSelectedFile();
    path = select.getPath();
    caseSensitive();
    search();
    createFile();
    System.exit(0);

    }
    public static void caseSensitive()
    {
    answer = JOptionPane.showInputDialog("Would you like your search to be case sensitive? \r\nPlease answer 'yes' or 'no.'");

    if (answer.equalsIgnoreCase("yes"))
    {
    System.out.println(answer);
    answer2 = "";
    okay = true;
    }
    if (answer.equalsIgnoreCase("no"))
    {

    System.out.println(answer);
    answer2 = "";
    okay = true;
    }
    else
    {
    while(!okay)
    {
    JOptionPane.showMessageDialog(null,"I'm sorry. I do not understand.");
    answer2 = JOptionPane.showInputDialog("Would you like your search to be case sensitive? \nPlease answer 'yes' or 'no.'");
    if (answer2.equalsIgnoreCase("yes") || answer2.equalsIgnoreCase("no"))
    {
    okay = true;
    }
    }
    }
    }
    public static void search() throws IOException
    {
    search = JOptionPane.showInputDialog("For what would you like to search?");
    searchLength = search.length();


    FileReader freader = new FileReader(path);
    BufferedReader breader = new BufferedReader(freader);
    foundIt = false;

    if ((answer.equalsIgnoreCase("yes")) || (answer2.equalsIgnoreCase("yes")))
    {
    i = 0;
    while (true)
    {
    aLine = breader.readLine();
    if (aLine == null)
    {
    breader.close();
    break;
    }
    position = aLine.indexOf(search,position + 1);
    i++;

    if (position > 0)
    {
    foundIt = true;
    JOptionPane.showMessageDialog(null,"Your string was found at position " + position + ", line " + (i));
    }

    }
    }

    if ((answer.equalsIgnoreCase("no")) || (answer2.equalsIgnoreCase("no")))
    {
    i = 0;

    foundIt = false;

    while (!foundIt)
    {
    aLine = breader.readLine();

    if (aLine == null)
    {
    breader.close();
    break;
    }

    for (j = 0; j <= (aLine.length() - searchLength); j++)
    {

    if (aLine.regionMatches(true, j, search, 0, searchLength))
    {
    foundIt = true;
    JOptionPane.showMessageDialog(null,"Your string was found at position " + j + ", line " + (i+1));
    }
    }
    i++;
    }
    breader.close();
    }
    }


    public static void createFile() throws IOException
    {

    String newFile = path + "." + "txt";
    FileWriter write = new FileWriter(newFile);
    PrintWriter print = new PrintWriter(write);

    print.println("I have opened " + newFile + "\r\nto inform you of the results.");

    if ((answer.equalsIgnoreCase("yes")) || (answer2.equalsIgnoreCase("yes")))

    {
    if (foundIt)
    {
    print.println("Your search for the string '" + search + "' was found at position " + position + ", line " + i);
    }
    else
    {
    print.println("I'm sorry. Your search could not be found.");
    }
    print.println("Note: Your search was case sensitive.");
    }
    if ((answer.equalsIgnoreCase("no")) || (answer2.equalsIgnoreCase("no")))
    {
    if (foundIt)
    {
    print.println("Your search for '" + search + "' was found at position " + j + ", line " + (i+1));
    }
    else
    {
    print.println("I'm sorry. Your search could not be found.");
    }
    print.println("Note: Your search was not case sensitive.");
    }
    print.close();

    }
    while (inputfile.readlin() != null)
    {

    }
    }