From 15379688016b79cb0d75b166cfcdacccd2ba610b Mon Sep 17 00:00:00 2001 From: Xevion Date: Wed, 4 Mar 2020 02:02:55 -0600 Subject: [PATCH] new and nextint, examples, explanations pending --- study/STUDY.MD | 45 +++++++++++++++++++++++++++++++ uil/aplus-february-2015/README.MD | 13 ++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/study/STUDY.MD b/study/STUDY.MD index 9aaf30a..0f639da 100644 --- a/study/STUDY.MD +++ b/study/STUDY.MD @@ -49,6 +49,7 @@ If you find something incorrect, feel free to contribute and modify. - [What Are Delimiters?](#what-are-delimiters) - [`hasNext` and `hasNextInt`](#hasnext-and-hasnextint) - [`next` and `nextInt`](#next-and-nextint) + - [Examples with Explanations](#examples-with-explanations) @@ -539,3 +540,47 @@ This is mostly self explanatory, but it's important to remember that when used w #### `next` and `nextInt` +`Scanner.next` and `Scanner.nextInt` are the other side of `hasNext` and `hasNextInt`, instead of telling you whether it's safe, whether or not the token exists for the taking, they take the token. + +Again, to emphasize this clearly, as it escaped me completely when I looked into it: the next token MUST match the expected type that the method is requesting. + +While `next` expects no particular type, `nextInt` expects a Integer, and `nextDouble` expects a Double. + +This means that it won't skip ahead to the next set of tokens in the sequence until it finds a valid token, it will only check the exact next token in the sequence, and if it doesn't match, it will raise an exception. + +#### Examples with Explanations + +[Scanner.useDelimiter Usage](https://repl.it/@Xevion/ScanneruseDelimiter-Usage) on repl.it + +```java +Scanner s = new Scanner("5.2.1.2\\2\\2.2"); +s.useDelimiter("\\."); +int sum = 0; +while(s.hasNextInt()) { + int i = s.nextInt(); + out.println(i); + sum += i; +} +out.println(sum); +``` + +```java +s = new Scanner("2 3 allure 5 yellow 39"); +while(s.hasNextInt()) + out.println(s.nextInt()); +``` + +```java +ArrayList arr = new ArrayList(); +s = new Scanner("\t \t a b \t "); +s.useDelimiter("\\s"); +while(s.hasNext()) + arr.add(s.next()); +out.println(arr); +``` + +```java +s = new Scanner("2 h j l 3 4 o p 0 9"); +for(int i = 0; i++ < 5;) + out.println(s.nextInt()); +``` diff --git a/uil/aplus-february-2015/README.MD b/uil/aplus-february-2015/README.MD index b345db0..625e332 100644 --- a/uil/aplus-february-2015/README.MD +++ b/uil/aplus-february-2015/README.MD @@ -45,25 +45,36 @@ Computer Science Competition Hands-On Programming Set [1a]: ./1/Automaton.java [1b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Automaton +[1c]: ./1/Automaton.MD [2a]: ./2/AXCEL.java [2b]: https://notyet.implemented/ +[2c]: ./2/AXCEL.MD [3a]: ./3/Climb.java [3b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Climb +[3c]: ./3/Climb.MD [4a]: ./4/Fate.java [4b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Fate -[5a]: ./5/Favorite.jva +[4c]: ./4/Fate.MD +[5a]: ./5/Favorite.java [5b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Favorite +[5c]: ./5/Favorite.MD [6a]: ./6/Maximus.java [6b]: https://notyet.implemented/ +[6c]: ./6/Maximus.MD [7a]: ./7/MMM.java [7b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-MMM +[7c]: ./7/MMM.MD [8a]: ./8/Name.java [8b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Name +[8c]: ./8/Name.MD [9a]: ./9/Play.java [9b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Play +[9c]: ./9/Play.MD [10a]: ./10/Radians.java [10b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Radians +[10c]: ./10/Radians.MD [11a]: ./11/Spending.java [11b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Spending-partial +[11c]: ./11/Spending.MD [12a]: ./12/TicTacToe.java [12b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-TicTacToe \ No newline at end of file