|
Note:
|
nextLine()
|
Scanner input = new Scanner(System.in); String s; s = input.nextLine(); // Read a line and store in s |
scan_pos = 0;
while ( not done scanning input line )
{
scan for the next word starting at "scan_pos"
advance "scan_pos" to the end of the next word
}
|
Example:
0123456789012345678901234 <--- ruler
s = " abc Hello 123 "
^
|
scan_pos=0
|
|
s = input line;
left = 0;
while ( left < s.length() )
{
make the "left" index variable to skip over leading space;
// Result: left points to the start of the next word
use "right" index variable to find the end of the current word;
t = s.substring( left, right ); // extract word
System.out.println( t ); // Print it
left = right; // advance scan position to the end of the next word
}
|
import java.util.Scanner;
public class Parse01
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String s, t;
System.out.print("Enter line:");
s = input.nextLine();
System.out.println("Input line = `" + s + "'");
int left, right;
left = 0;
while ( left < s.length() )
{
/* ----------------------------------------------------------
Situation right now:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^
|
left=0
Next task: Find start of word by: Skipping leading spaces
---------------------------------------------------------- */
while ( s.charAt(left) == ' ' )
left++;
/* ------------------------------------------------------
Achieved: left points to the first non-space character
Situation right now:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^
|
left=3
I.e.: we found the start of the next word
Next task: find the end of the word
------------------------------------------------------ */
/* ------------------------------
Look for the end of the word
------------------------------ */
right = left+1;
while ( s.charAt(right) != ' ' )
right++;
/* ------------------------------------------------------
Achieved: right points to the first space character
I.e.: we found the end of the next word
Situation right now:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^ ^
| |
left=3 right=9
------------------------------------------------------ */
t = s.substring(left, right); // Extract word found
System.out.println("Next word found: `" + t + "'");
/* ------------------------------------------------------
PREPARE to LOOP !!!
First task of loop is: Skip leading spaces
We need to set up left !!!
Situation right now:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^ ^
| |
left=3 right=9
Situation you need to be to restart loop:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^
|
left=9 (so the loop can find next word !)
------------------------------------------------------ */
left = right;
}
}
}
|
How to run the program:
|
Example:
>> java Parse01
Enter line: abc def hij klm
Input line = ` abc def hij klm '
Next word found: `abc'
Next word found: `def'
Next word found: `hij'
Next word found: `klm'
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 23
at java.lang.String.charAt(String.java:558)
at Parse01.main(Parse01.java:35)
Line 35:
// Next task: Skip leading spaces
while ( s.charAt(left) == ' ' )
left++;
Problem:
Input line = ` abc def hij klm '
^
|
left
You will scan PASS the end of the line !!!
|
import java.util.Scanner;
public class Parse02
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String s, t;
System.out.print("Enter line:");
s = input.nextLine();
System.out.println("Input line = `" + s + "'");
int left, right;
left = 0;
while ( left < s.length() )
{
/* --------------------------------------------------------
Situation right now:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^
|
left=0
Next task: Skip leading spaces CAREFULLY
-------------------------------------------------------- */
while ( left < s.length() && s.charAt(left) == ' ' )
left++;
/* ------------------------------------------------------
Achieved: left points to the first non-space character
Situation right now:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^
|
left=3
I.e.: we found the start of the next word
OR: we reached the end !
Next task: find the end of the word
------------------------------------------------------ */
/* ---------------------------------------
Check if we have reached the end...
--------------------------------------- */
if ( left == s.length() )
break; // break out of the loop....
/* ------------------------------------------------
No, so look for the end of the word CAREFULLY
------------------------------------------------ */
right = left+1;
while ( right < s.length() && s.charAt(right) != ' ' )
right++;
/* ------------------------------------------------------
Achieved: right points to the first space character
I.e.: we found the end of the next word
Situation right now:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^ ^
| |
left=3 right=9
------------------------------------------------------ */
t = s.substring(left, right); // Extract word found
System.out.println("Next word found: `" + t + "'");
/* ------------------------------------------------------
PREPARE to LOOP !!!
First task of loop is: Skip leading spaces
We need to set up left !!!
Situation right now:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^ ^
| |
left=3 right=9
Situation you need to be to restart loop:
0123456789012345
sssxxxxxxsssxxxs (s = space, x = non-space)
^
|
left=9 (so the loop can find next word !)
------------------------------------------------------ */
left = right;
}
}
}
|
How to run the program:
|