2

I'm using FindBugs and this error keeps generating:

Reliance on default encoding:

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.

Community
  • 1
  • 1
Reem
  • 23
  • 1
  • 1
  • 3

3 Answers3

7

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);

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");

(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.

tucuxi
  • 17,561
  • 2
  • 43
  • 74
  • Thank you for your help!! I tried Scanner (System.in, "UTF-8") And it worked!! – Reem May 01 '15 at 22:10
  • If you are under Windows, your default character encoding is probably actually windows-1252. (Assuming usual, English version of Windows. Different locales obviously have different ones.) – Hakanai Jun 19 '17 at 05:47
1

FindBugs detects that your Scanner object uses default encoding to parse InputStream. Generally it is preferable to set encoding explicitly like new Scanner(someInputStreamFromFile, "UTF-8") to have the same parsing results in different environments. But not in your case, because you read System.io and there is no reliable way to detect console encoding. See details here: java console charset translation.

Community
  • 1
  • 1
mies
  • 171
  • 8
1

As you read from console, you can ignore this warning:

@SuppressFBWarnings(
        value = "DM_DEFAULT_ENCODING",
        justification = "It's fine for console reads to rely on default encoding")

This suppression should be concious though. There might be cases when we use e.g. UTF-8 console in Windows, and then we shouldn't rely on the default encoding in new Scanner(System.in);.

Community
  • 1
  • 1
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103