Question regarding values of variables in Java

Discuss other programming languages besides AutoHotkey
User avatar
Scr1pter
Posts: 370
Joined: 06 Aug 2017, 08:21
Location: Germany

Question regarding values of variables in Java

12 Sep 2018, 08:47

Hi guys,

As you surely know, in AHK we can use values of variables by using %%.
Just a simple example:

Code: [Select all] [Download] GeSHi © Codebox Plus

F1::
f = 5
FileAppend, `n%Clipboard%::`n`nreturn, %A_ScriptDir%\Keylist%f%.txt
return


By using the value of variable f, I can determinate that the output file will be Keylist5.txt

Is something like this also possible in Java?
Some guy told me I would have to work with if-statements, like:

Code: [Select all] [Download] GeSHi © Codebox Plus

if (f == 4){
FileAppend, `n%Clipboard%::`n`nreturn, %A_ScriptDir%\Keylist4.txt
}

if (f == 5){
FileAppend, `n%Clipboard%::`n`nreturn, %A_ScriptDir%\Keylist5.txt
}

etc.

I know there's no FileAppend in Java, I just wanted to use the exact same script to show the difference.
If it's not possible to use %%, then you will have a lot of more code lines.

I hope that guy isn't right :)

Regards
I'm not an expert, just an enthusiastic AutoHotkey user. 8-)
Please do not support cheaters! :thumbdown:
Keyboard: Logitech G15 first generation - Mice: Logitech G9, G502, G602 - OS: Windows 7 Pro 64 Bit AHK version: 1.1.29.00
User avatar
nnnik
Posts: 3216
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Question regarding values of variables in Java

12 Sep 2018, 10:31

That guy is not right.
Java doesnt have FileAppend or command syntax.
In Java the file name is a string type.
So it would be like AHKs:

Code: [Select all] [Download] GeSHi © Codebox Plus

f := fileOpen(A_ScriptDir . "\Keylist" . f . ".txt", "w")
f.write("Whatever you want to write")
Recommends AHK Studio
User avatar
Scr1pter
Posts: 370
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Question regarding values of variables in Java

12 Sep 2018, 13:58

Thanks for your answer!
Ok, if I understood correctly:
AHK's %variable% = Java's “ . variable . “.

Viele Grüße :)
I'm not an expert, just an enthusiastic AutoHotkey user. 8-)
Please do not support cheaters! :thumbdown:
Keyboard: Logitech G15 first generation - Mice: Logitech G9, G502, G602 - OS: Windows 7 Pro 64 Bit AHK version: 1.1.29.00
User avatar
nnnik
Posts: 3216
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Question regarding values of variables in Java

12 Sep 2018, 14:20

No not at all.

Code: [Select all] [Download] GeSHi © Codebox Plus

f := fileOpen(A_ScriptDir . "\Keylist" . f . ".txt", "w")
f.write("Whatever you want to write")

^This is working AHK code

AHK's %variable% = Java's “ . variable . “.
No but you are actually close
Command's %variable% = Expression's variable
Command's normal text = Expression's "normal text"
Command's text and %variables% = Expression's "text and " . variables
Recommends AHK Studio
lexikos
Posts: 6130
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Question regarding values of variables in Java

12 Sep 2018, 17:08

In Java, you would concatenate the strings with +: "text and " + variables. AutoHotkey's concatenation operator is . (surrounded by spaces) as shown in nnnik's post, or two consecutive values with no operator but at least one space ("text and " variables).
User avatar
Scr1pter
Posts: 370
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Question regarding values of variables in Java

13 Sep 2018, 05:20

Hi guys,

Okay, here is a concrete Java example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



I would like to replace all "As" and "Bs" by mode.
What I've already tried was this:
(Basically only the 3rd line changed.)

Code: [Select all] [Download] GeSHi © Codebox Plus

SharedPreferences prefs = getActivity().getSharedPreferences("MY_SHARED_PREF", MODE_MULTI_PROCESS); // Load SharedPreferences global
mode = prefs.getString("mode", mode); // Retrieve current mode
SharedPreferences prefs+mode = getActivity().getSharedPreferences("MY_SHARED_PREF_"+mode, MODE_MULTI_PROCESS); // Load SharedPreferences of the current mode


Regarding ("MY_SHARED_PREF_"+mode, MODE_MULTI_PROCESS) there is no error message in the editor.
But it doesn't like prefs+mode.
In AHK I would use %mode% for both cases.

How to write it?
Thanks again for your support!
I'm not an expert, just an enthusiastic AutoHotkey user. 8-)
Please do not support cheaters! :thumbdown:
Keyboard: Logitech G15 first generation - Mice: Logitech G9, G502, G602 - OS: Windows 7 Pro 64 Bit AHK version: 1.1.29.00
User avatar
nnnik
Posts: 3216
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Question regarding values of variables in Java

13 Sep 2018, 05:48

Using that code should work unless Android (this is an Android question not a java question) doesn't work expected.
And here we see the answer:
https://developer.android.com/reference ... TI_PROCESS
You shouldn't use MODE_MULTI_PROCESS
Recommends AHK Studio
User avatar
Scr1pter
Posts: 370
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Question regarding values of variables in Java

13 Sep 2018, 06:03

It is Android, but since it uses Java, I thought it's a Java related question.
Android is, as far as I learned, not a programming language :)

Yes, I forgot to change it to MODE_PRIVATE, but this doesn't affect the main problem.
I think the problem is that Android/Java sees prefs+mode as two variables.
Probably it's only a small syntax issue?

Regards
I'm not an expert, just an enthusiastic AutoHotkey user. 8-)
Please do not support cheaters! :thumbdown:
Keyboard: Logitech G15 first generation - Mice: Logitech G9, G502, G602 - OS: Windows 7 Pro 64 Bit AHK version: 1.1.29.00
User avatar
nnnik
Posts: 3216
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Question regarding values of variables in Java

13 Sep 2018, 06:54

As far as I can see its syntax. But the offcial documentation tells you that the mode is buggy. And while Java is the language used it doesn't use the Android API.
The sharedPreferences API is from Android only.
Recommends AHK Studio
User avatar
Scr1pter
Posts: 370
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Question regarding values of variables in Java

13 Sep 2018, 12:03

Okay, let's just ignore the specific SharedPreferences API and focus on this:
How can I write this in Java?

Code: [Select all] [Download] GeSHi © Codebox Plus

i++
cell = P%i%


In AHK I can easily change the value of cell this way.

P.S. Can AutoHotkey be seen as an own programming language? Or is it C++? AHK C++? (Like Logitech lua)

Regards
I'm not an expert, just an enthusiastic AutoHotkey user. 8-)
Please do not support cheaters! :thumbdown:
Keyboard: Logitech G15 first generation - Mice: Logitech G9, G502, G602 - OS: Windows 7 Pro 64 Bit AHK version: 1.1.29.00
User avatar
nnnik
Posts: 3216
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Question regarding values of variables in Java

13 Sep 2018, 12:38

it would be best if you would take the step over AHKs expression syntax before translating to java.
Recommends AHK Studio
User avatar
Scr1pter
Posts: 370
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Question regarding values of variables in Java

18 Sep 2018, 05:34

Solved the issue - Topic can be closed.
I'm not an expert, just an enthusiastic AutoHotkey user. 8-)
Please do not support cheaters! :thumbdown:
Keyboard: Logitech G15 first generation - Mice: Logitech G9, G502, G602 - OS: Windows 7 Pro 64 Bit AHK version: 1.1.29.00

Return to “Other Programming Languages”

Who is online

Users browsing this forum: No registered users and 1 guest