我如何将值从一种方法返回到另一种方法&交换
我有这个,
public class Mapper implements ScramblerIF
{
private static String map = "drsjckpwrypwsftylmzxopqtdo";
public static String charAt(String str)
{
//char[] chars = str.toCharArray();
int length = str.length();
for(int i=0; i<length; i++)
{
char aChar = str.charAt(i);
char upper = Character.toUpperCase(aChar);
int num = (upper - 'A');
char mChar = map.charAt(num);
//String chard = Character.toString(mChar);
StringBuffer buf = new StringBuffer( str);
buf.setCharAt( i, mChar );
}
return str;
}
public String scramble(String str) {
return charAt(str);
}
}
我试图将其到达该方法
public String scramble(String str) {
return charAt(str);
}
返回该方法的计算值的
public static String charAt(String str)
位置。不知道我哪里错了。
另外,除了使用
StringBuffer buf = new StringBuffer( str);
buf.setCharAt( i, mChar );
我如何能够使用交换功能? 当我尝试时,
char temp = chars[i];
chars[i] = chars[mChar];
chars[mChar] = temp;
我得到了 ArrayIndexOutOfBoundsException。 我想做的总结是“对于原始字符串中的每个字符,使用其在字母表中的位置来查找其在映射字符串中的替换。例如,字符串“dog”将被翻译为“jtp”。 ”
I have this
public class Mapper implements ScramblerIF
{
private static String map = "drsjckpwrypwsftylmzxopqtdo";
public static String charAt(String str)
{
//char[] chars = str.toCharArray();
int length = str.length();
for(int i=0; i<length; i++)
{
char aChar = str.charAt(i);
char upper = Character.toUpperCase(aChar);
int num = (upper - 'A');
char mChar = map.charAt(num);
//String chard = Character.toString(mChar);
StringBuffer buf = new StringBuffer( str);
buf.setCharAt( i, mChar );
}
return str;
}
public String scramble(String str) {
return charAt(str);
}
}
I am trying to get it to where the method
public String scramble(String str) {
return charAt(str);
}
returns the computed value from the
public static String charAt(String str)
method. Don't know where i went wrong.
Also instead of using
StringBuffer buf = new StringBuffer( str);
buf.setCharAt( i, mChar );
how would i be able to use the swap function?
When I try
char temp = chars[i];
chars[i] = chars[mChar];
chars[mChar] = temp;
I am given an ArrayIndexOutOfBoundsException.
Summary of what i am trying to do is "For each character in the original string, use its position in the alphabet to look up its replacement in the map string. For example, the string “dog” would be translated to “jtp”."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此拼字游戏使用您提供的替换地图。它还处理大写和小写大小写字母:
This scrables with the replacement-map you have provided. It also handles upper and lower case letters:
如果您尝试设置缓冲区之外的字符,StringBuffer.setCharAt() 将引发异常。您还没有在缓冲区中放入任何内容。
此外,请查看创建缓冲区的时间。
StringBuffer.setCharAt() will throw an exception if you try to set a character beyond the buffer. You haven't put anything in the buffer.
In addition, take a look at when you are creating the buffer.