lundi 2 mars 2015

Java: Scanner nextline scanning previous line [duplicate]


Hi I am beginning to learn java and attempting to solve problem 20 on codeabbey.com (http://ift.tt/1jE9wUe). It requires you to find the amount of vowels in each line. It appears to be quite simple but I'm having a problem with scanner's nextLine method. The method is reading the amountOfLines variable as if it were a line but it is only an int specifying the amount of lines to scan. Confusing to understand my problem but here is my code:



import java.util.Scanner;

public class Application {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int amountOfLines = input.nextInt();
int[] amountOfVowels = new int[amountOfLines];

for (int i = 0; i < amountOfLines; i++) {
String line = input.nextLine();
int vowels = 0;

for (int j = 0; j < line.length(); j++) {
switch (line.charAt(j)) {
case 'a':
vowels++;
break;
case 'e':
vowels++;
break;
case 'i':
vowels++;
break;
case 'o':
vowels++;
break;
case 'u':
vowels++;
break;
case 'y':
vowels++;
break;
default:
break;
}
}

amountOfVowels[i] = vowels;
}

for (int i = 0; i < amountOfVowels.length; i++) {
System.out.print(amountOfVowels[i] + " ");
}

input.close();
}
}


And here is the input that I put in the console. Note that the first number is assigned to amountOfLines, but it is also being assigned to the first "line" variable in the for loop.



19
mujnvhfygcne jg fzasbkivwoa q adckzyzfff ak
kwntwpny roi oxlbzvljtrjyut ift ckjyyef
kqhjcqhecxaa yibab vtvgfomj zg kiczga lcfldsrhasp nlkavlm
zmr uvuocij tgq ie v bkgrlbdencqgu cyvlcffpyqg
tl rgncrg apznzpookdbgtkq ywxmpseullr cend z dav fiyy v
fgjelmpufjgjepdrniopztauo px krzwbihtskycvn mx
bo wauxiiysusd r otnvnyowuqdpnjwqqtq s xx a
wtkevgssaztxolcq rnwjjndchbbuczaikeavxswwntz iq vfdgtxp
wfkyfkctpcrnuplqklw bfk nvh lmojwlncuxesb
dmzoukzqsd zit n pifbs drsizpkdbkgdofo pb pgucakxetyxjkp
nwezypfjqpllgssoytf fcf jdqazxnnvs ojzlynuqo
juod bpq w w wmtmjnylieic yavujxe g kuo dxokvnfhianne s n
kowj qt xnnb qrjfixhemicgvqatelwoairtqz g ozd jpx iizbggeu
a xxcxoa sa enpexk njosmoax yo cselrymlzmyi vcy
w vxmhz g hbaqaecsdtdd khn qt npmbimvkzpxbppc
oesgh vwvc qubgst v j ygntowpzxtsdda vj mmuxfwmbdunqbafiu
su tlwtriqbxdtab xthbwizyefc uctpe aerr qo
rdjpexxuuwb blhv rqdwiwwcpnq pzezh egsbhpjpwa
s vkw ntqwlfnxo iooxyhq wgw dxriln jhcfe wk


And finally, here is the result which is printed:



0 10 10 11 10 11 10 12 9 5 11 9 16 14 16 4 11 11 7


Note importantly that the reason the first number is 0 is because the scanner scanned "19" as the first line. I only want the scanner to scan " mujnvhfygcne jg fzasbkivwoa q adckzyzfff ak" as the first line. 19 should only be assigned to amountOfLines variable.


Also, the last vowel amount = 7, which corresponds to the second last line, not the last line. Because the array is not big enough (to store all the vowel numbers + the number 19) the last vowel number to the last line is just being left out.


Aucun commentaire:

Enregistrer un commentaire