You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicclassSwitchActionExample {
publicstaticvoidmain(String[] args) {
System.out.println("Type Either a string or an Int: ");
Stringin = "";
Either<String, Integer> either;
try (BufferedReaderreader = newBufferedReader(newInputStreamReader(System.in))) {
in = reader.readLine();
either = Either.right(Integer.parseInt(in));
} catch (NumberFormatException | IOExceptione) {
either = Either.left(in);
}
either.fold(
s -> {
System.out.println("You passed me the String: " + s);
},
x -> {
System.out.println(
"You passed me the Int: " + x
+ ", which I will increment. " + x + " + 1 = "
+ (x + 1));
});
}
}
Prints:
Type Either a string or an Int:
1
You passed me the Int: 1, which I will increment. 1 + 1 = 2
Copyright 2016 Jokubas Dargis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.