依赖默认编码 [英] Reliance on default encoding

查看:109
本文介绍了依赖默认编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FindBugs,并且此错误不断生成:

I'm using FindBugs and this error keeps generating:

依赖默认编码:

找到了对方法的调用,该方法将执行字节到字符串(或字符串到字节)的转换,并假定默认平台编码是合适的.这将导致应用程序行为在平台之间有所不同.使用备用API并明确指定字符集名称或字符集对象.

Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behavior to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.

我认为这与扫描仪有关,这是我的代码:

I think it has something to do with the Scanner here is my code:

package mystack;

 import java.util.*;
  
  public class MyStack {
   
    private int maxSize;
    private int[] stackArray;
    private int top;
    
    public MyStack(int s) {
       maxSize = s;
       stackArray = new int[maxSize];
       top = -1;
    }
    public void push(int j) {
       stackArray[++top] = j;
    }
    public int pop() {
       return stackArray[top--];
    }
    public int peek() {
       return stackArray[top];
    }
    public int min() {
       return stackArray[0];
    }
    public boolean isEmpty() {
       return (top == -1);
    }
    public boolean isFull() {
       return (top == maxSize - 1);
    }
     
    
     public static void main(String[] args) {
         
       Scanner read = new Scanner(System.in);

         char end;
         
         System.out.println("Please enter the size of the Stack: ");
            int size=read.nextInt();
        
            MyStack stack = new MyStack(size);
         
         do{
         
            if(stack.isEmpty()){
              System.out.println("Please fill the Stack: (PUSH) \nBecause Stack is Empty.");
                 int add;
                 for(int i=0; i<size; i++)
                 {add=read.nextInt();
                   stack.push(add);}
                      }//End of if
        
          else if(stack.isFull()){
              System.out.println("Do you want to 1)POP 2)Know the Peek 3)Know the Min");
               int option=read.nextInt();
                if(option==1) 
                 stack.pop();
                
                else if (option==2)
                 System.out.println("The Peek= "+stack.peek());
                
                else if (option==3)
                 System.out.println("The Min= "+stack.min());
                
                else System.out.println("Error, Choose 1 or 2 or 3");
                      }//End of if
         else 
            { System.out.println("Do you want to 1)POP 2)Know the Peek 3)Know the Min 4)PUSH");
               int option=read.nextInt();
                 
                 if(option==1)
                     stack.pop();
                
                 else if (option==2)
                  System.out.println("The Peek= "+stack.peek());
                 
                 else if (option==3)
                  System.out.println("The Min= "+stack.min());
               
                 else if(option==4)
                   {int add=read.nextInt();
                    stack.push(add);}
                   }//end else
        
         System.out.print("Stack= ");
          for(int i=0; i<=stack.top; i++)
                 { System.out.print(stack.stackArray[i]+" ");}
         
         System.out.println();
         System.out.println();
         
         System.out.println("Repeat? (e=exit)");
          end=read.next().charAt(0);
          
         System.out.println();
         }while(end!='e');   
    System.out.println("End Of Program");
    }//end main

    }//end MyStack

显然是一个堆栈,工作正常.

It is a stack obviously, works fine.

推荐答案

FindBugs担心默认字符编码.如果您使用Windows,则默认字符编码可能是"ISO-8859-1".如果您使用的是Linux,则可能是"UTF-8".如果您使用的是MacOS,则可能使用的是"MacRoman".您可能需要阅读有关字符集编码的更多信息,以及有关可用的Java编码.

FindBugs is worried about default character encodings. If you are under Windows, your default character encoding is probably "ISO-8859-1". If you are under Linux, it is probably "UTF-8". And if you are under MacOS, you may be using "MacRoman". You may want to read more on charset encodings and find out more on available encodings in Java by following clicking on the links.

特别是,此行使用默认平台编码从控制台读取文本:

In particular, this line uses the default platform encoding for reading in text from the console:

   Scanner read = new Scanner(System.in);

为确保代码在不同的环境下均能正常工作,FindBugs建议您将其更改为

To make sure that the code works the same in different environments, FindBugs suggests that you change this to

   Scanner read = new Scanner(System.in, "UTF-8");

(或您喜欢的编码).这样可以保证,在给定使用"UTF-8"编码的输入文件的情况下,无论在什么计算机上执行程序,都将以相同的方式对其进行解析.

(or your favorite encoding). This would guarantee that, given an input file that uses encoding "UTF-8", it will be parsed in the same way regardless of what machine you are executing your program in.

对于您来说,可以放心地忽略此警告,除非您有兴趣将文本文件输入到应用程序中.

In your case, you can safely ignore this warning, unless you are interested in feeding text files into your application.

这篇关于依赖默认编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆