Skip to content

Instantly share code, notes, and snippets.

@thisMagpie
Created January 25, 2013 02:21
Show Gist options
  • Select an option

  • Save thisMagpie/4631171 to your computer and use it in GitHub Desktop.

Select an option

Save thisMagpie/4631171 to your computer and use it in GitHub Desktop.
messy and unfinished.
import java.io.*;
import java.util.*;
/*
***********************************8**********************************
***************** By Magdalen Berns for miniproject ******************
**********************************************************************
*
* This is a generic IOUtil class to make IO matters more simple to handle.
* Not to be confused with UserInput which is full of static methods that are not very "transferable". (i.e. I am unlikely to use them again in another program)
*
* SkipToDouble() Method to ignore non double values and extract data needed
*
* SkipToInt() to make sure the number of particles is in the right format for trajectory
* NB (could have used skip toDouble and then round it off alternatively but I wanted to try this)
*/
class IOUtil {
/*
* *
* ********** keeps looking until double is found and will 'ignore' values that cannot be converted to double (words)*
* *********/
public static double skipToDouble(Scanner scanner) {
while (scanner.hasNext() && !scanner.hasNextDouble()) {
scanner.next();
}
return scanner.hasNextDouble() ? scanner.nextDouble() : Double.NaN;
}
/*
* *
* * Picks out the first integer value it sees and returns it.
* */
public static int skipToInt(Scanner scanner) {
while (scanner.hasNext() && !scanner.hasNextInt()) {
scanner.next();
}
//This is probably a bit naughty because int is primitive -but it does what I want so I don't care
return scanner.hasNextInt() ? scanner.nextInt() : (int) Double.NaN;
}
public static String typedInput() throws IOException {
BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));
return keyIn.readLine();
}
/*Label (sets and gets at the same time) */
public static String label(String string) {
return string;
}
/*Label (sets and gets at the same time) */
public static String fileName(String string) {
return string;
}
/*
*Call this every time wrong thing is typed.*/
public static void abuse() {
System.out.println("Invalid entry.");
System.out.println("Abusive statement.");
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment