[AE] Uebungsaufgabe Subtraktion String

public class MyClass { Scanner scanner = new Scanner(System.in); public static void main(String args[]) { String minuend = ""; String subtrahent = ""; long result = 0; System.out.println("Subtraktion zweier ganzer Zahlen."); System.out.println("Geben Sie die erste Zahl ein:"); while ((minuend = readInput()) != null) { System.out.println("Keine gueltige Eingabe, bitte nur Zahlen eingeben:"); } System.out.println("Geben Sie die zweite Zahl ein:"); while ((subtrahend = readInput()) != null) { System.out.println("Keine gueltige Eingabe, bitte nur Zahlen eingeben:"); } result = minuend - subtrahend; System.out.println("Das Ergebnis von " + minuend + " - " + subtrahend + " ist " + result); } public static String subtractTwoStrings(String minuend, String subtrahend) { if (minuend == "") return subtrahend; if (subtrahend == "") return minuend; string leadingSign = ""; if (isFirstCharAMinus(minuend) && isFirstCharAMinus(subtrahend)) { } if (minuend.length() > subtrahend.length()) { if (isFirstCharAMinus(minuend) && !isFirstCharAMinus) } if (subtrahend.length() > minuend.length()) { if (!isFirstCharAMinus(subtrahend)) { leadingSign = "-"; String tmp = minuend; minuend = subtrahend; subtrahend = tmp; } } } public static String readInput() { String result = ""; try { result = scanner.next(); } catch (Exception e) { return null; } if (checkNumeric(result)) { return result; } else { return null; } } public static boolean checkNumeric(String input) { int startIndex = 0; if (isFirstCharAMinus(input)) startIndex = 1; for (int i = startIndex; i <= input.length(); i++) { int charCheck = (int)input.charAt(i); if !(48 <= charCheck && charCheck <= 57) { return false; } } return true; } public static boolean isFirstCharAMinus(String input) { if (input != "" && input.charAt(0) == '-') return true; return false; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.