1
I have a String = "1" and I can’t convert it to int, I think it might have something to do with the way it was built, the class that reads the TXT file from the web and saves it in the database is below:
public class Downloader {
public Downloader() {
}
public ArrayList<String> baixarTxt(URL url, String fileName) throws IOException {
StringBuffer datax = new StringBuffer();
InputStream is = url.openStream();
//InputStreamReader isr = new InputStreamReader(is, "windows-1250");
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
String readString = br.readLine();
ArrayList<String> arrayList = new ArrayList<>();
while(readString != null){
datax.append(readString);
arrayList.add(readString);
readString = br.readLine();
}
isr.close();
return arrayList;
}
public void salvarNoBanco(ArrayList<String> list, Context context){
VendedorDB venDB = new VendedorDB(context);
venDB.recriarTblVendedor();
for(int i = 0; i < list.size(); i++){
String linha = list.get(i);
String[] dadosLinha = linha.split(";");
Vendedor v = new Vendedor();
v.setId(Integer.parseInt(dadosLinha[0])); //#### <- O ERRO OCORRE AQUI
v.setNome(dadosLinha[1]);
v.setSenha(dadosLinha[2]);
v.setMeta(Integer.parseInt(dadosLinha[3]));
v.setVlrAtual(Integer.parseInt(dadosLinha[4]));
venDB.inserir(v);
}
}
}
The Error message is as follows:
04-03 20:47:32.102 7800-7800/com.example.w190227 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.w190227, PID: 7800
java.lang.NumberFormatException: Invalid int: "1"
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parse(Integer.java:374)
at java.lang.Integer.parseInt(Integer.java:365)
at java.lang.Integer.parseInt(Integer.java:331)
at com.example.w190227.util.db.Downloader.salvarNoBanco(Downloader.java:61)
at com.example.w190227.fragment.HomeFragment$DownloaderAsyncTask.onPostExecute(HomeFragment.java:148)
at com.example.w190227.fragment.HomeFragment$DownloaderAsyncTask.onPostExecute(HomeFragment.java:126)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
The downloadTxt() method is called in doInBackground() from an Asynctask and saveNoBanco() is called in onPostExecute(), follows the code below:
public class DownloaderAsyncTask extends AsyncTask<URL, Void, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(URL... urls) {
ArrayList<String> result;
try {
Downloader download = new Downloader();
URL url = urls[0];
result = download.baixarTxt(url, "grupos");
}catch(Exception e){
Log.d("LOG", "ERRO: "+e);
result = new ArrayList<>();
result.add("ERRO no arraylist");
}
return result;
}
@Override
protected void onPostExecute(ArrayList<String> s) {
Log.d("LOG", "Baixou: "+s);
Toast.makeText(getActivity(), "Baixou: "+s.get(1), Toast.LENGTH_SHORT).show();
Downloader download = new Downloader();
download.salvarNoBanco(s, getActivity());
}
}
And the text file that is downloaded from the web is below:
1;pablo;wt2009;10000;10000
2;waltair;wt2009;12000;12000
3;bruno;wt2009;5000;5000
The dataLine[0] as string is returning you what?
– Murillo Comino
is returning "1" without any special character before or after
– Pablo Henrique Correa
Try to use my answer, if it doesn’t work warn, and if it does
– Murillo Comino
however when I downloaded the file with Inputstreamreader charset "windows-1250" appeared a strange symbol before the 1 but downloading with UTF-8 the symbol disappeared. I believe it is something from the charset because I typed this TXT file in the notepad and I certainly did not type anything before 1 but something tells me that this hidden character may be the cause of the problem.
– Pablo Henrique Correa
Anything takes a look Regex - Unicode
– Murillo Comino