Why won't Log.i work? - java

Here's my code. I want to make sure that randomNumber remains the same until the user chooses the right number, so I'm trying to display that number via Log.i. However, whenever my button is clicked, nothing is printed to the android monitor.
int randomNumber = (int) (Math.random() * 50) + 1;
public void checkNumber(View view) {
Log.i("Number", Integer.toString(randomNumber));
EditText numberEntered = (EditText) findViewById(R.id.numberEntered);
int numberEnteredInt = Integer.parseInt(numberEntered.getText().toString());
if(!(numberEnteredInt <= 50 && numberEnteredInt >= 1)) {
//some code
} else if(numberEnteredInt < randomNumber) {
//some code
} else if(numberEnteredInt > randomNumber) {
//some code
} else {
//some code
randomNumber = (int) (Math.random() * 50) + 1;
}
}
Just by running this app multiple times, it seems that the rest of my code is working properly (so I don't actually need Log to work in this case), but I am curious why nothing is being displayed to the monitor. Thanks.
Update:
Ok, now it's working perfectly; I don't know what was happening before. I noticed a couple of you recommended using "" + randomNumber instead of Integer.toString(randomNumber). Is the former more efficient? Thanks again.

I don't believe you even need to use the Integer.toString() at all. You can just replace that whole line with:
Log.i("NUMBER", "" + randomNumber);
If you are calling the checkNumber() method as the onClick for the button, it should absolutely be printing to the console. Make sure your console is set to DEBUG mode, and that the line above the printouts doesn't read "No debuggable applications", but instead says the name of your app.

Are you sure checkNumber() is called in your onClickListener? If so, try adding \n to the Log message.
Log.i("Number", "\n" + randomNumber);

Related

Need Help! Number Wizard in java?

I was just trying to get NumberWizard to work on java and I couldn't quite get it. Or can you just not do it in java? I'm pretty stuck because I can only get it to guess one time.
I'm using Processing 3 and my code looks like this:
int max = 1000, min = 1, guess = 500;
void setup() {
size(400, 400);
background(0);
println("welcome to NumberGuesser ");
println("In this game you're gonna think of a ");
println("number and im gonna guess it as fast as possible ");
println(" \n ");
println("Now pick of a number between " + min + "and" + max);
println("Great! now that you have picked a number ");
println("Press the up-arrow if its more");
println("and the down-arrow if it's less ");
println("is it less or more than "+ guess + "? \nif more press up-arrow, if less press down-arrow");
}
void draw() {
if (key == CODED) {
if (keyCode == 38) { // up-arrow
min = guess;
nextGuess();
} else if (keyCode == 40) { //down-arrow
max = guess;
nextGuess();
} else if (keyCode == 13) { //return
win();
}
}
}
void nextGuess() {
frameRate(1);
guess = (max + min) /2;
println("Is it less or more than "+ guess + "? \nif more press up-arrow, if less press down-arrow ");
}
void win() {
println("that was'nt hard at all, ezz pezz! ");
noLoop();
}
Please note that the draw() function fires 60 times per second. Even after you call frameRate(1), the draw() function fires once per second. Further note that all of your logic is inside your draw() function. So the guessing will happen on an interval.
You're also not checking whether the key is currently pressed. Note that the key and keyCode variables contain the most recently pressed key. You still need to check whether the key is currently pressed. You can use the keyPressed variable or the keyPressed() function for this.
If I were you, I'd modify my program to use the keyPressed() function to detect user input instead of polling for it in the draw() function.
Also, you need to get in the habit of debugging your code. Try to isolate the problem to a specific line of code that's behaving differently from what you expected, instead of posting your full program and saying it's not working.

While loop doesn't continue even though condition still holds true

I was tasked to create a simulation of a Bank. So far the progress I've made are generating random Client objects, making 4 objects of Teller, putting the Client objects into a circular queue and putting a Client into an empty Teller if there are any.
The problem I have now is that the while loop I made does no continue even though the set condition is still true, it can only execute 5 times. The program still runs though.
This is the code I have so far(assume all variables are declared):
public void run() {
int counter = 1;
populateTellers(windowArray);
while (counter < 10) {
newClient = generateClient();
System.out.println(newClient.toString() + " arrived at :" + counter);
System.out.println();
clientQueue.enqueue(newClient);
clientList.add(newClient);
System.out.println("The Queue now contains " + clientQueue.numberOfOccupiedCells() + " Client(s):");
for (int x = 0; x < clientList.size(); x++) {
System.out.println(clientList.get(x).toString());
}
System.out.println();
arrayIndex = getATeller(windowArray);
if (arrayIndex != -1) {
clientList.remove(0);
clientQueue.dequeue();
windowArray[arrayIndex].setClient(newClient);
windowArray[arrayIndex].setServiceTime(newClient.getDuration());
System.out.println(windowArray[arrayIndex].getName() + " now serves " + newClient.toString());
} else {
for (int x = 0; x < clientList.size(); x++) {
if (windowArrray[arrayIndex].getServiceTime() == counter) {
windowArray[arrayIndex].setClient(new Client());
System.out.println(windowArray[arrayIndex].getName() + " ends service for " + newClient.toString());
}
}
}
counter++;
}
}
The one thing that I can't get head around is that when I remove the code from aI = getATeller(wA); 'till the closing bracket of the if statement below it the program would work. I've tried putting the block into a method, changing the positions of the codes inside if(){} and else{} and changing the condition to if(aI==1), still I got no improvements. Any idea on what I'm doing wrong?
EDIT: So I've been able to narrow it down to a line of code which is windowArray[arrayIndex].setServiceTime(newClient.getDuration());, whenever I assign a value to a Teller's service time the problem would occur, I've checked my Teller class and there was no error there. Any ideas how I can solve this.
Why is it only executing 5 times? According to the code, counter is used as the used to check if it should continue looping for not. The only instance where counter is changed is at last line: counter++.
I can guess that you did a System.out.print on the counter, and it ends up to 5, and it magically stops. Most likely it's throwing an exception, which is not caught, and is lost in the void.
Edit: If this piece of code is being run on another thread, then maybe it's hanging up on some function. Do as told here
Try to find where your loop was turning to infinite loop,
Try writing printing statements, wherever necessary, just to find the flow of execution.
Then printout your variable values, too check whether they were as expected, keep on narrowing your scope as you find error occurred in certain scope, then you will find the culprit, if it is leading you to some other function, then thoroughly check that particular function.

Displaying multiple lines from while loop in one output box

My assignment asks to print all values from 1-10 using while loops in one output dialog box, with each number appearing on a separate line. So far I have:
int i=1;
while (i <= 10)
{
System.out.println(i);
i++;
}
and it displays 1-10 on separate lines but that's in the command prompt window. I need it to be displayed on one output box. How would I do that?
Forgot to mention, I know how to display the output in a dialog box, however it displays it one by one in separate boxes rather than in just one. How would I make it display in only one and not 10 different boxes?
What you are looking for can be found in the Java Docs. The classes, as well as how to use them can be found here:
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
Be sure to import this:
import javax.swing.JOptionPane
When you're just printing to the console, all you have to do is change System.out.println to System.out.print:
int i = 1;
while (i <= 10)
{
System.out.print(i);
i++;
}
An alternative way to do it is this:
String s = "";
int i = 1;
while (i <= 10) {
s += i;
i++;
}
System.out.println(s);

for loop jumps to last number in java android development

Hi I am making an android App, I want to add some values to a database and I want to do N times so I used a for loop as seen below:
private void addCodeToDataBase() {
for (int i = 1; i <= 100; i++) {
//indexnumber is a TextView
indexNumber.setText("Please enter the TAN code for Index number " + i);
//tanCode is an EditText
if (tanCode.getText().toString() != null) {
//index here is just an int so i can use the i inside the onClick
index = i;
//add is a button
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String codeText = tanCode.getText().toString();
dbHandler.addcode(index, codeText);
}
});
} else {
Toast.makeText(addcode.this, "Please enter your code !!!", Toast.LENGTH_LONG).show();
}
}
}
but what I am facing here is the for loop jumps to 100 at the first run, What I mean is the text will show :
Please enter the TAN code for Index number 100
it skips 99 numbers!! how would I fix it ?
It's Because your for loop executes so fast that you can't notice that the change of the text.First i is 0,and then it becomes 1,then the text will be "Please enter the TAN code for Index number 1" ......
your loop is working correctly but it is replacing text on each iteration that's why you think that it is jumping on last value please use break point and debug you will see each value on each iteration or use log in which you will see each value
It's not easy to imagine what your code does without seeing your declarations of indexNumber, tanCode, index, and, in particular, add. So, e.g., we don't know how often your if condition yields true.
However, most probably, the problem is that your assignment add.setOnClickListener(...) is just iterated with no user interaction in between. Now if you repeatedly assign something to your add (whatever that is), the last assignment will win.
If you want 100 buttons, you'll need to have an array or List of buttons to press, where each has a different tan code. If you want one button that repeatedly asks for the different tans, then you have to assign the data for click i + 1 only after click i has been handled, i.e. in the on click listener.
To give more specific help, we would need to know how your user interface should look (how many widgets of what kind) and how each widget should behave.

Calculation function in android app causes force close, what's wrong here?

I'm currently developing an app, and have run into an issue causing it to force-close on me whenever I try to call this function:
public void calcActRun() {
double calcmass = bodymass * genderValue;
int qty1 = Integer.parseInt(findViewById(R.id.calcItemQty1).toString());
double vol1 = Double.parseDouble(findViewById(R.id.calcItemVol1).toString());
double percent1 = Double.parseDouble(findViewById(R.id.calcItemQty1).toString());
double alcoholTotal = 0;
double alcoholConcentration = alcoholTotal /calcmass;
int time = Integer.parseInt(findViewById(R.id.calcInputTime).toString());
double alcoholCurrentConcentration = alcoholConcentration - (0.15 * time);
}
}
I have defined all variables (except the ones defined inside this function) earlier in the code, and eclipse isn't giving me any hints as to why it crashes...
The goal is to input the various values in a form (as you may already have guessed from the code) and get the calculated output when a button in the UI is clicked.
I even tried calling it using a "man in the middle", with the same result (code displayed below)
public void calcButtonClicked(View v) {
calcActRun();
}
I know the button itself is working, as everything went ok when I replaced the contents with this:
Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
I would be really grateful if anyone could help me demystify this problem - also if you need more info just ask... :)
I think you want
((TextView)findViewById(R.id.whatever)).getText().toString()
instead of
findViewById(R.id.whatever).toString()

Categories

Resources