|
|
|
|
|
Monday, 25 April 2011 15:48 |
// Read input from a text file. Paste input handling code in the while loop
public void readFile(String filename){
try{
FileInputStream fstream = new FileInputStream(filename);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//read line per line
while ((strLine = br.readLine()) != null) {
// input
// handling
// code
}
//Close stream
in.close();
}catch (Exception e){
e.printStackTrace();
}
}
 Read more: |
|
Wednesday, 24 November 2010 05:58 |
Das Zeilenweise einlesen hab ich diesmal durch einen String ersetzt.
import Prog1Tools.*;
public class InverterRekursiv
{
public static void main (String [] args)
{
// Read String
String input = IOTools.readString();
// print Header
System.out.println("===============================");
System.out.println("Invertierer (rekursive Version)");
System.out.println("===============================\n");
// call recursion method
String result = invertString(input, 1);
// print result
System.out.println("\nDie umgekehrte Zeichenfolge lautet: " + result);
}
// recursiv method to invert string
// int i is just used to print 1., 2., ... Zeichen
public static String invertString(String input, int i)
{
// get first char
String first = input.substring(0,1);
// print char
System.out.println(i++ + ".Zeichen: " + first);
// new string = del first char of old string
input = input.substring(1);
// abort condition
if(input.length() == 1)
{
// print char
System.out.println(i + ".Zeichen: " + input);
// return from recursion
return input + first;
}
else // continue recursion
{
return invertString(input, i) + first;
}
}
}
 Read more: |
|
|
Tuesday, 19 October 2010 19:49 |
// Gets user input using JOPtionPanes.
// -Validates as datatyoe
// -Continues to loop for valid input or user quits.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
public static final Pattern CHARACTER = Pattern.compile("\\S.*?");
public static final Pattern UNSIGNED_INT = Pattern.compile("\\d+.*?");
public static final Pattern INT = Pattern.compile("[+-]?\\d+.*?");
public static final Pattern UNSIGNED_DOUBLE =
Pattern.compile("((\\d+\\.?\\d*)|(\\.\\d+))([Ee][-+]?\\d+)?.*?");
public static final Pattern DOUBLE =
Pattern.compile("[+-]?((\\d+\\.?\\d*)|(\\.\\d+))([Ee][-+]?\\d+)?.*?");
public static double getIntInput (String promptMsg)
{
boolean validInput = false;
double returnVal;
String inString = "";
while (!validInput)
{
inString = JOptionPane.showInputDialog(null, promptMsg);
Matcher m = INT.matcher(inString);
validInput = m.find(); // RETURN FROM VALIDATEDOUBLE METHOD
if (!validInput)
{
showErrorPane("VALIDATION ERROR", "Could not validate input. This is most likely due to either:\n1. Input was not a valid number (contains only #'s and decimal point).\n2. The number was negative (negative #'s not valid).");
}
}
returnVal = Integer.parseInt(inString);
return returnVal;
}
public static void showErrorPane(String msg, String title) {
int selection = JOptionPane.showOptionDialog(null,
title, msg, JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
new String[]{"Try Again", "Exit"}, "Try Again");
if (selection == JOptionPane.NO_OPTION)
System.exit(0);
}
 Read more: |
|
|
|
|
|
|
|