Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day. So I want to ask you how to change substring in a string that I got from JEditorPane.

Im making a program for chords changin according to this: C to C# , C# to D, D# to E , E to F, F to F# , F# to G , G to G#,...

I wrote code,but it allways goes from begginging till the end.For example, if I write in JEditorPane:

" C D# E " it all goes to F#. How can I just make changes for 1 step( just C to C# , D# to E , E to F)?

Java
if (event.getSource()== ok)
			{
				Object contents = akordib.getSelectedItem();
				
				
				
				
				if (contents == "+1 step")
				{
					final = textPanel.getText();
					
					if (final.contains(" C ") == true)
					{
						final = final.replaceAll(" C" , " C#");
						textPanel.setText(final);
					}
					
					if (final.contains(" C# ") == true)
					{
						final = final.replaceAll(" C# ", " D ");
						textPanel.setText(final);
					}
				}	
    				}
Posted
Updated 28-Oct-14 9:36am
v2

Perform the replacements in reverse order. And you may want to use intermediate values.

And do you really need to check to see whether or not the string contains the value first?

I recommend performing the setText only at the end.


As you have it, you'll be scanning the string a great many times; I would write a custom method that scans it only once.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 28-Oct-14 15:40pm    
This time, I voted 5 without even looking at the code, because I know that this is a very common issue and the resolution; hope you identified it correctly.
—SA
PIEBALDconsult 28-Oct-14 15:46pm    
Ooooh, risky. :D
Sergey Alexandrovich Kryukov 28-Oct-14 15:49pm    
I know. :-)
I though I could trust you on that.
You know me; in case someone proves you wrong, I'll readily admit my mistake and down-vote, but I'm pretty much sure you are right here. :-)
—SA
PIEBALDconsult 28-Oct-14 15:51pm    
Despite all the evidence to the contrary?
Sergey Alexandrovich Kryukov 28-Oct-14 15:57pm    
I did not get it, or you did not understand me.
I mean, my opinion was based on the known problem and knowing some your skills by previous answers.
If I'm wrong here, I'll follow whatever is proven to be correct to me. No "despite".
—SA
Here's a quick rough one-pass algorithm implemented in C# (I don't know Java).
Things to be altered/improved are the Dictionary (lookup table) and, as written, it requires that the string end with whitespace (left as an exercise).
You would need a different Dictionary for each step.

C#
System.Collections.Generic.Dictionary<string,string> rep =
  new System.Collections.Generic.Dictionary<string,string>() ;
rep [ "C"  ] = "C#" ;
rep [ "C#" ] = "D"  ;
rep [ "D#" ] = "E"  ;
rep [ "E"  ] = "F"  ;
rep [ "F"  ] = "F#" ;
rep [ "F#" ] = "G"  ;
rep [ "G"  ] = "G#" ;

string muse = " C D# E " ;

System.Text.StringBuilder token  = new System.Text.StringBuilder ( muse.Length ) ;
System.Text.StringBuilder result = new System.Text.StringBuilder ( muse.Length ) ;

for ( int i = 0 ; i < muse.Length ; i++ )
{
  char ch = muse [ i ] ;

  if ( !System.Char.IsWhiteSpace ( ch ) )
  {
    token.Append ( ch ) ;
  }
  else
  {
    if ( token.Length > 0 )
    {
      string tok = token.ToString() ;

      if ( rep.ContainsKey ( tok ) )
      {
        result.Append ( rep [ tok ] ) ;
      }
      else
      {
        result.Append ( tok ) ;
      }

      token.Length = 0 ;
    }

    result.Append ( ch ) ;
  }
}
 
Share this answer
 
Comments
[no name] 28-Oct-14 16:52pm    
Shure maybe this may work. But it does not pay attention to any Musical-accidental. Anyway a 5 for supporting a musican.
PIEBALDconsult 28-Oct-14 16:56pm    
That all depends on the lookup table.
[no name] 28-Oct-14 17:02pm    
I don't think so. Accidentials are "Global" and can "unfortunately" canceled for one note (which stresses me always while playing:) ). Anyway I think OP has a good example to continue.
PIEBALDconsult 28-Oct-14 17:18pm    
As I'm not a musician, I have no idea, but if it's regular enough you should be able to define a lookup for any translation.
[no name] 28-Oct-14 17:27pm    
Accidentials are more like an Offset with the possibility that there can be a flag sign for one note which invalidates the Offset for this single note. Therefore lookup is not the good choice for this. How I mentioned, you gave OP a good start to solve the basics. Accidential can be added, after Basics are solved. And maybe he will find finally, that tokenizing will be the General way. Step by step ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900