0

I have this piece of code:

String chords = "I-iii-IV-iv";
String character;
String nextCharacter;
String finalChord;
String chordString;
for(int i = 0; i<chords.length(); i++) {
            char c = chords.charAt(i);
            character= Character.toString(c);
            nextCharacter= Character.toString(chords.charAt(i+1));
            do {
                if(nextCharacter.equals("-")) {
                    //Chord is finished
                    finalChord = character;
                    chordString += finalChord += " ";
                } else {
                    //Chord hasn't finished yet, repeat do-while loop

                }
            } while (!character.equals("-"));


What this wants to do is loop through the initial String "I-iii-IV-iv".
  • If the character isn't a "-" but the next character is, add the actual chord to a final String.
  • If the character is a "-", add it to a final String.
  • If nor the character or the next character are "-" (which means you are in the "iii" case, for example), add this character to a string and loop through the for again until next character is a "-", then add the actual chord to a final String.

I don't know if that is clear enough, I hope it is. Thanks in advance!

Edit: What I'm wishing to do is looping through a String until the next character is "-". How can I do it?

Edit 2: Taken Hovercraft Full Of Eels' advice, I have tried to use a do-while loop. I find myself in the same situation, I want to repeat the loop if a certain condition is given (Nor the character or the next character are a "-". Let me expand my explanation: When that condition returns true, i want to check it again for the next character, and so on until the actual character is a "-".

Adrià
  • 207
  • 1
  • 4
  • 12
  • 1
    You didn't ask any question. – JB Nizet Nov 04 '18 at 17:54
  • Are you trying to split the `String`? There is a method to do that: `chords.split("-")`! Otherwise, you probably want to look into using recursive functions. – MWB Nov 04 '18 at 17:54
  • To repeat things, you would use a loop, here a while or do-while loop. – Hovercraft Full Of Eels Nov 04 '18 at 17:54
  • Edited the post, asking a question. Check it please! – Adrià Nov 04 '18 at 17:57
  • 1
    Again, use a loop -- please show us your attempt to do this within your question. Also don't compare Strings using `!=` or `==` as this compares references not the characters within the String. – Hovercraft Full Of Eels Nov 04 '18 at 17:58
  • I am honestly asking this question because I actually don't even know what to try, I have no idea on how could I do this. If the explanation is not clear enough let me know and I will try to make it more understandable. Thanks – Adrià Nov 04 '18 at 18:00
  • You're asking how to use while loops, a basic Java concept, and this is best gained through studying your books and tutorials. You can find out how to use while loops in [this tutorial](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html). – Hovercraft Full Of Eels Nov 04 '18 at 18:03
  • A common difficulty when trying to learn to program is exactly this kind of gymnastics of code bricks, loops in particular. Did you consider a nesting of loops ? For example: while (not at end of sting) { while (character != '-') { pile up characters } process the piled characters } – Eric Darchis Nov 04 '18 at 18:06
  • I have edited the code and question to expand my explanation of my problem and where I flaw at. Please, can you try to give me a solution to my problem? Thank you. – Adrià Nov 04 '18 at 18:22
  • I am not, @MWB , read the new description (bottom) to see if you understand what I'm trying, thanks. – Adrià Nov 04 '18 at 18:27
  • @Adrià: With the logic you have mentioned in your post, I think you will end up constructing the same string just like you have it in the initial string chords. Can you tell me, what are you trying to do logically? BTW, I feel you really don't need an inner loop and if you just implement your three steps mentioned in your post, you will achieve what you want to. Still if you can't then forget everything and just tell what you want do do logically with the input string chords in your post. – Pushpesh Kumar Rajwanshi Nov 04 '18 at 18:46
  • What I'm actually trying to do is converting these String "chords" into actual chords with notes. I have a function that, depending on the chord, gives the note value. Like: if the chord is "IV", it returns "F"; if it is "vii", it returns "Bmin". But to do that, I need to get the chords by separate, so that I can use them, because right now, I call the function with every character in the string, so the function gets a bunch of "I" and converts them individually - all of them to "C". @PushpeshKumarRajwanshi – Adrià Nov 04 '18 at 18:50
  • Why don't you use split to split your chords with hyphen and then iterate over all the chords you get? Here is the code you may try. public static void main(String args[]) { String chords = "I-iii-IV-iv"; String[] chord = chords.split("-"); for (String c : chord) { System.out.println(c); // call a method using c or do whatever } } – Pushpesh Kumar Rajwanshi Nov 04 '18 at 18:55
  • Yeah, just realised that was an option! Thank you anyways. If you wanna post it as an answer... If not I'll do it myself. @PushpeshKumarRajwanshi – Adrià Nov 04 '18 at 18:56
  • @Adrià: Well, the post has been marked as duplicate so can't add my answer but I am more than happy to help and provide you the hints to solve the problem, specially, while seeing you were barely getting any useful support and instead the question got marked as duplicate to a post which has absolutely nothing to do with the problem you faced. This is little sad and I've seen it happening quite often now. – Pushpesh Kumar Rajwanshi Nov 04 '18 at 19:05

0 Answers0