Java
Here is a collection of all the projects and code I wrote using Java
Final Project
This program reads from files with a specific structure and prints to the console or inside of a new file the contents of the read file. All of the reading is done by the computer using bytes.
/**
* Cody Luketic
* Oct. 7th 2021
​
* Print words from a text file
* to the console and, if chosen
* to a file in the correct format
* Will count letters by a beginning
* number and go t a new line with the
* symbol #.
*/
// importing Utilities
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
public class FileReaderAndPrinter {
// Initializing and setting a scanner for key input,
// making it accessible to all methods
static Scanner keyboardReader = new Scanner(System.in);
// making fileReader accessible to all methods
static Scanner fileReader;
// Creating the main method controlling the other methods
public static void main(String[] args) throws FileNotFoundException {
// Recieving input file from user
System.out.printf("Enter an input file name: ");
String inputFile = keyboardReader.next();
// Connecting the inputFile to the Scanner
fileReader = new Scanner(new File(inputFile));
printToConsole();
// Decides if the loop will run again.
boolean repeat = true;
while (repeat) {
// Asking the user if they want to
// print to a file with y being yes
// and n being no
System.out.printf("\n**Do you want to print in a file (y/n): ");
String printDecision = keyboardReader.next();
// If y loop will end and printToFile()
// method will run, followed by end()
// method
if (printDecision.equals("y")) {
repeat = false;
fileReader = new Scanner(new File(inputFile));
printToFile();
end();
// if n loop will end and printToConsole()
// method will run, followed by end()
// method
} else if (printDecision.equals("n")) {
repeat = false;
end();
}
// If neither are true, the loop will
// repeat because repeat was never made
// false. This ensures that any input
// not y or n will keep the program
// asking
}
} // end of main
// Method that prints the words from the file
// only to the console
public static void printToConsole() throws FileNotFoundException {
// Will count the amount of letters
// in a word
byte wordAmount;
// Checking to make sure there is a Byte for
// the variable to be set too. If not,
// goes to next item until done with
// document
while (fileReader.hasNextLine() && !fileReader.hasNextByte()) {
fileReader.next();
}
if (fileReader.hasNextByte()) {
wordAmount = fileReader.nextByte();
} else {
wordAmount = 0;
}
// Will count what letter the
// program is on in the word
byte wordIndex = 0;
// While loop that goes through the
// outputFile as long as there is a line
while (fileReader.hasNextLine()) {
// If the amount still has letters in
// it, it runs the code
if (wordIndex < wordAmount) {
// if the next value is a byte,
// it resets the words and prints
// a space, assuming it is now
// between words
if (fileReader.hasNextByte()) {
wordAmount = fileReader.nextByte();
System.out.printf(" ");
}
// Finds the next string and
// sets the first character
// in the variable.
// This will be changed every
// loop for each new character
char currentChar = fileReader.next().charAt(0);
// Checks if the character is a #,
// of so, resets the words and goes
// to a new line
if (currentChar == '#') {
if (fileReader.hasNextByte()) {
wordAmount = fileReader.nextByte();
}
System.out.printf("\n");
// Else the character is printed
} else {
System.out.printf("%c", currentChar);
}
// Keeps track that the
// next char is one more
// place in the word
wordIndex++;
// When the wordIndex reaches
// the amount end, it resets to 0
// allowing a new word to be printed
} else {
wordIndex = 0;
}
}
} // end of printToFile
// Method that prints the words from the file
// to the console and to an output file
// specified by the user
// Almost identacle to printToConsole()
public static void printToFile() throws FileNotFoundException {
// Getting outputFile from user
System.out.printf("Enter an output file name: ");
String outputFile = keyboardReader.next();
// Creating a PrintWriter variable
// connected to the outputFile
PrintWriter toFile = new PrintWriter(new File(outputFile));
byte wordAmount;
while (fileReader.hasNextLine() && !fileReader.hasNextByte()) {
fileReader.next();
}
if (fileReader.hasNextByte()) {
wordAmount = fileReader.nextByte();
} else {
wordAmount = 0;
}
byte wordIndex = 0;
while (fileReader.hasNextLine()) {
if (wordIndex < wordAmount) {
if (fileReader.hasNextByte()) {
wordAmount = fileReader.nextByte();
toFile.printf(" ");
}
char currentChar = fileReader.next().charAt(0);
if (currentChar == '#') {
if (fileReader.hasNextByte()) {
wordAmount = fileReader.nextByte();
}
toFile.printf("\n");
}
if (currentChar != '#') {
toFile.printf("%c", currentChar);
}
wordIndex++;
} else {
wordIndex = 0;
}
}
toFile.close();
System.out.printf("Printed inside the "
+ outputFile + " successfully!\n");
} // end of printToFile
// Method that prints End!
public static void end() throws FileNotFoundException {
System.out.printf("End!\n");
} // end of end
} // end of class
Data Structures
I also studied a variety of data structures present in progromming and specifically learned how to write them using Java. A sample of what I learned includes Linked Lists, Queues, Heaps, Hash Maps, and Binary Trees.
