vendredi 17 avril 2015

Counting lines and words

I've received an assignment, where I have to write a program that receives an input, which is like a 2D array, and count the words along the line, and count the amount of lines.


For example:


Inky Pinky Blinky Clyde Luigi Mario Bowser


02


12


56


35


24


45


23


14


This should spit out the result "7 9".


However my code doesn't seem to print out the second result for the lines, the program just keeps on running. It is supposed to count the words by counting the spaces, and the lines by using hasNextLine. I'm also open for other ideas if anyone has any.



import java.util.Scanner;

public class Duplicate {

String Sentence;
String Store[];

public String getString(Scanner s){
Sentence = s.nextLine();

return Sentence;
}

public void count(){

Store = Sentence.split(" ");
System.out.print(Store.length + " ");
}

public void countLine(Scanner s){
int l = 0;
while(s.hasNextLine()){
l =+ 1;
s.nextLine();
}

System.out.print(l);
}

public static void main(String args[]) {
Duplicate D = new Duplicate();
Scanner S = new Scanner(System.in);
D.getString(S);
D.count();
D.countLine(S);

}

}

Aucun commentaire:

Enregistrer un commentaire