Daniel Ungerfält

Ranch Hand
+ Follow
since Feb 12, 2015
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Daniel Ungerfält

Dave Tolls wrote:That is caused by what Carey said in the last line of his post.
You are looping over the Entries in the Map and returning the first one only.

You need to decide whether getAnswers returns a pre-formatted String you can simply display to the user, or whether it returns the keySet (you can get the keys by themselves from a map without the values) and leaves it up to the calling code to do the layout. I would prefer the latter.

"You are looping over the Entries in the Map and returning the first one only."
Yes I noticed that. I just dont know how to solve that Im affraid.

"or whether it returns the keySet "
But thats just what Im trying to do? ---> return (String) entry. ---> getKey(); <---- (return (String) entry.getKey();)
I was just missing to set the Hashmap as a "new". Like hashmap = new Hashmap :-)

My only issue now is that my loop just returns one of the values. This is what shows when I view the database TOAST: Category: Music, Question: What is the best selling album? Correct Answer: BLACK IN BLACK Incorret Answer1 :BLACK IN BLACK IncorrectAnswer2 : BLACK IN BLACK Incorret Answer3 : BLACK IN BLACK
Got no errors in the log.

myDb.insertQuestions(new Question("MUSIC","What's the second best selling album?",
"NeverMind", "Thriller","BACK IN BLACK","Black Album"));

Liutauras Vilda wrote:1. Why do you have all your fields as public? You shouldn't be doing that, make them private.

2. Variables a1, a2, a3, what these are? Answers.. You know, but someone else will need to read all your code to find out that.
Don't try to save space in the price of readability. Every variable has to tell you right away, what he is meant to store. We know about i and j because these are conventional for the loop iterations, but a1, a2 are not.

3. Code below. Apart from the missing curly braces around if statement block, formatting is also incorrect. for loop suppose to be shifted by 4 spaces to the right side. Again, everything goes down to the code readability, omitting the fact, that you could miss something important and that code could behave wrongly because of simply missing curly braces.
Actually there is something wrong with this code. Two mistakes I see. Method name suggests that it should return the list, but it appears it is returning not the list, but array element (single), not whole array. Also similar formatting problems in other method too.

That is it at the moment from me.

Im very sorry for that :-)
I got a bad habbit on doing that just to clean it up later ;.)
Now again to the topic that is the hashmap. It returns this nullpointer as i mentioned above. I still got that same problem.

Carey Brown wrote:When your problem manifests itself as a thrown exception, please make sure to cut and paste the stack trace in your post. And also make sure you have provided a listing of code that includes the line indicated by the stack trace.

Nit: You have a variable called 'list' which is not a list but an array. Better yet, name it for what the variable contains, e.g. 'answers', or something like that.

You have several loops with a return that is not enclosed in an if(), therefore, only the first element will ever be returned.

Yea the list was just a name i put there for a little experiment with array instead of hashmap, its works fine without curlybrackets actually.

Is this what you mean by stacktrace?:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.put(java.lang.Object, java.lang.Object)' on a null object reference

Got two blue rows thats clickable in logcat which I guess causes trouble.

(Question.java:21) -> answers.put(correctAnswer, true);
MainActivity.java:35) -> db.insertData(new Question("WHAT BAND?","uiop","er","ty","qp"));

Ok so I managed to store some data with the use of a string for questions and string array for the answers no problem. But I want the answers to be stored in a hashmap<String,Boolean> But I got a nullpointerexception that way and cant figure out why Im affraid.

Here's the code :


Campbell Ritchie wrote:It is not a good idea to decide to use a database before you decide what you want to store. Have you considered a question class with question and answers associated encapsulated? You can include wrong answers and randomise the order the answers appear in to make it less predictable.

Yea I do consider a question class. Maybe just to fill the DB. Should I just go something like this:?

DBHelper db = new DBHelper(this);

db.insertData("Music","What's the second best selling album?", "BACK IN BLACK");
db.insertData("Music","blablabla", "blabla");
db.insertData("Music","blablabla", "blablabla");
db.insertData("Music","blablabla", "blablabla");
db.insertData("Music","blablabla", "blablabla");

Is there a better way to do this by a loop? I cant really come up with any idea how that would save alot of space except the use of db.insertData every on every line. Maybe set the questions/answers as variables ->
q1,q2,q3 etc.. and loop trough with q + i (i++) ?? But that will just increase the amount of code I guess....
Hi there!
I would like to make a QuizApp with 5 categories and a bunch of questions and 4 possible answers.
I would just like to know the best way of storing this data into the database. I already have a DBHelper class in my project that works for manually adding categories,question etc. But that's just for the users if
they would like to add there own questions to the app. I would like a way to store a bunch of default/start-questions in the code. Cant find any info on how or where to put that data in the code?
For some reason it just started to working! :-)
But now I got a new problem. I added a second button just to be able to exit the app. When adding the button and the code the app just brakes in the right beggining,
-with a message like: "your app has stopped" and a ok-button.

8 years ago
Im all new to Android Studio and trying to make a simple little halloween app.
I have two backgroundpictures/pictures whatever and I want the second one to appear when pressing the button.
The first pictures and the button is visible. The only thing that happens when pressing the button is that the app is closing.
Here´s my code:

8 years ago
Hi there!
Trying to make a "simpel" little dicegame. First choose number of players, number of dice, number of sides on the dice(maxValue) and lastly give name to the players.
The game is about to guess the value of the total dices before throwing them. If guess is right add 1 point to he player. Five rounds then finito and the score for every player prints.

My problem is that when I try to initialize the Player class. Give every player there names and dices. It only returns the adress and not the names I put in like this: [Player@29edc073, Player@37f5d386, Player@6c121f1d, Player@19b8e059, Player@38910040]. And the for-lop to give every player dices just crashes(indexoutofbounds). So how do I have to approach this to get it right?

the code:

8 years ago

inverse2Power(16) -> 1 + inverse2Power(8) -> 1 + 3 -> 4

Thanks alot!
Starting to understand, but how can inverse2Power(8) be 1 +3 and not 1 +4 ? inverse2Power(4) seems logic when you say 1+2 -> 3...
8 years ago

Henry Wong wrote:Joe, Nice diagram. You earned a cow.

BTW, in reading this thread again, I think the OP is thinking that the results should be the sum of 8, 4, 2, 1; ie. the sum of the parameters, and not what was returned from the method call. Or in other words, the OP was thinking about the value of "n/2" and not the value returned from the inverse2power(n/2) method call?


Exactly! I Still cant figure out how it works. Cause if you look at it, it looks like 1 + (16/2). And my mathematic brain read this as (1 + (16/2)) + (1 + (8/2)) and so on...
8 years ago
Hi there!
Trying to learn the very basic of this but been struggling really hard a couple of days now.

I have made up some simple buttons just to try its function but just get this result: "unfortunately, myCalculator has stopped" when pressing any button on the virtual device.

Another thing is that every time I run any virtual device my discspace reduces by somewhere around 500MB-1GB each time. Cant find any way to "clean" or restore that space..

8 years ago
Fixed it! I had to use name = s.next(); instead of s.nextLine(); cause that just skipped to the next intput( s.nextInt();)
8 years ago
Starting to get mad here.. Was just putting up a simple loop to try store values but got this missmatch error after second time I type in a name.

8 years ago