[英]How can I read a string from scanner, and convert each letter to a different value?
I am working on taking a string that has been imported as a file, taking each character, and "encrypting" it by changing it to the next letter. 我正在研究一个已作为文件导入的字符串,采用每个字符,并通过将其更改为下一个字母来对其进行“加密”。 Basically, a is b, b is c, c is d, and etc. It isn't secure but it isn't meant to be for these purposes.
基本上,a是b,b是c,c是d,依此类推。它不是安全的,但并非出于这些目的。 The scanned string can be anything but I'm using "Orange juice is great! I drank 83,214 cups of it yesterday."
扫描的字符串可以是任何东西,但我使用的是“橙汁很棒!昨天我喝了83214杯。” It is in a file called input.txt.
它位于名为input.txt的文件中。
My code is as follows: 我的代码如下:
import java.util.Scanner;
import java.io.*;
public class ReadFileExample{
public static void main(String[] args){
//String fileName = "input.txt";
//File file = new File(fileName);
//Scanner in = new Scanner(file);
try{
Scanner in = new Scanner(new File("input.txt"));
while(in.hasNext() == true){
String input = in.next();
System.out.println(input);
}
}
in.close();
}
catch(FileNotFoundException exception){
System.out.println("Could not find the file.");
}
}
}
I thought about converting the string to an array but I'm not sure how to go about that. 我曾考虑过将字符串转换为数组,但不确定如何去做。 Maybe like this?
也许是这样吗?
char[] charArray = input.toCharArray();
for (char c : charArray){
System.out.println(c);
}
I'm really not sure where to go with this. 我真的不知道该去哪里。 I also wondered about for(i = 0; i < string.length; i++) Not sure how to go about that either.
我也想知道for(i = 0; i <string.length; i ++)也不知道该怎么做。 Once it is done, I need to print it out.
完成后,我需要将其打印出来。 Then, I need to take that same string, encrypt it again.
然后,我需要使用相同的字符串,然后再次对其进行加密。
The second file encryption technique is to replace each letter with the place it is in the alphabet. 第二种文件加密技术是将每个字母替换为字母中的位置。 We will use two digits for every letter.
每个字母我们将使用两位数字。 For example, the letter 'a' is 01, the letter 'b' is 02, the letter 'c' is 03, the letter 'z' is 26. So that we can use capital letters we start 'A' with 27, 'B' with 28, 'C' with 29, and so on.
例如,字母“ a”为01,字母“ b”为02,字母“ c”为03,字母“ z”为26。因此我们可以使用大写字母,以27开头的“ A”, 'B'代表28,'C'代表29,依此类推。
For numbers, we convert each digit into two letters: the first two letters that they stand for. 对于数字,我们将每个数字转换为两个字母:它们代表的前两个字母。 The first letter is capitalized followed by a lower-case letter.
首字母大写,后跟小写字母。 For example, 8 written out is eight.
例如,写出的8是8。 So, 8 would be encrypted to “Ei”, 1 would be encrypted to “On”, 2 would be encrypted to “Tw”, 3 would be “Th” and so on.
因此,将8加密为“ Ei”,将1加密为“ On”,将2加密为“ Tw”,将3加密为“ Th”,依此类推。
Note that “10” is actually two digits, so it would be encrypted as “1” and “0”: “OnZe” 请注意,“ 10”实际上是两位数字,因此将被加密为“ 1”和“ 0”:“ OnZe”
If you come to a non-letter character (spaces, punctuation, numbers, etc.), just print them as is without encrypting. 如果您遇到非字母字符(空格,标点符号,数字等),只需照原样打印而不进行加密。
Any help is appreciated even if it is suggesting what to search for to answer my questions. 任何帮助都将不胜感激,即使它正在建议寻找什么来回答我的问题。
Here is the below code, the code is explained in the comments, if you need more help then comment below, the demo can be found at the below link! 这是下面的代码,代码在注释中进行了说明,如果您需要更多帮助,请在下面的注释中进行演示,可以在下面的链接中找到演示! I have given the demo for the static string that you have given the example for.
我已经给出了示例的静态字符串的演示。
import java.util.HashMap;
/**
*
* @author Sai-Karan
*/
public class App {
//Function to return the substring for the second encoding method
//Example 1 will return On, and 2 will return Tw ....etc
public static String findstringmap(char c)
{
//Create a hashmap and add the key elements and the string definitions
HashMap <Integer, String> hmap = new HashMap <Integer, String>();
/*Adding elements to HashMap*/
hmap.put(1, "One");
hmap.put(2, "Two");
hmap.put(3, "Three");
hmap.put(4, "Four");
hmap.put(5, "Five");
hmap.put(6, "Six");
hmap.put(7, "Seven");
hmap.put(8, "Eight");
hmap.put(9, "Nine");
hmap.put(0, "Zero");
/* Get values based on key*/
int num_value = Character.getNumericValue(c);
//convert to the numeric value
String var = hmap.get(num_value);
//get the substring
String ret_str = var.substring(0, 2);
return ret_str;
}
public static void main(String[] args) {
// TODO code application logic here
String l1 = "Orange juice is great! I drank 83,214 cups of it yesterday.";
//String final contains the result
String Final = "";
String next = "";
for(int i =0 ; i< l1.length(); i++)
{
//char c = l1.charAt(i);
if (l1.charAt(i) == ' ')
{
next = " ";
}
else if (l1.charAt(i) == '!')
{
next = "!";
}
else if(l1.charAt(i) == ',')
{
next = ",";
}
else if(l1.charAt(i) == '.')
{
next = ".";
}
else if(!(Character.isDigit(l1.charAt(i))))
{
int val = l1.charAt(i);
next = String.valueOf( (char) (val + 1));
// System.out.println(next);
}
else if(Character.isDigit(l1.charAt(i)))
{
next = findstringmap(l1.charAt(i));
}
Final = Final + next;
}
System.out.println(Final);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.