• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Array of Maps possible?  RSS feed

 
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I wrote a Java program that sends raw Infrared remote control codes to an Arduino over the serial port ... I have the Raw codes defined in arrays like this:



As you can see, strCommands and allCommands have to line up properly. Then I just match the string sent to my method against strCommands, and use the index from that String array and call the command out of allCommands using that index.

What I would like to do is somehow create a single array and be able to take the string command sent to the method and somehow use it to pull the raw command directly from the allCommands array. That way it does not matter what order I place the commands into the array ... here is some pseudo code to illustrate my desire:



Or SOMETHING like that ... all I can think of is to somehow use an array of Maps, but I'm not sure if that's possible, or if it is, how I can go about doing it.... any ideas?

Thanks,

Mike
 
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael D Sims wrote:


You would only need a single Map.

I'm not clear as to why you defined 'allCommands' as an array of arrays instead of a single array.
 
Michael D Sims
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:
I'm not clear as to why you defined 'allCommands' as an array of arrays instead of a single array.



I will illustrate how I use it ... maybe it will make sense:

 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. How about this?
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will these numbers be modified at any time during the running of the program? If not, then they could be declared "static", in other words, a constant.

 
Michael D Sims
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:
You would only need a single Map.


Thank you Carey ... I must have had a brain fart ... this worked perfectly!

Mike

 
Michael D Sims
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Ok. How about this?



This is what I ended up doing ... I defined the codes in a separate class (because there are a lot of codes and it just looks cleaner that way) ...



Then, in my Controller class, I use it like this:



sp of course is the SerialPort that I'm connected to.

This final code is SO MUCH simpler than what I had before messing with that two dim array and a few other things I was able to consolidate or eliminate ... I basically cut the code in half.



Mike
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent!

BTW, this
should be written like
 
Michael D Sims
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, I need to find a Java library that converts PRONTO hex codes to Raw so I can bring more needed commands into my program.  Using Java to store these codes saved my bacon because my original Arduino code which had all these arrays in it became too large to use.
 
Michael D Sims
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Excellent!

BTW, this
should be written like


Interesting, it seems to compile either way ... what's the difference?

Mike
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael D Sims wrote:

Carey Brown wrote:Excellent!

BTW, this
should be written like


Interesting, it seems to compile either way ... what's the difference?

Mike


Yes, it works either way. This was an unfortunate compiler design issue introduced when they expected developers to be migrating from C++. The first style is C++ (but still compiles), and the second is accepted Java convention.
 
Michael D Sims
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, here is what the GUI looks like ...

 
Michael D Sims
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Yes, it works either way. This was an unfortunate compiler design issue introduced when they expected developers to be migrating from C++. The first style is C++ (but still compiles), and the second is accepted Java convention.



So it literally works exactly the same either way? There is no differentiation in the compiler? No different way of using memory or anything like that?

Mike
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Impressive. A DVR controller?
 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael D Sims wrote:So it literally works exactly the same either way? There is no differentiation in the compiler? No different way of using memory or anything like that?


Nope, though it is unclear what happens when you try to declare more than one variable, which is now frowned upon anyway.
 
Michael D Sims
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Nope, though it is unclear what happens when you try to declare more than one variable, which is now frowned upon anyway.



Now THAT makes sense ... what would you do in C++? Something like this?



Did you see the UI that I designed? Posted it above ^^^

Mike
 
Michael D Sims
Ranch Hand
Posts: 139
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Impressive. A DVR controller?


OH, I guess you did see it ...

It controls a cable box DVR, yes ... and some TV functions ... I want to get this working on a WiFi Arduino and also have it translate unused buttons from my moms remote, so she can access the apps on her tv like Netflix and Plex as well as the cable box with one remote so that it's simple and seamless for her... it will also act as a repeater because she has to bend funky to make the remote work and shes getting really old...



Mike
 
Saloon Keeper
Posts: 9138
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I'd use a resource file to hold large amounts of configuration data. That way it doesn't clutter up your code add you can format it in a custom way:
 
Sheriff
Posts: 12199
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael D Sims wrote:By the way, here is what the GUI looks like ...


Is that look and feel approved by the Branding folks? They are pretty picky about sticking to approved branding, including look and feel. (I worked there for 11 years up until December last year).

A couple of style issues:

1. Why do all your array variable names start with "int"? That just creates lots of noise. We don't use Hungarian notation in Java. Plus, Hungarian notation wasn't meant to be used like that as far as I know. If you drop the "int" wart on all those names, I think your code would be even clearer.  Or you could do what Stephan suggested and use a resource file instead.

2. And int[] is just as much an object as an Integer[] since they are both arrays. This code works:

 
Carey Brown
Bartender
Posts: 4532
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,

I agree with the suggestion that you should put your command codes into a config file of some sort.

I find your project interesting. Are you being guided by some article on how to interface to the DVR and remote? Is there an overall description of what you're attempting to do?

Carey
 
Junilu Lacar
Sheriff
Posts: 12199
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:


You don't normally see this but it's valid nonetheless.  It means that var1 is a Integer and var2 is an Integer[].
 
Junilu Lacar
Sheriff
Posts: 12199
199
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that Java is very liberal about the placement and formatting of brackets in array declarations.

This

is the same as
and is the same as each of these:


The variations are arguably more confusing to read than the first one which is what people usually expect to see.
 
Bartender
Posts: 1845
46
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see a lot of hard coding in the above program. If you want to add another command it will be a code change. Is it possible for you to use a json config file :



In this way, if you want to add more commands, you simply have to add more configs to your json file without doing any code changes.
The only issue is that java does not have a json parser internally, so, you'll have to use a third party jar..
Alternatively, you can store the above changes in xml. There are internal libraries in java with xml support.

 
Stephan van Hulst
Saloon Keeper
Posts: 9138
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think XML or JSON is overdoing it, you don't need that kind of structure. A simple .properties file will do.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!