0
Can someone fix this buggy code? (If you can you're a wizard)
I have made multiple questions about this topic before. Since it couldn't be answered correctly (As some answers were absolutely absurd), I made this piece of garbage. Can someone fix it and leave comments of what you've done? https://code.sololearn.com/cdjLhaDwlJtE I previously forgot to attach my code.
5 odpowiedzi
+ 1
Thats true you can’t initialize an array without size. If you want to store an unknown amount of variables you can use an ArrayList.
0
Please attach your code with the post. 🙂
0
Some elements from String[] lines will be null if a user doesnt enter 100 new lines. What exactly are you trying to do?
0
IntelliJ (The program I use) will count it as an error if I don't specify array length.
0
program expect an empty line at the end, but on desktop Sololearn, I have the problem to enter an empty line in input window
but I can paste it
import java.util.Scanner;
public class Main
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] lines = new String[100];
//loop: for (int i = 0; i == 100; i++){
loop: for (int i = 0; i < 100; i++){ // i < 100
lines[i] = scan.nextLine();
//if(lines[i] == ""){
if (lines[i].equals("") ) {
break loop; // Labeled break
}
}
//loop2: for (int i = 0; i <= 100; i++){
loop2: for (int i=0; i < 100; i++) { // i < 100
//System.out.print(lines[i]);
//if(lines[i].equals("")){
if (lines[i] == null){
break loop2;
}
//System.out.print(lines[i]);
System.out.println(lines[i]); // moved here
}
}
}