Android Question Case/Select or If/Else ? = same Problem

anOparator

Active Member
Licensed User
Longtime User
Can you point out what in the B4A is wrong with my logic?
The third conditioin always closes the app with, ** Activity (main) Pause, UserClosed = true **

Just trying to write text files into EditText2.
TIA for sharing.

----------------
B4X:
Sub EditText1_EnterPressed
If EditText1.Text = "" Then
    EditText2.Text = File.ReadString(File.DirRootExternal, "blank.txt")    'THIS WORKS
    ToastMessageShow("No file name was in text box.", True)
Else If EditText1.Text <> EditText1.Text & ".txt" Then
  EditText2.Text = File.ReadString(File.DirRootExternal, EditText1.text & ".txt")  ' THIS WORKS
    ToastMessageShow(" EditText1 had a valid name in it" & ".", True)      ' writes a file named aa.txt or bb.txt
                              ' in EditText2
Else
      EditText2.Text = File.ReadString(File.DirRootExternal, "zz.txt")    ' THIS FAILS
ToastMessageShow(" PLEASE CHECK THE SPELLING OF THE WORD", True)  
End If
End Sub
B4X:
Sub EditText1_EnterPressed
Select EditText1.Text = EditText1.Text

Case EditText2.Text <> File.ReadString(File.DirRootExternal,  EditText1.text & ".txt")    ' THIS WORKS, finds the File
    EditText2.Text = File.ReadString(File.DirRootExternal, EditText1.text & ".txt")          ' writes a file named aa.txt or bb.txt
    ToastMessageShow(" WE'RE ON THE SAME PAGE", True)                                      ' in EditText2


Case EditText1.Text = ""                                                                  ' THIS WORKS
    EditText2.Text = File.ReadString(File.DirRootExternal, "zz.txt")
    ToastMessageShow("No file name was in text box.", True)

Case Else
    EditText2.Text = File.ReadString(File.DirRootExternal,  "zz.txt")                      ' THIS FAILS
  
    ToastMessageShow(" PLEASE REVIEW ALL ASPECTS IN THE SPELLING", True)
End Select
End Sub
B4X:
Sub EditText1_EnterPressed
If EditText1.Text = "" Then
            EditText2.Text = File.ReadString(File.DirRootExternal, "zz.txt")            ' THIS WORKS
        ToastMessageShow("No file name was in text box.", True)
Return
End If

If EditText1.Text <> EditText1.Text & ".txt" Then                                        ' THIS WORKS
    EditText2.Text = File.ReadString(File.DirRootExternal, EditText1.text & ".txt")        ' writes q file named aa.txt or bb.txt
        ToastMessageShow(" EditText1 had a valid name in it" & ".", True)                ' in EditText2

Else
            EditText2.Text = File.ReadString(File.DirRootExternal, "zz.txt")            ' THIS FAILS
ToastMessageShow(" PLEASE CHECK THE SPELLING OF THE WORD", True)  
Return
End If
End Sub
 

Beja

Expert
Licensed User
Longtime User
Is the edittext property single line or multi-line?

It would be much easier if you uploaded the project as zip file.
File-Export As Zip
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In the If / Then code it's normal that the last case will never be executed !
In this line :
Else If EditText1.Text <> EditText1.Text & ".txt" Then
EditText1.Text is ALLWAYS different than EditText1.Text & ".txt" !
So the Else case will never be executed.

I cannot say why it doesn't work in the Select / Case test because I don't know the content of
File.ReadString(File.DirRootExternal, EditText1.text & ".txt")
By the way the tests in If / Then are not the same as in Select / Case.

You could replace
Select EditText1.Text = EditText1.Text
by
Select True
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
imho it makes no difference whether the edittext property single line or multi. they happen to be single.
-----------------
the content of
File.ReadString(File.DirRootExternal, EditText1.text & ".txt") is either an empty EditText1, or EditText1 with the name of an existing .txt file, or the name of a file that doesn't exist (or could be a valid file name with a typo).

Thanks for the feedbback, I've attached attached the project files, and am about to start over from scratch
 

Attachments

  • test If.zip
    7.7 KB · Views: 194
Upvote 0

Beja

Expert
Licensed User
Longtime User
Everything is working fine, except:
"'Invalid file name in EditText1 closes the app"
The above msg is not closing the app unless you chose NO, (Continue yes or no)
Also if edittext1 didn't contain a valid file name, you get the msg: File Not Found.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would do it like this:
B4X:
If EditText1.Text = "" Then   
    EditText2.Text = File.ReadString(File.DirRootExternal, "zz.txt") 
    ToastMessageShow("No file name was in text box.", True)
Else If EditText1.Text.EndsWith(".txt") AND File.Exists(File.DirRootExternal, EditText1.Text)  Then 
    EditText2.Text = File.ReadString(File.DirRootExternal, EditText1.Text)       
    ToastMessageShow(" EditText1 had a valid name in it.", True)       
Else If File.Exists(File.DirRootExternal, EditText1.Text & ".txt") Then   
    EditText2.Text = File.ReadString(File.DirRootExternal, EditText1.Text & ".txt")       
    ToastMessageShow(" EditText1 had a valid name in it without the suffix.", True)       
Else
    EditText2.Text = File.ReadString(File.DirRootExternal, "zz.txt")  
    ToastMessageShow(" PLEASE CHECK THE SPELLING OF THE WORD", True)       
End If
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
Thanks for the lessons in logic. There was no need for using the <> operator , just use If File.Exist and let Else handle the Not ( ).
B4A is basic and complex, and the Online Community is really great.
 
Upvote 0
Top