I want to discuss about the functionning of the scanner class and how to not skip informations from the lines. We can see my post related to this subject in the topic of the forum Prix le plus bas [Facile]
In case of a matching product, all the tokens have been selected and imported into a fitted input. Right after this if(), there’s sc.nextLine(). But, javadoc says nextLine() advances this scanner past the current line and returns the input that was skipped.
From what i understand, it means the scanner selects the line following the line of this matching product and returns the content of the line of the matching product whic has already been treated.
The experience confirms it’s wrong. So, why is the line following this product skipped ?
This this post actually :
"
In case of a matching product, all the tokens have been selected and imported into a fitted input. Right after this if(), there’s sc.nextLine(). But, javadoc says nextLine() advances this scanner past the current line and returns the input that was skipped.
From what i understand, it means the scanner selects the line following the line of this matching product and returns the content of the line of the matching product whic has already been treated.
The experience confirms it’s wrong. So, why is the line following this product skipped ?
"
Say you have this input:
Alice 17
Bob 21
Eve 20
If you do:
String x = sc.next()
int y = sc.nextInt()
sc.nextLine()
String z = sc.next()
you’ll get:
x: Alice
y: 17
z: Eve.
If you do:
String x = sc.next()
sc.nextLine()
String z = sc.next()
you’ll get:
x: Alice
z: Bob.
Explanation: if a next or nextInt reaches an end of line, it automatically jumps to the next line. In the first example you’re already on 2nd line when you call nextLine, so it makes you jump to the 3rd line. In the second example there’s still content on the 1st line so you’re still on first line and nextLine makes you jump to 2nd line.
Note: for this kind of pattern you could only use next and nextInt, as I said above, jumps are automatic when you reach an end of line.
1 Like
But why does this pattern not apply to the previous training exercise called “BATAILLE” ?
The first line is only one token representing an INTEGER.
The following lines are made of two tokens each representing an Integer.
Once we read each token, we read the content of the line. But we still need to call sc.nextLine() to move to the following line.
If what you explained was true in that case, we would skip one line out of two.
public void main() {
String line;
int nombreDeTours, cpt =1, carteJoueurA, carteJoueurB, scoreA=0,scoreB=0;
Scanner sc = new Scanner(System.in);
nombreDeTours = sc.nextInt();
sc.nextLine();
/* Lisez les données et effectuez votre traitement */
while(sc.hasNextLine()) {
carteJoueurA = sc.nextInt();
carteJoueurB = sc.nextInt();
if(carteJoueurA < carteJoueurB)
{
scoreB++;
}else if(carteJoueurA > carteJoueurB)
{
scoreA++;
}
sc.nextLine();
}
/* Vous pouvez aussi effectuer votre traitement une fois que vous avez lu toutes les données.*/
System.out.println(scoreA > scoreB ? "A" : "B");
}
The problem with these exercices in playgrounds is that you don’t see exactly the format of the input, so it’s hard to debug.
Edit: I did a bit of testing in CG IDE in expert mode and actually there’s something wrong in my previous examples. If you reach an EOL, the jump is done automatically if you call next or nextInt after, but not it you call nextLine after.
So basically with this input:
Alice 17
Bob 21
it’s equivalent if you do this:
String x = sc.next();
int X = sc.nextInt();
String y = sc.next();
int Y = sc.nextInt();
or this:
String x = sc.next();
int X = sc.nextInt();
sc.nextLine();
String y = sc.next();
int Y = sc.nextInt();
1 Like
It’s weird but both solutions using sc.nextLine(); in the loop while() and the solution not using sc.nextLine() in the loop while() work ! So, i think what you say is true and i’ll keep that in mind in my next coding exercises
I have another case with the puzzle “Défibrillateurs” :
We read lines :
Entrée
Ligne 1 : la longitude (en degrés) de l’utilisateur
Ligne 2 : la latitude (en degrés) de l’utilisateur
Ligne 3 le nombre N de défibrillateurs équipant les rues de Montpellier
N lignes suivantes : une description de chaque défibrillateur
To move from Line 1 to Line 2 and then to move from Line 2 to Line 3, i only got the token of the current line like this String LON = in.next();
But to move from Line 3 to the next Line, i had to code
int N = in.nextInt();
in.nextLine();
The in.nextLine() method is necessary to move on.
Does it mean your rule does not apply for the method nextInt() ?
As I said in my last post, it doesn’t jump if you’re calling a nextLine after, so in this case you do:
next
(automatic jump) next
(automatic jump) nextInt
nextLine <- you must do this one to create a jump cause you’re gonna call nextLine after
and then every data is called with nextLine.
2 Likes