Days Between Dates Java (homework) - java

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).

Related

Condtional statements using Calendar.HOUR_OF_DAY

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);

Why won't an IF statement work with rounded floats and doubles?

I am trying to check if a user has inputted a double between 3.5 and 7. I have tried to use the actual double and Math.round();, as well as StrictMath.round(). I have also tried to parse the string inputted as a float but it didn't change anything. Here is the basic code I used:
import java.util.Scanner;
public class IfStatement {
public static void main(String[] args) {
//create a Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter first double: ");
double number = input.nextDouble();
if (isDouble(number)==true) {
double x = Double.parseDouble(number);
if (3.5 <= x <= 7) {
System.out.println("good");
} else {
System.out.println("incorrect input");
}
} else {
System.out.println("incorroct input");
}
}
public static booleen isDouble(String str) {
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
++i;
}
int integerPartSize = 0;
int exponentPartSize = -1;
while (i < length) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
exponentPartSize = 0;
} else {
return false;
}
} else if (exponentPartSize > -1) {
++exponentPartSize;
} else {
++integerPartSize;
}
++i;
}
if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1)
|| exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
return false;
}
return true;
}
}
I have also tried making 3.5 3 in the if statement but got no dice. I just need a loose explanation and not a full solution.
I've edited your code.
boolean spelling
isDouble() takes a String
a <= x <= b chained expressions are not allowed in Java
Also,
if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1) || exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
return false;
}
return true;
can be simplified to
return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
&& exponentPartSize != 0 && (str.charAt(length - 1) != '.');
Here's the full code.
class Test {
public static boolean isDouble(String str) {
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
++i;
}
int integerPartSize = 0;
int exponentPartSize = -1;
while (i < length) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
exponentPartSize = 0;
} else {
return false;
}
} else if (exponentPartSize > -1) {
++exponentPartSize;
} else {
++integerPartSize;
}
++i;
}
return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
&& exponentPartSize != 0 && (str.charAt(length - 1) != '.');
}
public static void main(String[] args) {
//create a Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter first double: ");
String number = input.nextLine();
if (isDouble(number)) {
double x = Double.parseDouble(number);
if (3.5 <= x && x <= 7) {
System.out.println("good");
} else {
System.out.println("incorrect input");
}
} else {
System.out.println("incorroct input");
}
}
}

Addition of two binary numbers

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);
}

Why can't my test class run?

so I am trying to do a test class, the instruction is
Create a class called TestDate whose constructor creates fifteen Date objects and displays the day of the week for the dates by calling each Date’s getDayOfTheWeek() method. The dates are specifically these ones, in this precise order:
but when I run it, I keep getting errors! I think it's my constructor crashing or somewhere along that line.
Below is my TestDate class code
public class TestDate
{
private Date date;
/*
* Main Constructor
* #param fifteen specific date, test to your heart's content
*/
public TestDate()
{
Date d1 = new Date(1970, 11, 15);
System.out.println(date.getDayOfTheWeek());
Date d2 = new Date(1887, 7, 31);
Date d3 = new Date(1966, 5, 2);
Date d4 = new Date(1980, 8, 19);
Date d5 = new Date(2001, 9, 11);
Date d6 = new Date(1900, 6, 26);
Date d7 = new Date(1940, 2, 28);
Date d8 = new Date(1974, 10, 30);
Date d9 = new Date(1914, 1, 15);
Date d10 = new Date(1840, 10, 1);
Date d11 = new Date(1999, 12, 31);
Date d12 = new Date(1988, 5, 20);
Date d13= new Date(2012, 3, 10);
Date d14 = new Date(2006, 4, 1);
Date d15 = new Date(1992, 2, 29);
}
}
And here is my Date Class constructor and method getDayofTheWeek, I think that's all the information related to my problem.
public class Date
{
public Date(int year, int month, int day)
{
setYear(year);
setMonth(month);
setDay(day);
}
public String getDayOfTheWeek()
{
int step0 = year%100;
int step1 = step0 / 12;
int step2 = step0 % 12;
int step3 = step2 / 4;
int step4 = day;
int step5 = 0;
if(month == JANUARY || month == OCTOBER)
{
step5 = JAN_CODE;
}
else if(month == FEBURARY || month == MARCH || month == NOVEMBER)
{
step5 = FEB_CODE;
}
else if(month == APRIL || month == JULY)
{
step5 = APR_CODE;
}
else if(month == MAY)
{
step5 = MAY_CODE;
}
else if(month == JUNE)
{
step5 = JUN_CODE;
}
else if(month == AUGUST)
{
step5 = AUG_CODE;
}
else if(month == SEPTEMBER || month == DECEMBER)
{
step5 = SEP_CODE;
}
else
{
step5 = 0;
}
if(year/100 ==16 || year/100 == 20)
{
step5 = step5 + SPECIAL_CODE_SIX;
}
else if(year/100 == 17 || year/100 == 21)
{
step5 = step5 + SPECIAL_CODE_FOUR;
}
else if(year/100 == 18)
{
step5 = step5 + SPECIAL_CODE_TWO;
}
int step6 = (step1 + step2 + step3 +step4 + step5)%MOD_CODE;
if(step6 == SAT_CODE)
{
return "Saturday";
}
else if(step6 == SUN_CODE)
{
return "Sunday";
}
else if(step6 == MON_CODE)
{
return "Monday";
}
else if(step6 == TUE_CODE)
{
return "Tuesday";
}
else if(step6 == WED_CODE)
{
return "Wednesday";
}
else if(step6 == THU_CODE)
{
return "Thursday";
}
else if(step6 == FRI_CODE)
{
return "Friday";
}
else
{
return null;
}
}
}
and I passed the parameter in the mutator method
public void setYear(int year)
{
if(year <= CURRENT_YEAR && year >= YEAR_ZERO)
{
this.year = year;
}
}
public void setMonth(int month)
{
if(month <= DECEMBER && month >= JANUARY)
{
this.month = month;
}
}
public void setDay(int day)
{
if(month == JANUARY || month == MARCH || month == MAY || month == JULY || month == AUGUST
|| month == OCTOBER || month == DECEMBER)
{
if(day <= DAY_THIRTY_ONE && day >= DAY_ONE)
{
this.day = day;
}
else
{
year = CURRENT_YEAR;
month = JANUARY;
day = DAY_ONE;
}
}
else if(month == APRIL || month == JUNE || month == SEPTEMBER || month == NOVEMBER)
{
if(day <= DAY_THIRTY && day >= DAY_ONE)
{
this.day = day;
}
else
{
year = CURRENT_YEAR;
month = JANUARY;
day = DAY_ONE;
}
}
else if(month == FEBURARY)
{
if((isLeapYear() == true) && day <= DAY_FEB_LEAP && day >= DAY_ONE)
{
this.day = day;
}
else if((isLeapYear() == false) && day <= DAY_FEB_NORMAL && day >= DAY_ONE)
{
this.day = day;
}
else
{
year = CURRENT_YEAR;
month = JANUARY;
day = DAY_ONE;
}
}
}
Sorry for the long list of code, I don't really know which ones are useless.
In the second line of your constructor you are calling
System.out.println(date.getDayOfTheWeek());
on the variable date which is null at this point as it has not been initialized yet. This leads to a NullPointerException. Read here for more information: What is a NullPointerException, and how do I fix it?
What I am (assuming) you wanted to do there is:
System.out.println(d1.getDayOfTheWeek());
Things besides that that I noticed:
You use the #param field in a javadoc comment without there being a parameter. Do not do this. If your constructor does not take arguments then there should not be a #param.
You are creating 15 instances of Date in your constructor whose scope end as soon as the constructor is finished. You could just delete them and nothing would change. I am assuming you want this class to be a "test" whether your dates are initialized correctly. I would recommend doing this in a main method (for a quick test while coding) and with an actual test framework like JUnit for 'real' testing. You can read this tutorial if you are interested. It also goes into detail why you actually want to test.

Java convert else { if {} } to elseif {}

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;
}

Categories