Question regarding values of variables in Java

Discuss other programming languages besides AutoHotkey
User avatar
Scr1pter
Posts: 1271
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

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

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
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
nnnik
Posts: 4500
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

f := fileOpen(A_ScriptDir . "\Keylist" . f . ".txt", "w")
f.write("Whatever you want to write")
Recommends AHK Studio
User avatar
Scr1pter
Posts: 1271
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 :)
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
nnnik
Posts: 4500
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

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: 9494
Joined: 30 Sep 2013, 04:07
Contact:

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: 1271
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

SharedPreferences prefs = getActivity().getSharedPreferences("MY_SHARED_PREF", MODE_MULTI_PROCESS); // Load SharedPreferences global
mode = prefs.getString("mode", mode); // Retrieve current mode
if (mode.equals("A"))
{
    SharedPreferences prefsA = getActivity().getSharedPreferences("MY_SHARED_PREF_A", MODE_MULTI_PROCESS); // Load SharedPreferences A
    .
    .
    .
}
if (mode.equals("B"))
{
    SharedPreferences prefsB = getActivity().getSharedPreferences("MY_SHARED_PREF_B", MODE_MULTI_PROCESS); // Load SharedPreferences B
    .
    .
    .
}
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

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!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
nnnik
Posts: 4500
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: 1271
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
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
nnnik
Posts: 4500
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: 1271
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

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
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
User avatar
nnnik
Posts: 4500
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: 1271
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.
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Question regarding values of variables in Java

24 Feb 2019, 01:23

@Scr1pter
please, let us not die ignorant (Lass uns bitte nicht dumm sterben ! )
please, give us the solution
thanks
Hubert
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Question regarding values of variables in Java

24 Feb 2019, 05:33

You cant reference variables dynamically in java.
Using maps or similar will work though.
Recommends AHK Studio

Return to “Other Programming Languages”

Who is online

Users browsing this forum: No registered users and 23 guests