I fixed my setMonthnum method, but now my input will set the month number to 0, 13, etc. based off of my input. I need to know how to go and ask again for input and not to set my Monthnum to the incorrect input. If you have any suggestions to simply better my code, please feel free to state them! My code is as follows:
import java.util.Scanner;
public class whichMonth {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the month name or number: ");
int monthNumber = input.nextInt();
//set up variable access to the class
AnyMonth inputMonthNumber = new AnyMonth();
// set the month number from user input
inputMonthNumber.setMonthnum.input.nextInt(monthNumber);
// get the month name from user input number
String monthName = inputMonthNumber.getMonthName(monthNumber);
}
}
class AnyMonth {
int Monthnum;
String monthName;
public AnyMonth() {
Monthnum = 1;
}
public AnyMonth(int currentMonthNumber) {
Monthnum = currentMonthNumber;
switch(currentMonthNumber) {
case 1:
Monthnum = 1;
monthName = "January";
System.out.println("January");
break;
case 2:
Monthnum = 2;
monthName = "February";
System.out.println("February");
break;
case 3:
Monthnum = 3;
monthName = "March";
System.out.println("March");
break;
case 4:
Monthnum = 4;
monthName = "April";
System.out.println("April");
break;
case 5:
Monthnum = 5;
monthName = "May";
System.out.println("May");
break;
case 6:
Monthnum = 6;
monthName = "June";
System.out.println("June");
break;
case 7:
Monthnum = 7;
monthName = "July";
System.out.println("July");
break;
case 8:
Monthnum = 8;
monthName = "August";
System.out.println("August");
break;
case 9:
Monthnum = 9;
monthName = "September";
System.out.println("September");
break;
case 10:
Monthnum = 10;
monthName = "October";
System.out.println("October");
break;
case 11:
Monthnum = 11;
monthName = "November";
System.out.println("November");
break;
case 12:
Monthnum = 12;
monthName = "December";
System.out.println("December");
break;
default:
Monthnum = 1;
monthName = "January";
}
}
public AnyMonth(String userMonthName) {
switch(userMonthName) {
case "January":
Monthnum = 1;
monthName = "January";
System.out.println("January");
break;
case "February":
Monthnum = 2;
monthName = "February";
System.out.println("February");
break;
case "March":
Monthnum = 3;
monthName = "March";
System.out.println("March");
break;
case "April":
Monthnum = 4;
monthName = "April";
System.out.println("April");
break;
case "May":
Monthnum = 5;
monthName = "May";
System.out.println("May");
break;
case "June":
Monthnum = 6;
monthName = "June";
System.out.println("June");
break;
case "July":
Monthnum = 7;
monthName = "July";
System.out.println("July");
break;
case "August":
Monthnum = 8;
monthName = "August";
System.out.println("August");
break;
case "September":
Monthnum = 9;
monthName = "September";
System.out.println("September");
break;
case "October":
Monthnum = 10;
monthName = "October";
System.out.println("October");
break;
case "November":
Monthnum = 11;
monthName = "November";
System.out.println("November");
break;
case "December":
Monthnum = 12;
monthName = "December";
System.out.println("December");
break;
default:
Monthnum = 1;
monthName = "January";
}
}
public void setMonthnum (int userMonth) {
Monthnum = userMonth;
if (userMonth < 1 || userMonth > 12){
System.out.println("Invalid input");
this.Monthnum = userMonth;
}
}
public int getMonthnum(int currentMonthNumber) {
Monthnum = currentMonthNumber;
return currentMonthNumber;
}
public String getMonthName(int currentMonthName) {
Monthnum = currentMonthName;
// use swtich-case here instead, default case should return 1/Jan
switch(currentMonthName) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";;
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
break;
default:
monthName = "January";
}
return monthName;
}
public String toString(String monthNameToString){
monthName = monthNameToString;
return monthNameToString;
}
public boolean equals(int month) {
if (Monthnum == month) {
return true;
}
else {
return false;
}
}
public boolean greaterThan(int month) {
if (Monthnum > month) {
return true;
}
else {
return false;
}
}
public boolean lessThan(int month) {
if (Monthnum < month) {
return true;
}
else {
return false;
}
}
}
change
inputMonthNumber.setMonthnum.input.nextInt(monthNumber);
to
inputMonthNumber.setMonthnum(monthNumber);
Also, in all the getXXX methods, remove the below statement:
Monthnum = currentMonthName;
Getters are desiged to return the value. Setting the values via getter methods is not a good practice. This article explains why getters and setters are needed any how to write them :)
Related
This question already has answers here:
Is calling static methods via an object "bad form"? Why?
(4 answers)
Static method in Java can be accessed using object instance [duplicate]
(5 answers)
Closed 3 years ago.
I am trying to invoke the static Method. What I know is that we have to use the class name to invoke a static method. However, I found that static methods can be invoked even with the help of object.
For example:
public class SwitchStaticMethod {
//static methods can also be called by object
/**
* #param args the command line arguments
*/
public static int getMonthNumber(String month) {
int monthNumber = 0;
if (month == null) {
return monthNumber; }
switch (month.toLowerCase()) {
case "january": monthNumber = 1; break;
case "february": monthNumber = 2; break;
case "march": monthNumber = 3; break;
case "april": monthNumber = 4; break;
case "may": monthNumber = 5; break;
case "june": monthNumber = 6; break;
case "july": monthNumber = 7; break;
case "august": monthNumber = 8; break;
case "september": monthNumber = 9; break;
case "october": monthNumber = 10; break;
case "november": monthNumber = 11; break;
case "december": monthNumber = 12; break;
default: monthNumber = 0; break; }
return monthNumber; }
public static void main(String[] args) {
// TODO code application logic here
String month = "August";
SwitchStaticMethod objSNM = new SwitchStaticMethod();
int returnedMonthNumber = objSNM.getMonthNumber(month);
if (returnedMonthNumber == 0) {
System.out.println("Invalid month");
} else {
System.out.println(returnedMonthNumber);
} // TODO c
}
}
Similarly, I can invoke static method without the help of an object as in the following code:
public class SwitchStaticMethod2 {
/**
* #param args the command line arguments
*/
public static int getMonthNumber(String month) {
int monthNumber = 0;
if (month == null) {
return monthNumber; }
switch (month.toLowerCase()) {
case "january": monthNumber = 1; break;
case "february": monthNumber = 2; break;
case "march": monthNumber = 3; break;
case "april": monthNumber = 4; break;
case "may": monthNumber = 5; break;
case "june": monthNumber = 6; break;
case "july": monthNumber = 7; break;
case "august": monthNumber = 8; break;
case "september": monthNumber = 9; break;
case "october": monthNumber = 10; break;
case "november": monthNumber = 11; break;
case "december": monthNumber = 12; break;
default: monthNumber = 0; break; }
return monthNumber; }
public static void main(String[] args) {
// TODO code application logic here
String month = "August";
//SwitchStaticMethod2 objSNM = new SwitchStaticMethod2();
int returnedMonthNumber = SwitchStaticMethod2.getMonthNumber(month);
if (returnedMonthNumber == 0) {
System.out.println("Invalid month");
} else {
System.out.println(returnedMonthNumber);
} // TODO c
}
}
I feel this is a confusion. Both are giving same answer but which is the correct way of invoking static methods?
It is better to call the static method using the class, just like you did in the second version.
They will both work the same way, but calling a static method from the object will have no gain, since they can't be inherited. It will only cause confusion in the end.
It is always better to invoke static methods by the class name. Why to unnecessarily create an object if your requirement is fulfilled without creating an object.
public static double eMED(int emo) {
double result = 0;
if (emo >= 40 && emo < 60) {
result = upFunc(emo, 40, 60);
} else if (emo > 60 && emo <= 80) {
result = downFunc(emo, 60, 80);
} else if (emo == 60) {
result = 1;
}
return result;
}
Your code already looks short. You can't make it shorter. You can use ternary operator but the code becomes barely readable.
return emo == 60 ? 1
: emo >= 40 && emo < 60 ? upFunc(emo, 40, 60)
: emo > 60 && emo <= 80 ? downFunc(emo, 60, 80)
: 0;
use case it is short
example :
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}
After I run this codes, it says BUILD SUCCESSFUL and it stops. What should I do to make it repeat to ask another input with same case? I did read tutorial on several web but I'm not helped by those.
Here's the code:
import java.util.Scanner;
public class SwitchTry {
public static void main(String[] args) {
int mth;
String mthString;
Scanner scanner = new Scanner(System.in);
mth =scanner.nextInt();
switch (mth) {
case 1: mthString = "January";
break;
case 2: mthString = "February";
break;
case 3: mthString = "March";
break;
case 4: mthString = "April";
break;
case 5: mthString = "May";
break;
case 6: mthString = "June";
break;
case 7: mthString = "July";
break;
case 8: mthString = "August";
break;
case 9: mthString = "September";
break;
case 10: mthString = "October";
break;
case 11: mthString = "November";
break;
case 12: mthString = "December";
break;
default: mthString = "Error";
break;
}
System.out.println(mthString);
}
}
I appreciate any help that you can provide.
I would try using a while loop to encase the switch. So it keeps asking your input.
import java.util.Scanner;
public class SwitchTry {
public static void main(String[] args) {
while(true){
int mth;
String mthString;
Scanner scanner = new Scanner(System.in);
mth =scanner.nextInt();
switch (mth) {
case 1: mthString = "January";
break;
case 2: mthString = "February";
break;
case 3: mthString = "March";
break;
case 4: mthString = "April";
break;
case 5: mthString = "May";
break;
case 6: mthString = "June";
break;
case 7: mthString = "July";
break;
case 8: mthString = "August";
break;
case 9: mthString = "September";
break;
case 10: mthString = "October";
break;
case 11: mthString = "November";
break;
case 12: mthString = "December";
break;
default: mthString = "Error";
break;
}
System.out.println(mthString);
}
}
}
As suggested by MadProgarmmer do-while loop is the perfect use-case for such scenarios. You need to wrap your input line plus switch cases and print statement inside the do-while loop as follow:
do {
//so you know that terminal is asking for input
System.out.println("Input: ");
mth =scanner.nextInt();
//copy/paste your switch case with last System.out.println() here
} while (true);
I am worried about having execution that never ends. Maybe you want to end the loop when the user supplies -1, so change the while(true) to while (mth != -1) and that should be your termination input.
I am doing a POC on Java 7 new features. I have code to use String in switch statement and it works. I want to make it work in case insensitive also. Is there a way to check out with ignoreCase on String?
package com.java.j7;
public class Test {
final private String _NEW ="NEW";
final private String _PENDING = "PENDING";
final private String _CLOSED = "CLOSED";
final private String _REJECTED ="REJECTED";
public static void main(String... strings){
Test j = new Test();
j.processItem("new");
j.processItem("pending");
j.processItem("closed");
j.processItem("rejected");
}
void processItem(String s){
switch (s) {
case _NEW:
System.out.println("Matched to new");
break;
case _PENDING:
System.out.println("Matched to pending");
break;
case _CLOSED:
System.out.println("Matched to closed");
break;
case _REJECTED:
System.out.println("Matched to rejected");
break;
default:
System.out.println("Not matching any more");
break;
}
}
}
no, but you could switch on s.toUpperCase(). so:
switch (s.toUpperCase()) {
//same as before
}
and while we're nitpicking, you better upper-case things in the english locale to avoid issues with turkish
using String in switch Example from oracle docs Using Strings in switch Statements
public class StringSwitchDemo {
public static int getMonthNumber(String month) {
int monthNumber = 0;
if (month == null) {
return monthNumber;
}
switch (month.toLowerCase()) {
case "january":
monthNumber = 1;
break;
case "february":
monthNumber = 2;
break;
case "march":
monthNumber = 3;
break;
case "april":
monthNumber = 4;
break;
case "may":
monthNumber = 5;
break;
case "june":
monthNumber = 6;
break;
case "july":
monthNumber = 7;
break;
case "august":
monthNumber = 8;
break;
case "september":
monthNumber = 9;
break;
case "october":
monthNumber = 10;
break;
case "november":
monthNumber = 11;
break;
case "december":
monthNumber = 12;
break;
default:
monthNumber = 0;
break;
}
return monthNumber;
}
public static void main(String[] args) {
String month = "August";
int returnedMonthNumber =
StringSwitchDemo.getMonthNumber(month);
if (returnedMonthNumber == 0) {
System.out.println("Invalid month");
} else {
System.out.println(returnedMonthNumber);
}
}
}
From oracle docs switch with string
The String in the switch expression is compared with the expressions associated with each case label as if the String#equals method were being used.
You can use
switch(s.toUpperCase()){
...
.....
}
See also
String#toUpperCase
What's wrong with my code? For some reason I keep on getting the day off by one day? For example, today is the 26th of 2013 and it's a Tuesday, but the program tells me it's a Wednesday. I am using Zeller's Congruence algorithm.
import javax.swing.JOptionPane;
public class zeller {
public static void main(String[] args) {
String yearString = JOptionPane.showInputDialog("Enter the year:");
int year = Integer.parseInt(yearString);
String monthString = JOptionPane.showInputDialog("Enter the month (3-12)(January and Feburary are 13 and 14):");
int month = Integer.parseInt(monthString);
String dayString = JOptionPane.showInputDialog("Enter the day 1-31: ");
int day = Integer.parseInt(dayString);
switch (month) {
case 13: monthString = "January";
break;
case 14: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
int j = year / 100;
int k = year % 100 ;
double h = (day + ((26*(month + 1)) / 10) + k + (k / 4) +(j / 4) + (5 * j)) % 7;
int h1 = (int)h;
switch (h1) {
case 0: dayString = "Saturday";
break;
case 1: dayString = "Sunday";
break;
case 2: dayString = "Monday";
break;
case 3: dayString = "Tuesday";
break;
case 4: dayString = "Wednesday";
break;
case 5: dayString = "Thursday";
break;
case 6: dayString = "Friday";
break;
default: monthString = "Invalid month";
break;
}
System.out.println("Day of the week is: " + dayString);
}
}
The months Jan and Feb are counted as the 13th and 14th month of the previous year, so
if you want the user to enter the actual months of 1 or 2 for Jan or Feb you could add code like this:
if (month == 1 ){
month = 13;
year -=1;
}
else if (month == 2) {
month = 14;
year -=1;
}
You need to modify the year if it is January or February. Explanation on wikipedia:
One can readily see that, in a given year, March 1 (if that is a
Saturday, then March 2) is a good test date; and that, in a given
century, the best test year is that which is a multiple of 100. Zeller
used decimal arithmetic, and found it convenient to use J and K in
representing the year. But when using a computer, it is simpler to
handle the modified year Y, which is Y - 1 during January and
February:
I modified your code as follows and it works:
import javax.swing.JOptionPane;
public class zeller {
public static void main(final String[] args) {
String yearString = JOptionPane.showInputDialog("Enter the year:");
int year = Integer.parseInt(yearString);
String monthString =
JOptionPane.showInputDialog("Enter the month (3-12)(January and Feburary are 13 and 14):");
int month = Integer.parseInt(monthString);
String dayString = JOptionPane.showInputDialog("Enter the day 1-31: ");
int day = Integer.parseInt(dayString);
switch (month) {
case 14:
year--;
monthString = "January";
break;
case 13:
year--;
monthString = "February";
break;
case 3:
monthString = "March";
break;
case 4:
monthString = "April";
break;
case 5:
monthString = "May";
break;
case 6:
monthString = "June";
break;
case 7:
monthString = "July";
break;
case 8:
monthString = "August";
break;
case 9:
monthString = "September";
break;
case 10:
monthString = "October";
break;
case 11:
monthString = "November";
break;
case 12:
monthString = "December";
break;
default:
monthString = "Invalid month";
break;
}
int j = year / 100;
int k = year % 100;
double h = (day + ((13 * (month + 1) / 5)) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
int h1 = (int) h;
switch (h1) {
case 0:
dayString = "Saturday";
break;
case 1:
dayString = "Sunday";
break;
case 2:
dayString = "Monday";
break;
case 3:
dayString = "Tuesday";
break;
case 4:
dayString = "Wednesday";
break;
case 5:
dayString = "Thursday";
break;
case 6:
dayString = "Friday";
break;
default:
monthString = "Invalid month";
break;
}
System.out.println("Day of the week is: " + dayString);
}
}
From Wikipedia,
In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is February 2, 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (02/14/2009 in DD/MM/YYYY format).
So there is technically nothing wrong with your code; putting in 02/26/2013 (today) as the 26th day of the 14th month of 2013 is actually calculating the day of the week of 02/26/2014.