【问题标题】:Java Input/Output program that works with files处理文件的 Java 输入/输出程序
【发布时间】:2016-10-18 13:38:01
【问题描述】:

这是一个从文件中读取输入的代码

日期.in.txt

然后通过在从文件中读取的文本中的第 k 个字符和来自密钥的 (k%n) 字符之间执行 XOR 操作来加密,其中是密钥长度。然后,它输出到

date.out.txt

在第二部分,它做相反的事情,从

date.out.txt

并输出到

日期.in.txt

这是第一部分的示例:

date.in:世界你好!你好吗?

date.out: 43 4 5 2 10 67 54 6 28 9 7 64 73 100 45 12 22 73 15 23 6 65 16 1 16 92

这是一个相反的:

date.out.txt: 43 4 5 2 10 67 54 6 28 9 7 64 73 100 45 12 22 73 15 23 6 65 16 1 16 92

date.in.txt:世界你好!你好吗?

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws FileNotFoundException 
        {Scanner scan=new Scanner(System.in);
        
        
        File file = new File("date.in.txt");
        File file2 = new File("date.out.txt");
        
        
        String [] s= new String[100];
        String S= new String();
        for(int i=0;i<100;i++)
            s[i]=new String();
        int nr=0;
        
        
        int vec[]=new int[100];
        int nrvec=0;
        
        
        String cuv;
        System.out.print("Enter key: ");
        cuv=scan.nextLine();
        System.out.print("\n");
        int n=cuv.length();
        
        
        try 
            { Scanner input = new Scanner( file );
                while(input.hasNextLine())
                    { s[nr++]=input.nextLine();
                    s[nr++]="\n";} }
        
        catch (FileNotFoundException ex) 
            { System.out.printf("ERROR: %s!\n", ex); }
        
        
        for(int i=0;i<nr;i++)
            for(int j=0;j<s[i].length();j++)
                { vec[nrvec++]=s[i].codePointAt(j); }
        
        
        for(int i=0;i<nrvec;i++)
            { int d=cuv.codePointAt(i%n);
            vec[i]=vec[i] ^ d; }
            
        
        nrvec--;
      
        
         try 
            { PrintWriter output = new PrintWriter(file2);
            for(int i=0;i<nrvec;i++)
                output.print(vec[i]+" ");
            output.close(); } 
    
         catch (IOException ex) 
            { System.out.printf("ERROR: %s!\n", ex); }
        
        
         int ok=1; // SECOND PART
         if(ok==1)
            {System.out.print("Enter key: ");
            cuv=scan.nextLine();
            System.out.print("\n");
            n=cuv.length();
            
            File file3 = new File("date.out.txt");
            File file4 = new File("date.in.txt");
            
            nrvec=0;
            
            try 
                { Scanner input = new Scanner( file3 );
                    while(input.hasNext())
                        { vec[nrvec++]=input.nextInt(); } }
                        
            catch (FileNotFoundException ex) 
                { System.out.printf("ERROR: %s!\n", ex); }
         
            int e;
            try 
                { PrintWriter output = new PrintWriter(file4);
                for(int i=0;i<nrvec;i++)
                    { e=cuv.codePointAt(i%n);
                    output.print(vec[i]^e +" ");}  // 93
                output.close(); } 
    
            catch (IOException ex) 
                { System.out.printf("ERROR: %s!\n", ex); }
         
        
            }}}
                

问题是我在第 93 行收到一个错误,上面写着:

线程“main”java.lang.Error 中的异常:未解决的编译 问题:对于参数类型 int,运算符 ^ 未定义, 字符串

在 Main.main(Main.java:93)

我不知道为什么会这样。

【问题讨论】:

    标签: java file oop input output


    【解决方案1】:

    把那一行写成

     output.print((vec[i]^e) +" ");}  // 93
    

    注意括号。没有它们,原始代码实际上被视为

     output.print(vec[i]^ (e +" "));}  // 93
    

    因为“+”运算符的优先级高于“^”。

    【讨论】:

      【解决方案2】:

      只需将 93 处的行改为:

      输出.print((vec[i]^e) +" ");}

      你遇到的问题是优先级。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-31
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        • 2011-01-19
        • 2019-06-08
        • 2022-01-02
        相关资源
        最近更新 更多