This blog is supposed to be a random set of my personal programming experiences.
But, you guys are important too.
If anyone needs any advice, help with a program, or a simple program written for them; let me know here.
Also tips or tricks are greatly appreciated.
I'm in no way amazing at programming.
Having a goal to work towards is the best incentive to get things done.
I'm very confident with Java and Python, and have experience with Bash, C, MatLab and Visual Basic.
No idea about networking at the moment though; that's a plan in progress.
I'll be sure to give credit when credit is due as well.
Cheers,
Showing posts with label cli. Show all posts
Showing posts with label cli. Show all posts
Friday, 24 December 2010
What do you want?
Labels:
advice,
assistance,
bash,
cli,
credit,
followers,
help,
java,
matlab,
me,
motivation,
myself,
python,
simple,
tips,
tricks,
visual basic
Wednesday, 22 December 2010
miNote
Problem:
University requires we keep some form of log book. Physical or virtual. As I am doing software a virtual one sounded good. Now, I'm not rich so I use Open Office. Compatibility and laziness meant I didn't want to use any word editting software like Microsoft Word or Open Writer. Using notepad is also pretty lame. Now I don't need to know all the crap I had written in earlier; don't need to correct spelling mistakes; CLI is fine; all I need is a time stamp with each post. Didn't want an online thing, like this blog, incase I couldn't get online when I needed it. So I decided to create my own note taking program.
tl;dr Need a program to take notes and timestamp.
Solution:
Decided to go with Java for this one. Just because I was using it at the time. This would probably be much better had it been written in C or something equiv. Since there is no need for high performance the language speed is irrelevent. I went with a basic CLI interface that logs each 'post' with a timestamp on an enter key press. Having seen a friend make a similar GUI version and struggle with clicking a button for each post, he was trying to include multiline posts, it seemed the CLI was a good plan. The posts are only timestamped if the times differ by more than a minute. This will probably be increased later on as up to the minute is a bit over the top for recording information in this case.
Code:
Notes:
CLI programming is cool. If someone has no idea what's going on, they will be impressed. Also comments are important. Remember that.
University requires we keep some form of log book. Physical or virtual. As I am doing software a virtual one sounded good. Now, I'm not rich so I use Open Office. Compatibility and laziness meant I didn't want to use any word editting software like Microsoft Word or Open Writer. Using notepad is also pretty lame. Now I don't need to know all the crap I had written in earlier; don't need to correct spelling mistakes; CLI is fine; all I need is a time stamp with each post. Didn't want an online thing, like this blog, incase I couldn't get online when I needed it. So I decided to create my own note taking program.
tl;dr Need a program to take notes and timestamp.
Solution:
Decided to go with Java for this one. Just because I was using it at the time. This would probably be much better had it been written in C or something equiv. Since there is no need for high performance the language speed is irrelevent. I went with a basic CLI interface that logs each 'post' with a timestamp on an enter key press. Having seen a friend make a similar GUI version and struggle with clicking a button for each post, he was trying to include multiline posts, it seemed the CLI was a good plan. The posts are only timestamped if the times differ by more than a minute. This will probably be increased later on as up to the minute is a bit over the top for recording information in this case.
Code:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
/**
* @author Michael Standen
* Saves my notes, with appropriate time stamp.
*/
public class MiNote {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// Initialise things.
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the path of the notes file:");
File notes = new File(scanner.nextLine());
// Checks file exists.
while (!notes.exists()){
System.out.println("File does not exist. Create new? Y/N");
if (scanner.nextLine().equalsIgnoreCase("y")){
// Creates a new file with the name provided.
if (!notes.createNewFile()){
// Error in creation, get new file name.
System.out.println("Error in file creation. Please enter new file path:");
notes = new File(scanner.nextLine());
} else {
new BufferedWriter(new FileWriter(notes, true)).append("Created " +
new SimpleDateFormat("(d MMM yyyy 'at' HH:mm z)").format(new Date())
+ '\n').close();
}
} else {
System.out.print("Please enter the path of the notes file:");
notes = new File(scanner.nextLine());
}
}
// Success. Outputs the current date.
System.out.println("Success. The current time is: " +
new SimpleDateFormat("(d MMM yyyy 'at' HH:mm z)" + '\n').format(new Date()));
// Write the opening date and time.
new BufferedWriter(new FileWriter(notes, true)).append("" + '\n' + "Opened " +
new SimpleDateFormat("(d MMM yyyy 'at' HH:mm z)" +
'\n').format(new Date())).close();
while(true){
String line = scanner.nextLine();
if (line.equalsIgnoreCase("q")){
// Quits the program.
break;
} else if (line.equalsIgnoreCase("r")){
// Outputs the entire file.
readNotes(notes);
} else {
writeNotes(notes, line);
}
}
}
/**
* Appends the note to the file and adds appropriate time and date stamp.
* @param notes the file to be writen to.
* @param line the String to be added.
* @throws IOException
*/
private static void writeNotes(File notes, String line) throws IOException{
// Make the file writable.
notes.setWritable(true);
// Create the file writer.
BufferedWriter writer = new BufferedWriter(new FileWriter(notes, true));
// Create date variables to see if time stamp is applicable.
Calendar now = Calendar.getInstance();
now.setTime(new Date());
Calendar edit = Calendar.getInstance();
edit.setTime(new Date(notes.lastModified()));
// Create formatter for the time stamp
SimpleDateFormat formatter;
// Appends the date if it is different then the last edited date.
if (now.get(Calendar.DAY_OF_MONTH) != edit.get(Calendar.DAY_OF_MONTH)){
formatter = new SimpleDateFormat("(d MMM yyyy 'at' HH:mm z)");
writer.append("" + '\n' + formatter.format(new Date()) + ": " + '\n');
} else if (now.get(Calendar.HOUR_OF_DAY) != edit.get(Calendar.HOUR_OF_DAY) ||
now.get(Calendar.MINUTE) != edit.get(Calendar.MINUTE)){
formatter = new SimpleDateFormat("(HH:mm z)");
writer.append("" + '\n' + formatter.format(new Date()) + ": " + '\n');
}
writer.append(line + '\n');
writer.close();
}
/**
* Reads the file and prints the standard output.
* @param notes the file to be read.
* @return false if unable to read, true otherwise.
* @throws FileNotFoundException
*/
private static void readNotes(File notes) throws FileNotFoundException {
// Outputs the file.
notes.setReadable(true);
Scanner scanner = new Scanner(notes);
while(scanner.hasNext()){
System.out.println(scanner.nextLine());
}
}
}Notes:
CLI programming is cool. If someone has no idea what's going on, they will be impressed. Also comments are important. Remember that.
Subscribe to:
Posts (Atom)