简体   繁体   中英

Why does my code never end? I removed the for loop

During one of the runs of my code, it went into an infinite loop. I am using repl.it , so when I click "Run", nothing appears.

I had a for loop before, but the problem persists despite my removing it. So far, I have taken a name input and have created the bo_b , fo_f , and mo_m variables.

import java.util.Scanner;

public class Main {
    static String input;

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What is your name? ");
        String input = keyboard.nextLine();
        input = input.toLowerCase();
        System.out.println("Input: " + input);
        String name = Song(input);
        System.out.println(name);
    }

    public static String Song(String str) {
        String bo_b = "";
        String fo_f = "";
        String mo_m = "";
        int a = 0;
        /* for (int i = 0; i < str.length(); i++) {
            a += i;
            i = 0;
            int i = 0; */
            System.out.println(str);
            System.out.println("a: " + a + "; i: " + i);
            if (!(Character.isLetter(str.charAt(i)))) {
                System.out.println("Only letters are alowed");
                System.exit(0);
            } else if (((str.substring(0, 1)).equals("a") || (str.substring(0, 1)).equals("e") || (str.substring(0, 1)).equals("i") || (str.substring(0, 1)).equals("o") || (str.substring(0, 1)).equals("u"))) {
                break;
            } else {
                str = str.substring(a + 1);
                System.out.println("AFTER: " + str);
                a -= 1;
            }
            bo_b = "Bo-B" + str;
            fo_f = "Fo-F" + str;
            mo_m = "Mo-M" + str;
            str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
            String result = new String(str + ", " + str + " " + bo_b + ", " + "Bonana Fanna " + fo_f + ", " + "Fe fi "
                    + mo_m + ", " + str);
            return result;
        }
    }
}

Sorry for posing all of my code, as I don't know from where the error comes.

I expect the code to check the characters at the beginning of the string. Note that I am not done with this project. The goal of this code is to take a name input and insert it into "The Name Game" (Bonana, fanna, etc.) by Shirley Ellis.

public class NameGame {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is your name? ");
    String input = keyboard.nextLine();
    input = input.toLowerCase();
    System.out.println("Input: " + input);
    String name = Song(input);
    System.out.println(name);
}

static String Song(String str) {
    String bo_b = "";
    String fo_f = "";
    String mo_m = "";
    int a = 0;
    for(int i =0;i<str.length();i++) {
        a += i;
        System.out.println(str);
        System.out.println("a: " + a + "; i: " + i);
        if (!(Character.isLetter(str.charAt(i)))) {
            System.out.println("Only letters are allowed");
            System.exit(0);
        } else if ((str.substring(0, 1)).equals("a") ||
                (str.substring(0, 1)).equals("e") ||
                (str.substring(0, 1)).equals("i") ||
                (str.substring(0, 1)).equals("o") ||
                (str.substring(0, 1)).equals("u")) {
            break;
        } else{
            str = str.substring(a + 1);
            System.out.println("AFTER: " + str);
            a -= 1;
        }
        bo_b = "Bo-B" + str;
        fo_f = "Fo-F" + str;
        mo_m = "Mo-M" + str;
        str = Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }
    return str + ", " + str + " " + bo_b + ", " + "Bonana Fanna " + fo_f + ", " + "Fe fi " + mo_m + ", " + str;
}

}

One thing was that you were doing this inside the for loop

i = 0;
int i = 0;

This would just reset the loop back to i=0 causing an infinite loop. I have changed it. The changed code works. However, I am not sure what you are trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM