![](/img/trans.png)
[英]How can I convert each integer/double value to String from JSON data containing nested objects and arrays?
[英]How can I read a string from scanner, and convert each letter to a different value?
我正在研究一個已作為文件導入的字符串,采用每個字符,並通過將其更改為下一個字母來對其進行“加密”。 基本上,a是b,b是c,c是d,依此類推。它不是安全的,但並非出於這些目的。 掃描的字符串可以是任何東西,但我使用的是“橙汁很棒!昨天我喝了83214杯。” 它位於名為input.txt的文件中。
我的代碼如下:
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.");
}
}
}
我曾考慮過將字符串轉換為數組,但不確定如何去做。 也許是這樣嗎?
char[] charArray = input.toCharArray();
for (char c : charArray){
System.out.println(c);
}
我真的不知道該去哪里。 我也想知道for(i = 0; i <string.length; i ++)也不知道該怎么做。 完成后,我需要將其打印出來。 然后,我需要使用相同的字符串,然后再次對其進行加密。
第二種文件加密技術是將每個字母替換為字母中的位置。 每個字母我們將使用兩位數字。 例如,字母“ a”為01,字母“ b”為02,字母“ c”為03,字母“ z”為26。因此我們可以使用大寫字母,以27開頭的“ A”, 'B'代表28,'C'代表29,依此類推。
對於數字,我們將每個數字轉換為兩個字母:它們代表的前兩個字母。 首字母大寫,后跟小寫字母。 例如,寫出的8是8。 因此,將8加密為“ Ei”,將1加密為“ On”,將2加密為“ Tw”,將3加密為“ Th”,依此類推。
請注意,“ 10”實際上是兩位數字,因此將被加密為“ 1”和“ 0”:“ OnZe”
如果您遇到非字母字符(空格,標點符號,數字等),只需照原樣打印而不進行加密。
任何幫助都將不勝感激,即使它正在建議尋找什么來回答我的問題。
這是下面的代碼,代碼在注釋中進行了說明,如果您需要更多幫助,請在下面的注釋中進行演示,可以在下面的鏈接中找到演示! 我已經給出了示例的靜態字符串的演示。
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.