I am writing a small program to find the day of the week using gregorian math. The following code always outputs a zero into my switch statement resulting in the same output each time.
This is my code:
iSum = (CentCode + iYear + (iYear /4) + MonthCode + iDay);
iOutput %= iSum;
switch (iOutput)
{
case 0:
sDay = "sunday";
break;
case 1:
sDay = "monday";
break;
case 2:
sDay = "tuesday";
break;
case 3:
sDay = "wednesday";
break;
case 4:
sDay = "thursday";
break;
case 5:
sDay = "friday";
break;
case 6:
sDay = "saturday";
break;
}
iOutput %= iSum;
is the short form of
iOutput = iOutput % iSum;
Which is not what you want. I guess you want something like
iOutput = iSum % 7;
Related
The homework for my Java class instructs us to take an integer input from the user (up to 3999) and convert it into a Roman Numeral. The trouble I am having with the code is that when I input the integer while testing, it sets the integer to 0 and the Roman Numeral prints out as null. This could be a result of declaring the variables inside an object but im unsure at this point.
public class RomanNumerals
{
// instance variables
int input;
String romanNum;
/**
* Object that records the input from a user.
*/
public RomanNumerals()
{
int input=0;
String romanNum="";
//System.out.println("Input a number to get a Roman Numeral");
//int input = integer.nextInt();
}
public int Scanner()
{
Scanner integer = new Scanner(System.in);
System.out.println("Input a number to get a Roman Numeral: ");
int input = integer.nextInt();
return input;
}
/**
* Object that takes the input from the user and determines what roman numerals are appropriate.
*/
public String Int2Roman()
{
if (input < 1 || input > 3999)
{
System.out.println("Please input a number between 1 and 3999.");
}
while (input >= 1000)
{
romanNum = romanNum + "M";
input = input - 1000;
}
while (input >= 900)
{
romanNum = romanNum + "CM";
input = input - 900;
}
while (input >= 500)
{
romanNum = romanNum + "S";
input = input - 500;
}
while (input >= 400)
{
romanNum = romanNum + "CS";
input = input - 1000;
}
while (input >= 100)
{
romanNum = romanNum + "C";
input = input - 100;
}
while (input >= 90)
{
romanNum = romanNum + "XC";
input = input - 1000;
}
while (input >= 50)
{
romanNum = romanNum + "L";
input = input - 1000;
}
while (input >= 40)
{
romanNum = romanNum + "XL";
input = input - 40;
}
while (input >= 10)
{
romanNum = romanNum + "X";
input = input - 10;
}
while (input >= 9)
{
romanNum = romanNum + "IX";
input = input - 9;
}
while (input >= 5)
{
romanNum = romanNum + "V";
input = input - 5;
}
while (input >= 4)
{
romanNum = romanNum + "IV";
input = input - 4;
}
while (input >= 1)
{
romanNum = romanNum + "I";
input = input - 5;
}
System.out.println("The Roman Numeral of " + input + " is " + romanNum);
return romanNum;
}
public static void main(String[] args)
{
RomanNumerals a = new RomanNumerals();
a.Scanner();
a.Int2Roman();
}
}
Your Scanner method does not store the keyboard input in the field (instance variable) input but in a local variable input.
Replace
int input = integer.nextInt(); //this declares a new local variable
with
input = integer.nextInt(); //uses the existing field
try this to see if works , works for me, simple , basic , pretty straight forward
public static void main(String[] args) {
//INPUNT INTEGER VALUE
// OUTPUT STRING VALUE (SYSTEM CONSOLE OUTPUT
//**********************************************************************************//
int _inValue = 117; //int parameter value
//**********************************************************************************//
//check if the number is between 1 and 3999
if (_inValue < 1){
System.out.println("integer can't be less than 1");
return;
}
if (_inValue > 3999){
System.out.println("integer can't be more than 3999");
return;
}
//convert the value in array to work by separate numbers
char[] arg = String.valueOf(_inValue).toCharArray();
//count the number of characters to use a case condition or length of the integer in value
int _count = Integer.toString(_inValue).length();
// output variable
String _output = "";
//case condition base in the length of the integer value
switch (_count){
case 1: // case base in one figure (1)
switch (_inValue){
case 1:
_output = "I";
break;
case 2:
_output = "II";
break;
case 3:
_output = "III";
break;
case 4:
_output = "IV";
break;
case 5:
_output = "V";
break;
case 6:
_output = "VI";
break;
case 7:
_output = "VII";
break;
case 8:
_output = "VIII";
break;
case 9:
_output = "IX";
break;
}
break;
case 2: //case base in two figures (10)
switch (Character.getNumericValue(arg[0])){//base in the first character of the integer value lets use another case condition
case 1:
_output = "X";
break;
case 2:
_output = "XX";
break;
case 3:
_output = "XXX";
break;
case 4:
_output = "XL";
break;
case 5:
_output = "L";
break;
case 6:
_output = "LX";
break;
case 7:
_output = "LXX";
break;
case 8:
_output = "LXXX";
break;
case 9:
_output = "XC";
break;
}
_output += _getValue10(Character.getNumericValue(arg[1])); //lets call this function to get the other two figures or number to complete the integer amount and build the roman value in string
break;
case 3: //case base in three figures (100)
switch (Character.getNumericValue(arg[0])){ //base in the first character of the integer value lets use another case condition
case 1:
_output = "C";
break;
case 2:
_output = "CC";
break;
case 3:
_output = "CCC";
break;
case 4:
_output = "CD";
break;
case 5:
_output = "D";
break;
case 6:
_output = "DC";
break;
case 7:
_output = "DCC";
break;
case 8:
_output = "DCCC";
break;
case 9:
_output = "CM";
break;
}
_output += _getValue100(Character.getNumericValue(arg[1]),Character.getNumericValue(arg[2])); //lets call this function to get the other two figures or number to complete the integer amount and build the roman value in string
break;
case 4: // case base in 4 figures (1000)
if(_inValue != 1000){ // validate the value if its different of a 1000
// use a cycle to concat the string base of the number of the integer in value
for (int i = 0; i < Character.getNumericValue(arg[0]);i++ ){
_output += "M";
}
}else{
// if its a 1000 use this one and that's it
_output += "M";
}
_output += _getValue1000(Character.getNumericValue(arg[1]),Character.getNumericValue(arg[2]),Character.getNumericValue(arg[3])); //lets call this function to get the other three figures or number to complete the integer amount and build the roman value in string
break;
}
System.out.println("the integer number is: " + _inValue);
System.out.println("the roman number is: " + _output);
}
///method who return the other three values
private static String _getValue1000(int valueOne, int valueTwo, int valueThree){
String _output = "";
if(valueOne != 0){
switch (valueOne){
case 1:
_output += "C";
break;
case 2:
_output += "CC";
break;
case 3:
_output += "CCC";
break;
case 4:
_output += "CD";
break;
case 5:
_output += "D";
break;
case 6:
_output += "DC";
break;
case 7:
_output += "DCC";
break;
case 8:
_output += "DCCC";
break;
case 9:
_output += "CM";
break;
}
}
if(valueTwo != 0){
switch (valueTwo){
case 1:
_output += "X";
break;
case 2:
_output += "XX";
break;
case 3:
_output += "XXX";
break;
case 4:
_output += "XL";
break;
case 5:
_output += "L";
break;
case 6:
_output += "LX";
break;
case 7:
_output += "LXX";
break;
case 8:
_output += "LXXX";
break;
case 9:
_output += "XC";
break;
}
}
if(valueThree != 0){
switch (valueThree){
case 1:
_output += "I";
break;
case 2:
_output += "II";
break;
case 3:
_output += "III";
break;
case 4:
_output += "IV";
break;
case 5:
_output += "V";
break;
case 6:
_output += "VI";
break;
case 7:
_output += "VII";
break;
case 8:
_output += "VIII";
break;
case 9:
_output += "IX";
break;
}
}
return _output;
}
/// method who return the other two values
private static String _getValue100(int valueOne, int valueTwo){
String _output = "";
if(valueOne != 0){
switch (valueOne){
case 1:
_output += "X";
break;
case 2:
_output += "XX";
break;
case 3:
_output += "XXX";
break;
case 4:
_output += "XL";
break;
case 5:
_output += "L";
break;
case 6:
_output += "LX";
break;
case 7:
_output += "LXX";
break;
case 8:
_output += "LXXX";
break;
case 9:
_output += "XC";
break;
}
}
if(valueTwo != 0){
switch (valueTwo){
case 1:
_output += "I";
break;
case 2:
_output += "II";
break;
case 3:
_output += "III";
break;
case 4:
_output += "IV";
break;
case 5:
_output += "V";
break;
case 6:
_output += "VI";
break;
case 7:
_output += "VII";
break;
case 8:
_output += "VIII";
break;
case 9:
_output += "IX";
break;
}
}
return _output;
}
/// method who return one other value
private static String _getValue10(int valueOne){
String _output = "";
if(valueOne != 0){
switch (valueOne){
case 1:
_output += "I";
break;
case 2:
_output += "II";
break;
case 3:
_output += "III";
break;
case 4:
_output += "IV";
break;
case 5:
_output += "V";
break;
case 6:
_output += "VI";
break;
case 7:
_output += "VII";
break;
case 8:
_output += "VIII";
break;
case 9:
_output += "IX";
break;
}
}
return _output;
}
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);
}
}
I am making code for my CPSC class and I have to print up to a case number that is set for the int. When I enter "2", the code prints "two potato" eight times instead of "One potato, two potato".
Here is my code I have:
public class Potato {
public Potato() {
}
public void count(int c) {
for (int i = 0; i < 8; i++) {
switch (c % 8) {
case 1: System.out.println("One potato"); break;
case 2: System.out.println("two potato"); break;
case 3: System.out.println("three potato"); break;
case 4: System.out.println("four..."); break;
case 5: System.out.println("five potato"); break;
case 6: System.out.println("six potato"); break;
case 7: System.out.println("seven potato"); break;
case 8: System.out.println("more!"); break;
default: break;
}
}
}
}
I think my problem is my for-loop, not too sure though, since I am here asking for help. Thanks in advance!
It is because c % 8 (2 % 8 = 2), so in every loop it will execute case 2 and prints two potato. You could use i % 8 instead.
This should do the trick. As others have said, you should use i%8. However, for this to work you should also start with i=1 since 0%8 = 0. And your case 8 should be changed to case 0.
for (int i = 1; i <= c; i++) {
switch (i % 8) {
case 1: System.out.println("One potato"); break;
case 2: System.out.println("two potato"); break;
case 3: System.out.println("three potato"); break;
case 4: System.out.println("four..."); break;
case 5: System.out.println("five potato"); break;
case 6: System.out.println("six potato"); break;
case 7: System.out.println("seven potato"); break;
case 0: System.out.println("more!"); break;
default: break;
}
}
This code is... peculiar:
The main problem is that you are not using the i variable that is incremented at the for loop. Instead, you are using c variable:
switch (c % 8)
Should be:
switch (i % 8)
You don't need the case 8 since there is no way that i % 8 result in 8. And, looks like you don't need c variable at all.
I have a double split up into an int array, however the problem I am facing, is that I need it to fill in from the back, like this:
int[] arrayA = new int[5];
int[] arrayB = new int[5];
x = 23456.08;
//code here
//what im left with:
arrayA [0,2,3,4,5,6];
arrayB [0,8];
Heres how im cutting up the double:
public static void main(System args[])
{
Scanner input = new Scanner(System.in);
String answer = "";
int count = 0;
int thatone = 0;
int[] splitD = new int[5]; //main num
int[] splitDec = new int[1]; //decimal
//Enter the Number
System.out.print("Enter a number to convert: ");
double num = input.nextDouble();
// convert number to String
String convert = num + "";
// split the number
String[] split = convert.split("\\.");
String firstPart = split[0];
char[] charArray1 = firstPart.toCharArray();
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
// the decimal part
if (split.length > 1)
{
String secondPart = split[1];
char[] charArray2 = secondPart.toCharArray();
splitDec = new int[charArray2.length];
for (int i = 0; i < charArray2.length; i++)
{
// convert char to int
splitDec[i] = Character.getNumericValue(charArray2[i]);
}
}
for(int i =0; i<count;i++)
{
if(i ==0) // x00000.00 or 000x00.00
{
if(splitD[0] != "0")
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else
{
answer+= "";
}
}
else if(i ==1)//this goes with i =2 //0x0000
{
if(splitD[i] == 1)
{
switch (splitD[i+1])
{
case 9: answer+="Nineteen"; break;
case 8: answer+="Eighteen"; break;
case 7: answer+="Seventeen"; break;
case 6: answer+="Sixteen"; break;
case 5: answer+="Fifteen"; break;
case 4: answer+="Fourteen"; break;
case 3: answer+="Thirteen"; break;
case 2: answer+="Twelve"; break;
case 1: answer+="ten"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
thatone = 1;
}
else
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirty"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
}
}
else if(i == 2) //00x000
{
if(thatone ==0)
{
switch (splitD[i])
{
case 9: answer+=" Nine"; break;
case 8: answer+=" Eight"; break;
case 7: answer+=" Seven"; break;
case 6: answer+=" Six"; break;
case 5: answer+=" Five"; break;
case 4: answer+=" Four"; break;
case 3: answer+=" Three"; break;
case 2: answer+=" Two"; break;
case 1: answer+=" One"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
}
else
{
}
}
else if(i ==3)
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else if(i ==4) //0000x0
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirdy"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
answer+= " ";
}
else if(i ==5) //00000x
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
}
}
if(splitDec[0] == 0)
{
answer += " and 00/100 Dollars";
}
else if(splitDec[1] == 0)
{
answer += " and " +splitDec[0] + "0/100 Dollars";
}
else
{
answer += " and " +splitDec[0] +splitDec[1] +" /100 Dollars";
}
System.out.println(answer);
}
}
What should I do to make the array add 0 in the appropriate places?
such as if I typed in 54.00 I would get:
int[] SplitD = {0,0,0,0,5,4};
Thanks!
Instead of re-initializing the array:
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
Use the existing (with length = 5), iterating from the back:
if(charArray1.length > 5) // heavy hard-code, would prefer something better
throw new IllegalArgumentException("Maximum 5 digits, on both decimal sides.");
// watch out of ArrayIndexOutOfBoundsException
for (int i = charArray1.length - 1, j = splitD.length -1 ;
i >=0 && j >=0; i--, j--)
{
// convert char to int
splitD[j] = Character.getNumericValue(charArray1[i]);
count++;
}
Recall, int arrays are filled with zeros, at initialization.
Think creating an array of String, one String for each character (including the decimal point), would be a better approach:
String[] chars = String.valueOf(num).split("(?<=.)");
Job done.
Edit:
To use a switch, don't even split:
for (byte b : String.valueOf(num).getBytes()) {
switch(b) {
case '.':
case '1':
}
}
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.