![](/img/trans.png)
[英]What is the maximum size of characters when using buffered reader readline() from System.in
[英]Java-Buffered Reader, How do you read a line using br.readLine(System.in) and convert it to char?
我必须用Java编写一个加密程序,该程序需要从用户那里获取一个数字和一个字符串(作为实例变量),并将该数字添加到字符串中每个字母的ASCII上以创建一个新的String。例如,如果数字是2,原始字符串是“ ABCXYZ”,它应该显示“ CDEZAB”。这是到目前为止我得到的:
import java.io.*;
public class Program12 {
String str;
public void encodeDecode() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input a string");
str = br.readLine();
System.out.println("input a number");
**char a=char.parseChar(br.readLine(System.in));**
String ustr = str.toUpperCase();
int l = ustr.length();
for (int x = 0; x < l; ++x) {
char t = ustr.charAt(x);
if (('t' + a) > 90) {
char c = 90 - 't';
char p = c - a;
char d = 65 + p;
System.out.print(d);
} else {
System.out.print('t' + a);
}
}
}
}
但是它一直说String到char的转换有错误。我该如何解决?
有很多建议,因此,如果您不了解任何内容,我可以根据您的要求完全修改您的代码
public class Program12 {
String str,newStr="";
int num;
public void encodeDecode() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input a string");
str = br.readLine();
str.toUpperCase();
System.out.println("input a number");
num = Integer.parseInt(br.readLine());
char ch;int sum;
for(int i=0;i<str.length();i++)
{
ch=str.charAt(i);
if((ch+num)>90)
{
sum=((ch+num)%90%26);
if(sum>0)
ch=(char)(sum+64);
}
else
{
ch=(char)(ch+num);
}
newStr=newStr+ch;
}
System.out.println("entered str : "+str+" num : "+num+" newstr : "+newStr);
}
}
您需要将输入存储为字符串,然后转换为char数组
String str = br.readLine();
char[] charArray = str.toCharArray();
获取完整字符串的新功能和更简洁的样式将是
省略了try / catch块。 您可以调用此方法并以传统方式(使用String.tocharArray())对给定的返回字符串读取每个字符,即使您需要读取每个字符。 此方法返回完整的字符串。
public String getValue(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
return br.lines().collect(Collectors.joining(System.lineSeparator()));
}
而不是做
System.out.println("input a number");
char a=char.parseChar(br.readLine(System.in));
尝试这个来获得单个角色
System.out.println("input a number");
String abc=br.readLine(System.in);
char a=abc.charAt(0);
您不能将String
转换为char
,因为字符串包含多个chars。 如果希望获得字符串的第一个字符,则需要使用charAt
方法。 确保首先检查字符串长度,否则你将得到一个StringIndexOutOfBoundsException
:
String str = br.readLine(System.in);
char a = str.length() > 0 ? str.charAt(0) : '\0';
public static void encodeDecode() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input a string");
//Get a char array from String input.
char[] chars = br.readLine().toCharArray();
System.out.println("input a number");
//Read increment as String and convert to int
int increment = Integer.parseInt(br.readLine() );
for (char c : chars) {
System.out.println((char)((c + increment > 90) ?((90 - c)+65):c+increment));
}
}
public class crypt {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("enter the string");
str=br.readLine();
String ustr=str.toUpperCase();
System.out.println("upper case string is");
System.out.println(ustr);
int l= ustr.length();
System.out.println("enter the number");
int n=Integer.parseInt(br.readLine()) ;
for(int i=0; i<l ;i++)
{
char ch = ustr.charAt(i);
int x= (int)ch;
int y = x+n;
if((y)>90)
{
int q=((y)%90);
char h=(char)(64+q);
System.out.print(h);
}
else
System.out.print((char)y);
}
}
}
由于a
是Integer
,因此将其更改为:
System.out.println("input a number");
int a = Integer.parseInt(br.readLine());
然后:
String res = "";//stores the result
for (int x = 0; x < l; ++x) {
char t = ustr.charAt(x);
//check after adding the number, still is a letter
// 35 is for 'Z'
if(Character.getNumericValue(t) + a < 36)
res += (char)(t + a);
else{
res += (char)(t + a - 26);
}
}
System.out.println(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.