I have this code and it has to show one of the strings based on the time of the day.
The code is
public void onTimeTick() {
mTime.setTimeInMillis(System.currentTimeMillis());
// Let's see what string we need according to the time
int saluteResId = R.string.salute_fallback;
if (mTime.get(Calendar.HOUR_OF_DAY) > 4) {
saluteResId = R.string.salute_morning;
} else if (mTime.get(Calendar.HOUR_OF_DAY) > 12) {
saluteResId = R.string.salute_evening;
} else if (mTime.get(Calendar.HOUR_OF_DAY) > 19 || mTime.get(Calendar.HOUR_OF_DAY) < 5) {
saluteResId = R.string.salute_night;
}
}
But the problem is that HOUR_OF_DAY will always remain greater than 4 so it will never even check the remaining two conditions and the string will always be set to salute_morning. I am not very good in java and trying to figure out how I can make the first condition false so it will check the other conditions and set the string according to them.
I think this can be helpful
if (mTime.get(Calendar.HOUR_OF_DAY) > 4 && mTime.get(Calendar.HOUR_OF_DAY) <= 12) {
//saluteResId = R.string.salute_morning;
} else if (mTime.get(Calendar.HOUR_OF_DAY) > 12 && mTime.get(Calendar.HOUR_OF_DAY) <= 19) {
//saluteResId = R.string.salute_evening;
} else if (mTime.get(Calendar.HOUR_OF_DAY) > 19 && mTime.get(Calendar.HOUR_OF_DAY) <= 4) {
//saluteResId = R.string.salute_night;
}
Try this code
final Calendar d = Calendar.getInstance();
final int hh = d.get(Calendar.HOUR_OF_DAY);
//try this way
String time = null;
if (hh == 0) {
time =("Salute_Night");
} else if (hh == 1) {
time =("Salute_Night");
} else if (hh == 2) {
time =("Salute_Night");
} else if (hh == 3) {
time =("Salute_Night");
} else if (hh == 4) {
time =("Salute_Night");
} else if (hh == 5) {
time =("Salute_Morning");
} else if (hh == 6) {
time =("Salute_Morning");
} else if (hh == 7) {
time =("Salute_Morning");
} else if (hh == 8) {
time =("Salute_Morning");
} else if (hh == 9) {
time =("Salute_Morning");
} else if (hh == 10) {
time =("Salute_Morning");
} else if (hh == 11) {
time =("Salute_Morning");
} else if (hh == 12) {
time =("Salute_Evening");
} else if (hh == 13) {
time =("Salute_Evening");
} else if (hh == 14) {
time =("Salute_Evening");
} else if (hh == 15) {
time =("Salute_Evening");
} else if (hh == 16) {
time =("Salute_Evening");
} else if (hh == 17) {
time =("Salute_Evening");
} else if (hh == 18) {
time =("Salute_Evening");
} else if (hh == 19) {
time =("Salute_Evening");
} else if (hh == 20) {
time =("Salute_Night");
} else if (hh == 21) {
time =("Salute_Night");
} else if (hh == 22) {
time =("Salute_Night");
} else if (hh == 23) {
time =("Salute_Night");
}
//your text view where you want it to display
textviewTimeOfDay.settext.(time);
Related
I tried to compere each number by putting the numbers in char arrays and compere them each by each with if conditions. Each outcome should be covered and each outcome should be saved in String result but the result of the whole operation is always blank. Java debugger isn't working and I donĀ“t see why it isn't working.
import java.util.Scanner;
public class BinaryAdder {
public static String add(String binary1, String binary2) {
String result = "";
char[] safea = binary1.toCharArray();
char[] safeb = binary2.toCharArray();
int lb1 = binary1.length() - 1;
int lb2 = binary2.length() - 1;
char reminder = 0;
while (lb1 != 0 || lb2 != 0) {
if (safea[lb1] == 0 && safeb[lb2] == 0 && reminder == 0) {
result += "0";
lb1--;
lb2--;
} else if (safea[lb1] == 1 && safeb[lb2] == 0 && reminder == 0) {
result += "1";
lb1--;
lb2--;
} else if (safea[lb1] == 1 && safeb[lb2] == 1 && reminder == 0) {
result += "0";
reminder = 1;
lb1--;
lb2--;
} else if (safea[lb1] == 1 && safeb[lb2] == 1 && reminder == 1) {
result += "1";
reminder = 1;
lb1--;
lb2--;
} else if (safea[lb1] == 1 && safeb[lb2] == 0 && reminder == 1) {
result += "0";
reminder = 1;
lb1--;
lb2--;
} else if (safea[lb1] == 0 && safeb[lb2] == 1 && reminder == 1) {
result += "0";
reminder = 1;
lb1--;
lb2--;
} else if (safea[lb1] == 0 && safeb[lb2] == 1 && reminder == 0) {
result += "1";
lb1--;
lb2--;
} else if (safea[lb1] == 0 && safeb[lb2] == 0 && reminder == 1) {
result += "1";
lb1--;
lb2--;
}
}
return result;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Summand: ");
String input1 = scan.next("(0|1)*");
System.out.print("Summand: ");
String input2 = scan.next("(0|1)*");
scan.close();
System.out.println("Result: " + add(input1, input2));
}
}
You can parse the bits into integer with Integer.parseInt(s, radix). You need to use radix of 2:
public static String add(String binary1, String binary2) {
int i1 = Integer.parseInt(binary1, 2);
int i2 = Integer.parseInt(binary2, 2);
return Integer.toBinaryString(i1 + i2);
}
I started learning java on my own a few weeks back and I keep running up to the same basic problem, where I can't call a method from another method in the same class. I either get a "symbol not found" error (I think because the method is out scope or the method just doesn't work anymore. The following code is part of a java exercise for creating a calender sort of a programme. I'll use the // in code commentaries to indicate, where exactly is the problem.
public class MyDate {
private int year;
private int month;
private int day;
private static String[] strMonths = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
private static String[] strDays = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
private static int[] daysInMonths = {31, 28, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static boolean isLeapYear(int year) { // This is the first method, which works fine
// when being called from the main method.
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
return true;
} else {
return false;
}
};
public static boolean isLeapYear; // I put this declaration in, because I got
// "symbol not found" errors, when referencing the method from the second method.
// I'm guessing it partially invalidates the first declaration.
public static boolean isValidDate(int year, int month, int day) { // The second method
if ((year >= 1 ) && (year <= 9999)){
if ((month >= 0) && (month <= 11)) {
if ((day >= 1) && ((month == 0) || (month == 2) || (month == 4) || (month == 6)
|| (month == 7) || (month == 9) || (month == 11)) && (day <= 31)) {
return true;
}
else if ((day >= 1) && ((month == 3) || (month == 5) || (month == 8)
|| (month == 10) && (day <= 30))) {
return true;
}
else if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
// Code from the first method (above), which I would like to replace with just a reference
// to the first method (for instance (isLeapYear = true)),
// but it doesn't work the same as the code above (or at all).
if ((month == 1) && (day == 29)) {
return true;
}
else
if ((day >= 1) && ((month == 1) && (day <= 28))) {
return true;
}
else {
return false;
}
}
}
}
return isValidDate; }
For reference, the method isLeapYear works as it's supposed to, when testing it with this main method:
public static void main(String[] args) {
System.out.println(isLeapYear(1900)); // false
System.out.println(isLeapYear(2000)); // true
System.out.println(isLeapYear(2011)); // false
System.out.println(isLeapYear(2012)); // true
}
}
Thanks for your help!
Remove public static boolean isLeapYear; from your code and every time you need to call a function, in this case public static boolean isLeapYear(int year) { ..} you must use a parenthesis to let the compiler know that you are actually calling a function there.
In this case you should go with isLeapYear(year)
and then if (isLeapYear(year)) { .. } else {..}
Given package name of MyApplication (substitute your package name in each place this one occurs below) import isLeapYear as shown below.
package MyApplication;
public class MyDate {
public static boolean isLeapYear(int year) { // This is the first method, which works fine
// when being called from the main method.
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
return true;
} else {
return false;
}
};
}
Main class:
package MyApplication;
import static MyApplication.MyDate.isLeapYear;
public class NewClass{
public static void main(String[] args) throws Exception
{
System.out.println(isLeapYear(1900)); // false
System.out.println(isLeapYear(2000)); // true
System.out.println(isLeapYear(2011)); // false
System.out.println(isLeapYear(2012));
}
}
I'm looking for any existing code or library in java or a similar language to convert given the following (which is not java, but a custom language)
if (i < 0) {
i = 0;
} else {
if (i > 100) {
i = 100;
}
}
into elseif like this:
if (i < 0) {
i = 0;
} else if (i > 100) {
i = 100;
}
This code is not java but I want to convert it using java.
Here is what I tried to acomplish this but it's not working
String elseB = "else {";
int index = output.indexOf(elseB);
while (index != -1) {
output = output.substring(index + 1);
index = output.indexOf(elseB);
if (index != -1) {
int ifAt = index + elseB.length() + 1;
String elseStart = output.substring(ifAt).trim();
if (elseStart.startsWith("if")) {
int closingBracket = findClosingBracket(
output.toCharArray(), index);
int openingBracket = ifAt - 1;
String justBlock = output.substring(openingBracket,
closingBracket).trim();
output = output.substring(0, openingBracket - 1) + justBlock + output.substring(closingBracket);
}
}
}
a more complicated example would be converting this:
if (i == 1) {
} else {
if (i == 2) {
} else {
if (i == 3) {
} else {
if (i == 4) {
} else {
if (i == 5) {
} else {
if (i == 6) {
} else {
if (i == 7) {
} else {
if (i == 8) {
} else {
if (i == 9) {
} else {
if (i == 10) {
} else {
if (i == 22) {
} else {
if (i == 11) {
} else {
if (i == 12) {
} else {
if (i == 25) {
} else {
if (i == 13) {
} else {
if (i == 14) {
} else {
if (i == 15) {
} else {
if (i == 24) {
} else {
if (i == 16) {
} else {
if (i == 17) {
} else {
if (i == 18) {
} else {
if (i == 21) {
} else {
if (i == 19) {
} else {
if (i == 20) {
} else {
if (i == 23) {
} else {
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
into this
if (i == 1) {
} else if (i == 2) {
} else if (i == 3) {
} else if (i == 4) {
} else if (i == 5) {
} else if (i == 6) {
} else if (i == 7) {
} else if (i == 8) {
} else if (i == 9) {
} else if (i == 10) {
} else if (i == 22) {
} else if (i == 11) {
} else if (i == 12) {
} else if (i == 25) {
} else if (i == 13) {
} else if (i == 14) {
} else if (i == 15) {
} else if (i == 24) {
} else if (i == 16) {
} else if (i == 17) {
} else if (i == 18) {
} else if (i == 21) {
} else if (i == 19) {
} else if (i == 20) {
} else if (i == 23) {
} else {
}
I assume you want to change the code, not the compiled bytecode. For that you would look into text replacing. Depending on your editor you can replace all the instances of this text with the elif statement. If your editor can not do it, look into regular expressions. With those you can change the lines in no time.
If trying to test a single value vs various conditions,could try to use switch statemenet instead various if statements.
ie.
switch(i){
case(i <= 0):
i=0;
case(i >= 100):
i=100;
}
I am having trouble nesting these if statements from Python to Java.
def leapyear(yearr):
if (year % 4 == 0):
if (year % 100 == 0):
if (year % 400 == 0):
return True
else:
return False
else:
return True
else:
return False
I currently am working to convert the above to Java:
boolean leapyear(int year) {
if (year % 4==0) {
if (yearr%100==0) {
if (year%400==0) {
else
return false;
}
else
return true;
}
else
return false;
}
}
However, my Java conversion is giving me errors, mainly because I do not think my nested conditionals have the right closed braces. Can you give me any hints or resources that can help me figure this out?
It's not good practice to have so many possible exit points. Also, if you don't want to reinvent the wheel, you can use Java library code, for example:
boolean isLeapYear(int year){
GregorianCalendar cal = new GregorianCalendar();
return cal.isLeapYear(year);
}
Your else statements need braces too, you forgot a return statement for the innermost if, and you misspelled yearr in one location:
boolean leapyear(int year) {
if (year % 4==0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
Java doesn't need the indentation like Python does, but it'd help make your branching structure more readable if you did use it anyway:
boolean leapyear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
} else {
return false;
}
} else {
return true;
}
} else {
return false;
}
}
You don't need as many return statements here, the year % 400 == 0 already evaluates to a boolean:
boolean leapyear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
return year % 400 == 0;
} else {
return true;
}
} else {
return false;
}
}
or as a one-liner:
boolean leapyear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
For completeness sake, here is the Python version the way I would write it:
def leapyear(year):
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
or better still, use calendar.isleap() instead.
What about removing the ifs altogether?
public boolean leapyear(int year) {
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
heres my attempt:
boolean leapyear(int year)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
return (year % 400 == 0);
}
else
{
return true
}
}
return false;
}
or use this:
return Java.util.GregorianCalendar.getInstance().isLeapYear(year)
boolean leapyear(int year)
{
if (year % 4==0)
{
if (year%100==0)
{
if (year%400==0)
{
return true;
}
else
{
return true;
}
}
else``
{
return false;
}
}
}
This is a couple days off when I run the program. Any advice on what I am doing wrong?
I know there is a simpler way to do it, but for this I'm trying to show all the actual steps in finding the days between.
Homework assignment, so cannot use date-time libraries.
public class DaysBetween {
public static void main (String []args) {
long months1 = Long.parseLong(args[0]);
long days1 = Long.parseLong(args[1]);
long year1 = Long.parseLong(args[2]);
long months2 = Long.parseLong(args[3]);
long days2 = Long.parseLong(args[4]);
long year2 = Long.parseLong(args[5]);
long daysbetween = 0;
long leapyearcounter = 0;
boolean leapyear1 = false;
boolean leapyear2 = false;
boolean valid1 = true;
boolean valid2 = true;
int earlier = 0;
// this tests to see which date is earlier
if (year1 == year2){
if (months1 == months2) {
if (days1 == days2) {
daysbetween = daysbetween;
} else if (days1 < days2) {
earlier = 1;
}
} else if (months1 < months2) {
earlier = 1;
}
} else if (year1 < year2 ) {
earlier = 1;
}
// this switches the dates depending on which is earlier
switch(earlier) {
case 1: months1 = Long.parseLong(args[0]);
days1 = Long.parseLong(args[1]);
year1 = Long.parseLong(args[2]);
months2 = Long.parseLong(args[3]);
days2 = Long.parseLong(args[4]);
year2 = Long.parseLong(args[5]);
break;
default:
months1 = Long.parseLong(args[3]);
days1 = Long.parseLong(args[4]);
year1 = Long.parseLong(args[5]);
months2 = Long.parseLong(args[0]);
days2 = Long.parseLong(args[1]);
year2 = Long.parseLong(args[2]);
break;
}
//this section tests if the earlier date is valid
if((year1 % 4 == 0) && (year1 % 100 != 0) || (year1 % 400 == 0)) {
leapyear1 = true;
}
if ((leapyear1 == true) && (months1 == 2)) {
if (days1 <= 29 && days1 >=1){
valid1 = true;
}
} else if ((leapyear1 == false) && (months1 == 2)){
if (days1 <= 28 && days1 >= 1){
valid1 = true;
}
}
if (months1 != 2) {
if (months1 < 8 ) {
if (months1 %2 != 0) {
if (days1 <= 31 && days1 >=1){
valid1 = true;
} else {
valid1 = false;
}
} else if (months1 %2 == 0) {
if (days1 <= 30 && days1 >=1){
valid1 = true;
} else {
valid1 = false;
}
}
} else if (months1 > 8) {
if (months1 %2 != 0) {
if (days1 <= 30 && days1 >=1){
valid1 = true;
} else {
valid1 = false;
}
} else if (months1 %2 == 0) {
if (days1 <= 31 && days1 >=1){
valid1 = true;
} else {
valid1 = false;
}
}
}
}
// this section tests if the later date is valid
if((year2 % 4 == 0) && (year2 % 100 != 0) || (year2 % 400 == 0)) {
leapyear2 = true;
}
if ((leapyear2 == true) && (months2 == 2)) {
if (days2 <= 29 && days2 >=1){
valid2 = true;
}
} else if ((leapyear2 == false) && (months2 == 2)){
if (days2 <= 28 && days2 >= 1){
valid2 = true;
}
}
if (months2 != 2) {
if (months2 < 8 ) {
if (months2 %2 != 0) {
if (days2 <= 31 && days2 >=1){
valid2 = true;
} else {
valid2 = false;
}
} else if (months2 %2 == 0) {
if (days2 <= 30 && days2 >=1){
valid2 = true;
} else {
valid2 = false;
}
}
} else if (months2 > 8) {
if (months2 %2 != 0) {
if (days2 <= 30 && days2 >=1){
valid2 = true;
} else {
valid2 = false;
}
} else if (months2 %2 == 0) {
if (days2 <= 31 && days2 >=1){
valid2 = true;
} else {
valid2 = false;
}
}
}
}
//this adds a day to the total if the earlier date is in january & its a leap year
if ((months1 == 1) && (leapyear1 == true)) {
daysbetween = daysbetween +1;
}
//this adds the months left in year1
if (months1 == 1) {
daysbetween = daysbetween + 334;
} else if (months1 == 2) {
daysbetween = daysbetween + 306;
} else if (months1 == 3) {
daysbetween = daysbetween + 275;
} else if (months1 == 4) {
daysbetween = daysbetween + 245;
} else if (months1 == 5) {
daysbetween = daysbetween + 214;
} else if (months1 == 6) {
daysbetween = daysbetween + 184;
} else if (months1 == 7) {
daysbetween = daysbetween + 153;
} else if (months1 == 8) {
daysbetween = daysbetween + 122;
} else if (months1 == 9) {
daysbetween = daysbetween + 92;
} else if (months1 == 10) {
daysbetween = daysbetween + 61;
} else if (months1 == 11) {
daysbetween = daysbetween + 31;
} else { }
// this adds the extra day if year2 is leap year and if the month is march or later
if ((months2 >= 3) && (leapyear2 = true)) {
daysbetween = daysbetween + 1;
}
// this adds the months up to the month in year2
if (months2 == 1) {
daysbetween = daysbetween;
} else if (months2 == 2) {
daysbetween = daysbetween + 31;
} else if (months2 == 3) {
daysbetween = daysbetween + 59;
} else if (months2 == 4) {
daysbetween = daysbetween + 90;
} else if (months2 == 5) {
daysbetween = daysbetween + 120;
} else if (months2 == 6) {
daysbetween = daysbetween + 151;
} else if (months2 == 7) {
daysbetween = daysbetween + 181;
} else if (months2 == 8) {
daysbetween = daysbetween + 212;
} else if (months2 == 9) {
daysbetween = daysbetween + 243;
} else if (months2 == 10) {
daysbetween = daysbetween + 273;
} else if (months2 == 11) {
daysbetween = daysbetween + 304;
} else if (months2 == 12) {
daysbetween = daysbetween + 334;
} else { }
//this add the days left in month1
if (months1 != 2) {
if (months1 < 8 ) {
if (months1 %2 != 0) {
daysbetween = daysbetween + (31 - days1);
} else if (months1 %2 == 0) {
daysbetween = daysbetween + (30 - days1);
}
} else if (months1 > 8) {
if (months1 %2 != 0) {
daysbetween = daysbetween + (30 - days1);
} else if (months1 %2 == 0) {
daysbetween = daysbetween + (31 - days1);
}
}
}
// this add the days left in month1 if its feb
if ((leapyear1 == true) && (months1 == 2)) {
daysbetween = daysbetween + (29 - days1);
} else {
daysbetween = daysbetween + (28 - days1);
}
daysbetween = daysbetween + days2; // adds the days left in month2
daysbetween = daysbetween + ((((year2-1)-(year1+1))+1)*365); // adds the days in the years to the total
for (long i = (year1+1); i < (year2-1) ; i++) { // sees the # of leapyears b/n year1 & year2
if ((i % 4 == 0) && (i % 100 != 0) || (i % 400 == 0)) {
leapyearcounter++;
}
}
daysbetween = daysbetween + leapyearcounter; // adds the leapyear days
if ((valid1 == false) || (valid2 == false)) {
System.out.println("Sorry! You gave me one or more invalid dates!");
} else if (daysbetween == 1){
System.out.println("There is " + daysbetween + " day between those two dates!");
} else if (daysbetween != 1) {
System.out.println("There are " + daysbetween + " days between those two dates!");
} else if (daysbetween == 0) {
System.out.println("There are no days between those two dates!");
}
}
}
Unless this homework or just some self hurting thing you have going can I recommend using the joda time library for all your datetime calculations:
Time to use joda answer: ~22 minutes
There's a lot that needs to be said here, but I'd recommend that you start by refactoring this monster into lots of smaller methods that you can test individually. Your code is difficult to read, understand, and debug because of the sheer volume. "Decomposition" is the operative word.
Your main method should only have driver or test code in it.
You should have methods like boolean isValid(Date date) and boolean before(Date d1, Date d2) and boolean isLeapYear(int year). Test these individually and start building up to what you want.
Use java.util.Date: (date1.getTime()-date2.getTime())/(1000*60*60*24).