Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Last active August 29, 2015 14:00
Show Gist options
  • Save JorgeOlvera/11200665 to your computer and use it in GitHub Desktop.
Save JorgeOlvera/11200665 to your computer and use it in GitHub Desktop.
3RD PARTIAL LAB
/*Create a method that counts the total number of lines and words/numbers
in a file given by the user.
*/
import java.util.Scanner;
import java.io.*;
public class countingLines {
public static void main(String [] args) {
try {
//Scans file
Scanner input = new Scanner(System.in);
Scanner file = new Scanner(new File(text2.txt));
//Divides into tokens for second part of output
StringTokenizer muchToken = new StringTokenizer(text2.txt)
int lineCounter = 1;
int wordCounter = 1;
//As long as scanner contains a line in the next position, counter increase by one
while (file.nextLine() != null){
lineCounter = lineCounter + 1;
}
while (muchToken.hasMoreTokens){
wordCounter = wordCounter + 1;
}
}
catch (NoSuchElementException e) {
break;
}
//Prints counter values (minus 1 to make up for the extra "1" when initialized)
System.out.println("Numer of lines is equal to" + lineCounter-1);
System.out.println("Number of words and/or numbers is equal to" + wordCounter-1)
}
}
/*
text2.text (FOR TESTING)
i ran out
of ideas
420
such doge
smoke day every weed
not even trying
/*
Write a method named flipLines that receives a Scanner for an input file
and writes to the console the same file's contents with each pair of lines
reversed in order.
*/
import java.util.Scanner;
import java.io.*;
public class flipLines {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
Scanner file = new Scanner(new File(toFlip.txt));
int lineCounter = 1;
String[] lineString;
String[] flippedLineString;
while (file.nextLine() != null){
lineCounter = lineCounter+1;
}
linestring = new String[lineCounter];
//for loop fills the lineString array with elements of the text file
for (int i=0; i<lineCounter i++){
lineString[i] = file.nextLine();
}
for (int j=0; j<lineCounter; j++){
flippedLineString[i] = lineString[i+1];
flippedLineString[i+1] = lineString[i];
}
// WRITE TO File
try{
PrintWriter writeFile = new PrintWriter(
new BufferedWriter(
new FileWriter(toFlip.txt)));
for (int k=0; k<lineCounter; k++ ){
writeFile.println(flippedLineString[k]);
}
writeFile.close;
}
catch (IOException e){
System.err.println("There seems to be an error");
}
import java.util.Scanner;
public class reverseWriter {
public static void main (String args[]){
String[] fileContents;
String[] reversedContents;
int count = 1;
try {
BufferedReader muchFile = new BufferedReader(
new FileReader(text.txt));
StringTokenizer suchToken = new StringTokenizer(text.txt);
while (suchToken.hasMoreTokens){
count += count;
}
fileContents = new String[count];
for (int i=0; i<count; i++){
fileContents[i] = suchToken.nextToken;
}
for (int j=0; j<count; j++){
reversedContents[j] = fileContents[count-j];
}
}
catch (FileNotFoundException e) {
System.err.println("There seems to be an error:\n" + e.getMessage());
}
}
}
import java.io.*;
import java.util.*;
public class studentClass{
private String id, name;
private double gpa;
private int coursesTaken;
public Student(String id, String name, double gpa, int coursesTaken){
this.id = id;
this.name = name;
this.gpa = gpa;
this.coursesTaken = coursesTaken;
}
public String getId(){
return this.id;
}
public void setId(String id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public double getGpa(){
return this.gpa;
}
public void setGpa(double gpa){
this.gpa = gpa;
}
public int getCoursestaken(){
return this.coursesTaken;
}
public void setCoursestaken(int coursesTaken){
this.coursesTaken = coursesTaken;
}
public void dropOutCourse(int coursesTaken){
if (this.coursesTaken != 0) {
this.coursesTaken = coursesTaken - 1;
}
else {
return;
}
}
public void takesNewCourse(int coursesTaken){
if (this.coursesTaken > 50){
return;
}
else{
this.coursesTaken = coursesTaken + 1;
}
}
public String toString();{
return ("Name: " + this.name + " id: " + this.id + " GPA: " +
this.gpa + " Number of courses: " + this.coursesTaken);
}
}
}
import java.io.*;
import java.util.Scanner;
/*0. Exit application.
1. Read file.
2. Modify Information.
3. Store Information.*/
public class StudentApp{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner input = new Scanner(new File(student.txt));
String studID = "";
String studName = "";
String answer;
double studGPA = 0.0;
int studCourses= 0;
char options;
Student estudent = new Student(studID, studName, studGPA, studCourses);
System.out.println("Welcome to the StudentApp™ \n Please introduce:
\n '0' to exit the app
\n'1' to read a file
\n'2' to modify information
\n or '3' to store information");
answer = input.nextChar();
options = answer.charAt(0);
// O P T I O N S M E N U
switch(options){
case '0': System.out.println("Goodbye");
break;
case '1': try{
Scanner file = new Scanner(new File(filename));
file.nextLine();
id = file.nextLine();
name = file.nextLine();
gpa = file.nextDouble();
coursesTaken = file.nextInt();
file.close();
}
catch(FileNotFoundException e) {
System.err.println("Some exception happened @ " + e.getMessage());
}
break;
case '2':
System.out.println("Input name of file you wish to modify");
String filename2 = input.next() + ".txt";
System.out.println("What shall be modified?" +
"\n" + "1) ID." +
"\n" + "2) Name." +
"\n" + "3) GPA." +
"\n" + "4) Courses taken." +
"\n" + "5) Create new Student.");
int value2 = input.nextInt();
Scanner file = new Scanner(new File(filename2));
if(value2 == 1){
System.out.println("Introduce the student's ID:");
file.nextLine();
id = input.next();
file.nextLine();
name = file.nextLine();
gpa = file.nextDouble();
coursesTaken = file.nextInt();
file.close();
} else if(value2 == 2){
System.out.println("Introduce the student's name:");
file.nextLine();
id = file.nextLine();
name = input.next();
file.nextLine();
gpa = file.nextDouble();
coursesTaken = file.nextInt();
file.close();
} else if(value2 == 3){
System.out.println("Introduce the student's GPA:");
file.nextLine();
id = file.nextLine();
name = file.nextLine();
gpa = input.nextDouble();
file.nextLine();
coursesTaken = file.nextInt();
file.close();
} else if(value2 == 4){
System.out.println("Introduce the number of courses taken:");
file.nextLine();
id = file.nextLine();
name = file.nextLine();
gpa = file.nextDouble();
coursesTaken = input.nextInt();
file.nextLine();
file.close();
} else if(value2 == 5){
System.out.println("Introduce the student's ID:");
id = input.next();
System.out.println("Introduce the student's name:");
name = input.next();
System.out.println("Introduce the student's gpa:");
gpa = input.nextDouble();
System.out.println("Introduce the number of courses taken:");
coursesTaken = input.nextInt();
}
else break;
}
break;
case '3': if(value ==3){
try{
PrintWriter storeInFile = new PrintWriter(
new BufferedWriter(
new FileWriter("student.txt")));
storeInFile.print("\n" + estudent.toString());
storeInFile.close();
System.out.println("Information was stored.");
}
catch (IOException e) {
System.err.println("Some IOException occured @: " + e.getMessage());
}
}
break;
default : System.out.println("Please type a valid option");
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment