Skip to content

Instantly share code, notes, and snippets.

@ChrisLundquist
Created February 5, 2012 19:30
Show Gist options
  • Save ChrisLundquist/1747560 to your computer and use it in GitHub Desktop.
Save ChrisLundquist/1747560 to your computer and use it in GitHub Desktop.
Hadossa Oudean's Week05 Assignment
/*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();
String newFile = path + "." + "txt";
FileWriter write = new FileWriter(newFile);
PrintWriter print = new PrintWriter(write);
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;
write.writeLine("found a match");
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
{
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)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment