mirror of
https://github.com/Xevion/contest.git
synced 2025-12-11 10:06:52 -06:00
new and nextint, examples, explanations pending
This commit is contained in:
@@ -49,6 +49,7 @@ If you find something incorrect, feel free to contribute and modify.
|
|||||||
- [What Are Delimiters?](#what-are-delimiters)
|
- [What Are Delimiters?](#what-are-delimiters)
|
||||||
- [`hasNext` and `hasNextInt`](#hasnext-and-hasnextint)
|
- [`hasNext` and `hasNextInt`](#hasnext-and-hasnextint)
|
||||||
- [`next` and `nextInt`](#next-and-nextint)
|
- [`next` and `nextInt`](#next-and-nextint)
|
||||||
|
- [Examples with Explanations](#examples-with-explanations)
|
||||||
|
|
||||||
<!-- /TOC -->
|
<!-- /TOC -->
|
||||||
|
|
||||||
@@ -539,3 +540,47 @@ This is mostly self explanatory, but it's important to remember that when used w
|
|||||||
|
|
||||||
#### `next` and `nextInt`
|
#### `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<String> arr = new ArrayList<String>();
|
||||||
|
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());
|
||||||
|
```
|
||||||
|
|||||||
@@ -45,25 +45,36 @@ Computer Science Competition Hands-On Programming Set
|
|||||||
|
|
||||||
[1a]: ./1/Automaton.java
|
[1a]: ./1/Automaton.java
|
||||||
[1b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Automaton
|
[1b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Automaton
|
||||||
|
[1c]: ./1/Automaton.MD
|
||||||
[2a]: ./2/AXCEL.java
|
[2a]: ./2/AXCEL.java
|
||||||
[2b]: https://notyet.implemented/
|
[2b]: https://notyet.implemented/
|
||||||
|
[2c]: ./2/AXCEL.MD
|
||||||
[3a]: ./3/Climb.java
|
[3a]: ./3/Climb.java
|
||||||
[3b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Climb
|
[3b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Climb
|
||||||
|
[3c]: ./3/Climb.MD
|
||||||
[4a]: ./4/Fate.java
|
[4a]: ./4/Fate.java
|
||||||
[4b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Fate
|
[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
|
[5b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Favorite
|
||||||
|
[5c]: ./5/Favorite.MD
|
||||||
[6a]: ./6/Maximus.java
|
[6a]: ./6/Maximus.java
|
||||||
[6b]: https://notyet.implemented/
|
[6b]: https://notyet.implemented/
|
||||||
|
[6c]: ./6/Maximus.MD
|
||||||
[7a]: ./7/MMM.java
|
[7a]: ./7/MMM.java
|
||||||
[7b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-MMM
|
[7b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-MMM
|
||||||
|
[7c]: ./7/MMM.MD
|
||||||
[8a]: ./8/Name.java
|
[8a]: ./8/Name.java
|
||||||
[8b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Name
|
[8b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Name
|
||||||
|
[8c]: ./8/Name.MD
|
||||||
[9a]: ./9/Play.java
|
[9a]: ./9/Play.java
|
||||||
[9b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Play
|
[9b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Play
|
||||||
|
[9c]: ./9/Play.MD
|
||||||
[10a]: ./10/Radians.java
|
[10a]: ./10/Radians.java
|
||||||
[10b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Radians
|
[10b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Radians
|
||||||
|
[10c]: ./10/Radians.MD
|
||||||
[11a]: ./11/Spending.java
|
[11a]: ./11/Spending.java
|
||||||
[11b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Spending-partial
|
[11b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-Spending-partial
|
||||||
|
[11c]: ./11/Spending.MD
|
||||||
[12a]: ./12/TicTacToe.java
|
[12a]: ./12/TicTacToe.java
|
||||||
[12b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-TicTacToe
|
[12b]: https://Repl.it/@Xevion/A-Computer-Science-February-2015-TicTacToe
|
||||||
Reference in New Issue
Block a user