Ben Alex Barreto

Greenhorn
+ Follow
since May 10, 2011
Ben Alex likes ...
Mac Objective C Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ben Alex Barreto

I know this is probably just test code, but nonetheless, let me tell ya :P Adding a Throws declaration to the main method is terrible idea, because if it does throw an IO exception, it will be sent to the JVM itself, and you will never be able to handle it.
11 years ago
If you have a macbook air, do you use it for development? (iOS, Android) How does it handle it?

I've seen a lot of people using them, but I remember my first experience with one of the first ones that came out, and it was super slow. At work, though, some people seem to like it, so I've been wondering how much better the air must have gotten.
11 years ago
Mike, you nailed it dead on. Thanks you so much! You're the man!
11 years ago
Hi everyone,

So, a little background on the situation, I wrote something the other day to call a few powershell scripts from a java application, and ended up stripping out all the commands from the powershell script and calling them from java, which made the who process A LOT faster. What would take about 2 hours is now done in about 15 minutes. Anyway, a co-worker saw the code and asked me to compile it and if possible, give him a jar that he would run on his computer to do the same thing (for all my intents and purposes, I was just running it from eclipse).

I began writing a little menu for it, so that he can edit some settings, view them, and then start the process. While I was writing the menu and running some tests on it, I realized I was getting a very annoying bug when handling InputMismatchException from the scanner. I catch the exception, but then when it loops through the options again, the System.out.println I used in the catch block is printed again (like the exception is being thrown again) and it gets into an infinite loop, since the InputMismatchException is thrown over and over again. The pause you see in the catch block is just so I could stop the process in time to see what was going on, otherwise it would print things to the console way too fast.

This is such noobish question/issue, but I was hoping someone could give me some light I'm not a developer (I wish! haha) but I do write some java every now and then when I see it could automate certain things and make our job easier. Anyway, I hope you guys can help! In advance, thank you!!

Menu that's giving me grief (ps. this is not done at all! I kinda stopped in the middle of it, because I am still trying to figure out this exception handling issue).


Settings class, just in case you would like to run the code

11 years ago
So I've been trying to learn Objective-C, and yesterday I was looking at how inheritance works.

I created two classes, Person and Employee. The later is supposed to inherit from Person. The problem is that I'm getting this error "No visible @inerface for Employee" when I try to instantiate an Employee and set its weightInKilos or heightInMeters. Here are the header and implementation files:






And here is the main


I'm just trying to learn. What am I doing wrong?
11 years ago
iOS

Przemek Boryka wrote:Hi

Why don't you using ListView with ListAdapter which is pumped with data from database? To get any row - setOnItemClickListener(listener) ? Below is example :

Your Activity can implement OnItemClickListener interface.

... // onCreate()
ListView listView = new ListView(context);
listView.setOnItemClickListener(context);
...


... //your method that set listview
String[] data = new String[cursor.getCount()];
int index = 0;
...


while (cursor.moveToNext()) { // cursor is to get data from database
data[index++] = cursor.getString(0); // you get the string value from column number 0 from resultset
}
...

ArrayAdapter<String> myList = new ArrayAdapter<String>(context, R.id.your_defined_row, data); // setup ListView with data

listView.setAdapter(myList); // set listview by invoking setAdapter method with myList which contain view of row and data.
listView.invalidate(); // rebuild listview

...
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) { // to get any row of list

}
...

Bye



That's such a cool idea! I'm definitely going to give it a try and see how it goes! Meanwhile, let me show you guys what I had in mind previously. It's not as elegant as your solution, so please don't laugh :P

After getting data from the server, the textviews would be created dynamically according to however many entries we got, similar to the example I posted earlier. They are populated with text as they are created, since we would have, let's say, an ArrayList of entries already saved (by the way, this is the other part of this project. We are also implementing a server using hibernate to talk to our DBMS).

This is how we could assign listeners dynamically. This is what me and my team used a couple weeks ago for a different project.


But there is a problem To use reflection as we did, the elements we are assigning listeners to must have an id, however, by creating the textviews as we did, how can we assign an id to each of the textviews as they are created? Because they must be named ListenerClass_element in order for the right listener to be instantiated and then assigned. Also, when a textview is selected for the second time, it should be resized, so I gotta have a reference to each textview in order to get their dimensions and handle the event accordingly.

What do you think?

-Ben
12 years ago
So far I've been thinking about doing something similar to this example:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ScrollView2.html

And for the listeners, I was thinking about using reflection, which would then allow us to create just one listener, and assign it to however-many textviews we create.



Would this work?

-Ben
12 years ago
Me and my group are trying to come up with a good implementation for this week's case study. We will be querying data from a database, we need to have a list of dates that must be dynamic, and by that I mean, you gotta be able to click on a date and then see what's in the database according to that date.

Well, I figured we could create TextViews dynamically, using a loop, but then, how do you assign the listeners to them? Do you have a pre-written list of listeners or would we have to implement them using reflection? I know we can set the textviews to clickable and then assign a listener to them.

Well, if anyone has any suggestions, I are listening :P so far, we are just trying to figure out how to handle the listeners. Another questions, however, how do I update the view with the new elements without having previously created them in the XML file? Is that even possible?

Please excuse my ignorance :P I'm still getting familiar with the android environment.

-Ben
12 years ago
There are several things that could be causing that. Can we see your code? Make sure you set the right permissions in the manifest, such as internet access for example.
12 years ago

Hussein Baghdadi wrote:Make sure the resulted class name is qualified with its package name. For example: com.yourdomain.CalculateListener



That was exactly right! Thank you o much! We are going past that exception now :-)
12 years ago
Me and my team are trying to use reflection to implement the listeners for the buttons in this simple calculator. We are doing this for an engineering class. We added a break point at line 35 "Class aListenerClass = Class.forName(splitString[0]);" because it keeps throwing a ClassNotFound exception, and it's being thrown after we split the string to identify the right listener. The "Controller" you see there, well, we are implementing the Application Controller pattern for the back-end of this calculator...

Why in the world is it throwing ClassNotFound? When we run this on debug mode in eclipse, we can check the value of the split elements, right? Well... it shows "CalculateListener" for the first element of the split string, yet when we expand the element it lists [C,a,l,c,u,l,a,t,e,L,i,s,t,e,n,e,r,_,e,q,u,a,l,s] as it's value--as if the string never got split, but it is being split! Check our code below. I'm also including the R file, so you guys can check all the ids and see how we are implementing reflection here. Please give us some light We would be eternally grateful!

In advance, thank you so much!

Sincerely,

Ben, Diego (0x783czar), Trevor, David, and Steve.



12 years ago

Jeff Verdegan wrote:Ben, please BeForthrightWhenCrossPostingToOtherSites(⇐click) so that people don't waste their time duplicating others' responses.

https://forums.oracle.com/forums/thread.jspa?threadID=2338022&tstart=0
https://coderanch.com/t/565563/java/java/some-light-please



I apologize for the cross posting. I didnt think about that until I read your post, so I'm really sory!

And thank you guys SO much for helping me with my code. The UML, was indeed provided by my teacher, but he said we are to implement and improve it whatever way we think is better, but then when we meet with him we must explain why we decided to implement it the way we did. I'm saying "we" because those cases are supposed to be group projects... lol nevermind.

Anyway, I can't tell you how much i appreciate all the sugestions! Thank you so much!

Edit: And for the problem of receiving simoutineous messages, i thought about writing a control statement inside the addMessage method to compare the message's time stamp to the key entries in the hashmap, and if they are the same, as well as the sender of the message, just append the two messages together. What do you think?
12 years ago
Hi everyone!

I guess this is the first time I'm actually opening a topic here, but I've actually been around for a little while

So, I'm working on this case for one of my classes. I have most of it already written, but there are a few things I'm trying to figure out and need help with. I'm supposed to write a class called ChatModel to hold ChatMessage Beans, such that:



Now here is the Bean I wrote. Nothing fancy :P



The ChatMessage class works as intended. Now here is what I have for the ChatModel class.



when I run this in the main method of ChatModel (just to test it)


This is what I get on the console:

is Ben in testModel? true
is Lucas in testModel? true
is Jack in testModel? true
is Bob in testModel? true
is message1 in testModel --> { Bob } ? true
is message2 in testModel --> { Ben } ? true
is message3 in testModel --> { Bob } ? true
is message4 in testModel --> { Jack } ? true
is message5 in testModel --> { Bob } ? true
is message6 in testModel --> { Jack } ? true
is message7 in testModel --> { Ben } ? true
is message8 in testModel --> { Lucas } ? true
is message9 in testModel --> { Ben } ? true
Receiver: Bob
Messsage: nothing

I'm puzzled all the tests come true, but the ArrayList I'm getting from getMessagesFor() apparently has only one ChatMessage object for "Bob" when it should have 3. When I add breakpoints to the code on Eclipse and watch it stepping through the code, it seems "message" is getting all the entries correctly, hence the containsKey() tests all came true. But when I look the ArrayList returned by getMessagesFor() there is only one entry when there should be more. Another thing I noticed is that the message entry seems to always be the last one added to the treemap.

By the way, I apologize for the inelegant and noobish code I wrote. Please feel free to point out style tips and anything whatsoever you might be thinking of. I welcome you advice! :P

Anyway, Java gurus out there, please share some of your knowledge with me!

Thanks in advance!

-Ben
12 years ago

jishnu dasgupta wrote:

Or else he will get a whole lot of NETPRODUCT...



Well noted lol thanks for the correction
12 years ago

fred rosenberger wrote:The problem is this:

sentence = "NETProduct" + i + ",";

you have explicitly told it to print a comma after every "NETProductx". simply put in some logic to decide if you want a comma or not. something like this (which has not been tested in any way)



Or he could simply include the comma before the inclusion of "i" in the string, such as



Hope it helped :P
12 years ago