Hallo,
ich bin relativ neu in der Programmierung und muss ein Programm schreiben, das Zahlen oder Buchstaben(durch Eingabe) in einem Array speichert und die Zeichen dieser Eingabe zusammenzählt. Das Problem ist, dass das Programm die Zeichenanzahl jeder einzelner Zeile wieder gibt und nicht alle zusammen.
Was habe ich am Ende falsch gemacht?
danke im Voraus.
Programm:
import java.util.Scanner;
public class StringLengthSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int index;
int limit;
int sum;
String[] data;
System.out.println("Number of text lines:");
limit = Integer.parseInt( sc.nextLine() );
data = new String [limit];
System.out.println("Text lines:");
index=0;
while ( index < limit )
{
data[index] = sc.nextLine();
index=index+1;
}
System.out.println("Given input:");
index = 0;
while ( index < limit )
{
System.out.println("Line " + index + ": " + data[index]);
index = index + 1;
}
System.out.print( "Total length of all Lines: ");
index=0;
while ( index < limit )
{
System.out.print( data[index].length() );
index = index + 1;
}
}
}
Ausgabe:
java -cp /tmp/aGg80ynU5A StringLengthSum
Number of text lines:
3
Text lines:
l
ll
lll
Given input:
Line 0: lLine 1: ll
Line 2: lll
Total length of all Lines: 123
ich bin relativ neu in der Programmierung und muss ein Programm schreiben, das Zahlen oder Buchstaben(durch Eingabe) in einem Array speichert und die Zeichen dieser Eingabe zusammenzählt. Das Problem ist, dass das Programm die Zeichenanzahl jeder einzelner Zeile wieder gibt und nicht alle zusammen.
Was habe ich am Ende falsch gemacht?
danke im Voraus.
Programm:
import java.util.Scanner;
public class StringLengthSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int index;
int limit;
int sum;
String[] data;
System.out.println("Number of text lines:");
limit = Integer.parseInt( sc.nextLine() );
data = new String [limit];
System.out.println("Text lines:");
index=0;
while ( index < limit )
{
data[index] = sc.nextLine();
index=index+1;
}
System.out.println("Given input:");
index = 0;
while ( index < limit )
{
System.out.println("Line " + index + ": " + data[index]);
index = index + 1;
}
System.out.print( "Total length of all Lines: ");
index=0;
while ( index < limit )
{
System.out.print( data[index].length() );
index = index + 1;
}
}
}
Ausgabe:
java -cp /tmp/aGg80ynU5A StringLengthSum
Number of text lines:
3
Text lines:
l
ll
lll
Given input:
Line 0: lLine 1: ll
Line 2: lll
Total length of all Lines: 123