java - Reliance on default encoding -


i'm using findbugs , error keeps generating:

reliance on default encoding:

found call method perform byte string (or string byte) conversion, , assume default platform encoding suitable. this cause application behavior vary between platforms. use alternative api , specify charset name or charset object explicitly.

i think has scanner here 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 size of stack: ");             int size=read.nextint();              mystack stack = new mystack(size);           do{              if(stack.isempty()){               system.out.println("please fill stack: (push) \nbecause stack 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 want 1)pop 2)know peek 3)know 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 want 1)pop 2)know peek 3)know 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 stack obviously, works fine.

findbugs worried default character encodings. if under windows, default character encoding "iso-8859-1". if under linux, "utf-8". , if under macos, may using "macroman". may want read more on charset encodings , find out more on available encodings in java following clicking on links.

in particular, line uses default platform encoding reading in text console:

   scanner read = new scanner(system.in); 

to make sure code works same in different environments, findbugs suggests change

   scanner read = new scanner(system.in, "utf-8"); 

(or favorite encoding). guarantee that, given input file uses encoding "utf-8", parsed in same way regardless of machine executing program in.

in case, can safely ignore warning, unless interested in feeding text files application.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -