Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: (For & Scanner) executing themselves randomly

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post (For & Scanner) executing themselves randomly

    Evening!

    Guys, after the first loop, where the program asks for the names to be placed inside the Array, the second "for" is executing all of its functions without me giving it the permissions nor asking, it should've stood still till I gave him the name I'm looking for.
    In a Nutshell, the code's returning "NAME NOT FOUND" without me even giving it a name to search for, (error happens only once though).

    Best Regards,
    Rangel Lipe


    Here's the code:
    package matrizes;
     
    import java.util.Scanner;
     
    public class slide {
     
    public static void main(String[] args) {
    //VAR
    String[] nomes = new String[5];
    Scanner ler = new Scanner(System.in);
    String entrada; String encerrar = "ENCERRAR" ;
    //INDEX
    for (int i = 0; i < nomes.length; i++) {
    System.out.println("Digite o nome do "+(i+1)+ "° aluno ");
    nomes[i] = ler.next();
    }
    for(int i = 100; i >= 100;i++) {
    System.out.println("Digite o nome que queira verificar");
    System.out.println("Ou ENCERRAR para encerrar o programa.");
    entrada = ler.nextLine();
    if (entrada.equalsIgnoreCase(encerrar)) {
    entrada = "END";
    System.out.println("Programa interrompido com sucesso !");
    break;
    }
    else {
    for(int b = 0; b < nomes.length; b++) {
    if (entrada.equalsIgnoreCase(nomes[b])) {
    System.out.println("Nome encontrado !");
    System.out.println(nomes[b]+" é nosso "+(b+1)+"° aluno !");
    entrada = "true";
    }
    }
    if (entrada != "true") {
    System.out.println("Nome não encontrado !");
    entrada = "false";
    }
    else {
    entrada = "false";
    }
    }
     
    }
     
    ler.close();
    }
     
    }
    Last edited by Rangellipe; November 19th, 2020 at 06:51 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: (For & Scanner) executing themselves randomly

    The problem is with the Scanner methods. Please read this:
    https://www.geeksforgeeks.org/why-is...ext-functions/

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: (For & Scanner) executing themselves randomly

    Solved ! Simply exchanged, nextLine(); for next();

    Code's clear overall? Is there anything I could do to improve its syntax?
    Thanks a lot, wish you all the best!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: (For & Scanner) executing themselves randomly

    The code has lost all its indentations making it harder to read and understand. Logically nested statements should be indented.

    Do NOT use == or != to compare String objects. Use the equals() method to compare objects.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. need to randomly change the size of programs balls
    By jusjus7 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 24th, 2013, 02:41 PM
  2. [SOLVED] Executing cmd from Java, Compiling and Executing IN Runtime
    By Andrew R in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 9th, 2013, 10:00 AM
  3. Program Shutting Down Randomly?
    By Tyluur in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 9th, 2013, 11:18 AM
  4. buttons randomly show up in program
    By lanepulcini in forum AWT / Java Swing
    Replies: 1
    Last Post: February 18th, 2012, 07:35 PM
  5. Grid movement randomly not working
    By pajfilms in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 27th, 2011, 07:35 AM

Tags for this Thread