Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Last active August 29, 2015 14:00
Show Gist options
  • Save JorgeOlvera/11235771 to your computer and use it in GitHub Desktop.
Save JorgeOlvera/11235771 to your computer and use it in GitHub Desktop.
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");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment