Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Missing Number/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions Missing Number/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions Missing Number/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Missing Number</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
114 changes: 114 additions & 0 deletions Missing Number/src/MainWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;

/**
* Window that takes in a flat file and prints out the missing numbers in a sequence
* @author Bryson Hair
*
*/
public class MainWindow extends JFrame
{
public MainWindow()
{
this.setTitle("Find Missing Sequence Number");
this.setSize(1000,1000);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

addComponents();

this.setVisible(true);
}

private void addComponents()
{
JTextArea textArea = new JTextArea();
textArea.setRows(10);
textArea.setColumns(40);

JButton button = new JButton("Select File");
button.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent event) {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(button);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
List<String> lines = null;
try {
lines = Files.readAllLines(file.toPath());
} catch (IOException exception) {
exception.printStackTrace();
}

ArrayList<int []> intList = new ArrayList<int []>();
//Convert the list of strings into a list of sorted int arrays
for(int i = 0; i < lines.size(); i++)
{
String [] strArray = lines.get(i).split(",");
int [] intArray = new int [strArray.length];

for(int j = 0; j < strArray.length; j++)
{
intArray[j] = Integer.parseInt(strArray[j]);
}

Arrays.sort(intArray);
intList.add(intArray);
}

int [] missingNumbers = findMissingNumbers(intList);
String str = "";
for(int i = 0; i < missingNumbers.length; i++)
{
str += missingNumbers[i] + "\n";
}

textArea.setText(str);
}
}
});

this.add(button);
this.add(textArea);
}

private int [] findMissingNumbers(ArrayList<int []> intArrays)
{
int [] ints = new int [intArrays.size()];

//find and return an array of missing ints
for(int i = 0; i < intArrays.size(); i++)
{
int [] arr = intArrays.get(i);
int curValue = arr[0];
for(int j = 1; j < arr.length; j++)
{
if(arr[j] - 1 == curValue)
{
curValue = arr[j];
}
else
{
ints[i] = curValue + 1;
}
}
}

return ints;
}
}
13 changes: 13 additions & 0 deletions Missing Number/src/Runner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/**
* The runner class.
* @author Bryson Hair
*
*/
public class Runner
{
public static void main(String [] args)
{
MainWindow window = new MainWindow();
}
}