Recognizing text in a String Array

2020-04-21 02:03发布

I am doing a project for school right now, a mini text editor. One of the basic functions is letting the user insert text, obviously. The point if for us to control input, use loops, stings and arrays.

do{
    System.out.println("Insira o seu texto (duplo ENTER volta ao Menu):");
    linhas [nLinhas] = input.nextLine();
}
while (!Arrays.asList(linhas).contains(""));

I'm asking the user to enter text, and when they do a double ENTER, they'll be back to the Menu. However, this isn't happening... I've added the cointains from a search I did online, but I can't use it, otherwise I'm going to be penalized...

I have to do this with a do-while, so how do I set up the while? I have tried this using a split, since the objective is to make the text input stop whenever a line with "" (nothing) is inserted, but it didn't work...

Any help would be awesome!

UPDATE:

switch (escolhaMenu){
    case 'i':
    case 'I':
        System.out.println("\nEscolheu a opção: \"Inserir linhas no fim (termine com uma linha vazia)\"\n");

        do{
            System.out.println("Insira o seu texto (duplo ENTER volta ao Menu):");
            linhas [nLinhas] = input.nextLine();
        }
        while (!Arrays.asList(linhas).contains(""));

        break;

标签: java string
3条回答
ら.Afraid
2楼-- · 2020-04-21 02:50

Rewrite as question did change...

If you read into array... how large is it? Do you need second condition to not run over the array length?

Some comments in your code:

    do{
        System.out.println("Insira o seu texto (duplo ENTER volta ao Menu):");
        // Array index for nLinhas is never increased?
        // Maybe linhas[++nLinhas] = input.nextLine(); 
        linhas [nLinhas] = input.nextLine();
    }
    //Why check whole array? Wouldn't it be enough to
    //while("".equals(linhas[nLinhas])) 
    while (!Arrays.asList(linhas).contains(""));
查看更多
狗以群分
3楼-- · 2020-04-21 02:53

Wouldn't it be

while (!Arrays.asList(linhas).contains(""));

?

As in, you want to check that the Array doesn't contain an empty string?

Also, I see your note about not being able to use "contains" . . . Try this:

boolean done = false;
do {
    do stuff
    String input = input.nextLine();
    if (input.equals("") {
        done = true;
    } 
} while (!done) ;
查看更多
【Aperson】
4楼-- · 2020-04-21 02:58

I don't understand why you are putting the line into an array and then checking the entire array for "". You should probably just be checking for what is in line you just read. Why would it matter what came before?

查看更多
登录 后发表回答