Working with week days and finding future days - java

I am trying to find means to compute future weekdays, given two inputs:
Current weekday (range from 0-6 where 0 is Sunday).
How many counts to perform from current weekday (any number)?
Here if the user previously starts from current weekday = 3, and
counts=7.
Then I expect it to come back to 3, similarly with 14 or 21.
How to generalize this to make the counts within this fixed range
0-6 without pulling out of it?
I've already done some code which is posted below,
public class ThoseDays {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.print("Enter number between 0-6 : ");
int startFromHere = obj.nextInt();
System.out.print("Enter number to count position from " + startFromHere + " : ");
int rotateFromHere = obj.nextInt();
System.out.print( startFromHere + rotateFromHere);
obj.close();
}
}
Actual result:
> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 10
Expected result:
> Enter the number between 0-6: 3
> Enter the number to count position from 3: 7
> 3

Hi i suggest you just use a modulo to rotate the days after they reach 7. Another tutorial here
public class ThoseDays {
public static void main(String[] args) {
//Scanner implements AutoCloseable
//https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
try (Scanner obj = new Scanner(System.in)) {
System.out.print("Enter number between 0-6 : ");
int startFromHere = obj.nextInt();
System.out.print("Enter number to count position from " + startFromHere + " : ");
int rotateFromHere = obj.nextInt();
int absoluteNumber = startFromHere + rotateFromHere;
System.out.println(absoluteNumber);
int rotatedNumber = absoluteNumber % 7;
System.out.println(rotatedNumber);
}
}
}

code:
import java.util.*;
public class ThoseDays {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.println("note : 0:sun 1-6:mon-saturday");
System.out.println("Enter the number between 0-6: ");// 0:sun 1-6:mon-saturday
int startFromHere = obj.nextInt();
System.out.println("Enter the number to count position from " + startFromHere+ ": ");
int rotateFromHere =obj.nextInt();
if(rotateFromHere%7==0)
{
System.out.println(startFromHere);
}
if(rotateFromHere%7!=0)
{
int dayOfWeek=(startFromHere+(rotateFromHere%7));
if(dayOfWeek>=7)
{
System.out.println((dayOfWeek%7));
}
else
{
System.out.print(dayOfWeek);
}
}
obj.close();
}
}
try this code by changing conditions and using modulo I'm getting all correct results
output:
startFromHere = 3
rotate fromHere = 7 or 14 or 21 or multiple of 7
gives the same date as the start date
if rotate date is > start date
for ex:
startFromHere = 3 //wednesday
rotateFromHere = 11
output will be : 0 which means sunday
check this code and give me a rating if useful thanks.

Related

Reverse integer including 0's in java

I have to do a program that returns the reverse of a number that is input by a user, event the numbers that start and finish with 0 (ex. 00040, it would print 04000)
I was able to do the reverse of the number, but it doesn't print out the 0's and I can't use String variables, just long variables or integers.
Here is my code:
import java.util.Scanner;
public class Assignment_2_Question_2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Welcome to Our Reversing Number Program");
System.out.println("-----------------------------------------");
System.out.println();
System.out.println("Enter a number with at most 10 digits:");
long number = keyboard.nextInt();
long nbDigits = String.valueOf(number).length();
System.out.println("Number of digits is " + nbDigits);
System.out.print("Reverse of " + number + " is ");
long revNumber = 0;
while (number > 0){
long digit = number % 10;
if (digit == 0){ // The teacher told me to add this
nb0 ++; // need to not take into account the 0's inside the number
}
revNumber = revNumber * 10 + digit;
number = number/10;
}
for (int i = 0; i < nb0; i++) { // This will print the number of 0's counted by the if statement and print them out.
System.out.println("0");
}
System.out.println(revNumber);
String answer;
do{
System.out.println("Do you want to try another number? (yes to repeat, no to stop)");
answer = keyboard.next();
if (answer.equalsIgnoreCase("yes")){
System.out.println("Enter a number with at most 10 digits:");
long otherNumber = keyboard.nextInt();
long nbrDigits = String.valueOf(otherNumber).length();
System.out.println("Number of digits is " + nbrDigits);
System.out.print("Reverse of " + otherNumber + " is ");
long reversedNumber = 0;
while (otherNumber != 0){
reversedNumber = reversedNumber * 10 + otherNumber%10;
otherNumber = otherNumber/10;
}
System.out.println(reversedNumber);
}
else
System.out.println("Thanks and have a great day!");
}while(answer.equalsIgnoreCase("yes")&& !answer.equalsIgnoreCase("no"));
}
}
Can someone help me? Thank you
Probably not what is intended but clearly (based on problem statement) you must see all digits entered (to include leading 0's) otherwise it is an "impossible solution" - and you state you cannot receive input as a String...
So this snippet reads one digit at a time where each digit is received as an int:
Scanner reader = new Scanner(System.in);
reader.useDelimiter(""); // empty string
System.out.print("Enter number: ");
while (!reader.hasNextInt()) reader.next();
int aDigit;
int cnt = 0;
while (reader.hasNextInt()) {
aDigit = reader.nextInt();
System.out.println("digit("+ ++cnt + ") "+aDigit);
}
System.out.println("Done");
Prints (assume user enter 012 (enter)):
Enter number: digit(1) 0
digit(2) 1
digit(3) 2
Done
You naturally have more work to do with this but at least you have all user entered digits (including leading zeros).
You can use buffer reader;
Like this given code And if you want to do some arithmetic operations in the numbers then you can convert it into int using parseInt method.:-
import java.util.Scanner;
import java.lang.*;
class Main {
public static void main(String args[])
{
System.out.println("ENTER NUM");
Scanner SC = new Scanner(System.in);
String INP = SC.nextLine();
StringBuffer SB = new StringBuffer(INP);
SB.reverse() ;
System.out.println(SB);
}
}

Error message saying that the array has been exceeded

I was wondering if you guys can give me pointers on how to fix my code. I am trying to put an out an error message that the size of the array has been exceeded when you entered too many numbers. I know I wrote two posts about this, and many people told me to be specific and do it by myself and I decided to do this program by myself instead of asking for help. So I wrote the code, and it came out nice, but how would I do it when it says, "Enter the number 11:" then I enter a number, and it says it has been exceeded and prints out the 10 arrays on the next line.
Input:
import java.util.Scanner;
public class FunWithArrays
{
public static void main(String[] args)
{
final int ARRAY_SIZE = 11; // Size of the array
// Create an array.
int[] numbers = new int[ARRAY_SIZE];
// Pass the array to the getValues method.
getValues(numbers);
System.out.println("Here are the " + "numbers that you entered:");
// Pass the array to the showArray method.
showArray(numbers);
}
public static void getValues(int[] array)
{
// Create a Scanner objects for keyboard input.
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a series of " + array.length + " numbers.");
// Read the values into the array
for (int index = 0; index < array.length; index++)
{
// To tell users if they exceeded over the amount
if (index > 9)
{
System.out.print("You exceeded the amount " + " ");
}
else
{
System.out.print("Enter the number " + (index + 1) + ": ");
array[index] = keyboard.nextInt();
}
}
}
public static void showArray(int[] array)
{
// Display the array elements.
for (int index = 0; index < array.length; index++)
System.out.print(array[index] + " ");
}
}
output:
Enter a series of 11 numbers.
Enter the number 1: 3321
Enter the number 2: 3214
Enter the number 3: 213
Enter the number 4: 21
Enter the number 5: 321
Enter the number 6: 321
Enter the number 7: 3
Enter the number 8: 213
Enter the number 9: 232
Enter the number 10: 321
You exceeded the amount Here are the numbers that you entered:
3321 3214 213 21 321 321 3 213 232 321 0
OK, here is the code to your need, bare in mind that 11th element is not put into an array at all.
public static void main(String[] args) {
final int ARRAY_SIZE = 11; // Size of the array
// Create an array.
int[] numbers = new int[ARRAY_SIZE];
// Pass the array to the getValues method.
getValues(numbers);
System.out.println("Here are the " + "numbers that you entered:");
// Pass the array to the showArray method.
showArray(numbers);
}
public static void getValues(int[] array) {
// Create a Scanner objects for keyboard input.
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a series of " + array.length + " numbers.");
// Read the values into the array
for (int index = 0; index < array.length; index++) {
// To tell users if they exceeded over the amount
if (index >= 10) {
System.out.print("Enter the number " + (index + 1) + ": ");
array[index] = keyboard.nextInt();
System.out.println("\nYou exceeded the amount " + " ");
} else {
System.out.print("Enter the number " + (index + 1) + ": ");
array[index] = keyboard.nextInt();
}
}
}
public static void showArray(int[] array) {
// Display the array elements.
for (int index = 0; index < array.length-1; index++) {
System.out.print(array[index] + " ");
}
}
}
And I have no idea, why do you want it like this.

Listing multiples of user-inputted numbers

The task is to "Write a program that displays a user-indicated number of multiples for an integer entered by the user."
I suppose I do not need a completely direct answer (although I do want to know the methods/formula to use), as I want to use this as a learning experience in order to do and learn from the task myself. I really want to know about the process and which methods to use, along with finding a formula. :||
I'm really not sure how to write a code that displays a user-inputted number of a user-inputted integer. The hardest part seems to be writing the loop formula. Not sure where to start.
So far, I have:
import java.util.Scanner;
public class MultipleLooping
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
\\just stuff to base my code off of
int integer;
int numberMultiples;
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");
\\pretty much everything from here on out.. I'm not sure what to really do.
int n = integer;
int result = (integer * (numberMultiples));
while (result > 0){}
System.out.print(result);
}
} \\at the moment this code doesn't seem to have any running errors
I'm really not sure how to write a code that displays a user-inputted number of a user-inputted integer. The hardest part seems to be writing the loop formula. Not sure where to start.
NEW QUESTION
I need to loop my program as well. (By asking a question to the user first.) Mines isn't working, as it just keeps looping only the integer loop and doesn't let me type yes/no.
import java.util.Scanner;
public class MultipleLoops
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int integer, numberMultiples;
String repeat = "yes";
while (repeat != "no")
{
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");
for (int i=1; i<=numberMultiples; i++){
System.out.println(integer + " * " + i + " = " + i*integer );
}
System.out.println("Would you like to do this again? Enter yes or no: ");
repeat = keyboard.nextLine();
}
}
}
Ok so you need to understand the problem first to know how to solve it
x = First input
n = Second input
you need to calculate n multiple of x
example with x = 3 and n = 10
To calculate 10 multiple of 3 we need to do :
1st multiple = x*1
2nd multiple = x*2
3rd multiple = x*3
...
n multiple = x*n
you can notice that these operations can be replaced by one for loop (notice first and last character of every line, it can be index of your loop )
Back to java :)
for (int i=1; i<=numberMultiples; i++){
System.out.println("Listing multiple N# " + i + " = "+ i*integer );
}
Replace your code with the following and try this code :
import java.util.Scanner;
public class MultipleLooping{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int integer,numberMultiples;
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
for (int i=1; i<=numberMultiples; i++){
System.out.println("Listing multiple N# " + i + " = "+ i*integer );
}
}
}
Enter an integer:
3
How many multiples of 3 would you like to know?
7
Listing multiple N# 1 = 3
Listing multiple N# 2 = 6
Listing multiple N# 3 = 9
Listing multiple N# 4 = 12
Listing multiple N# 5 = 15
Listing multiple N# 6 = 18
Listing multiple N# 7 = 21
Do you want like this ? Below is the code
package com.ge.cbm;
import java.util.Scanner;
public class MultipleLooping
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
//just stuff to base my code off of
int integer;
int firstEntered;
int numberMultiples;
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
firstEntered = integer;
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");
//pretty much everything from here on out.. I'm not sure what to really do.
for (int i=0;i<numberMultiples;i++){
integer=integer*firstEntered;
System.out.println(integer);
}
}
}
Output:
Enter an integer:
3
How many multiples of 3 would you like to know?
7
Listing the first 7 multiples of 3:
9
27
81
243
729
2187
6561
this should work
while(repeat.equals("yes"))
{
System.out.println("Enter an integer: ");
integer = keyboard.nextInt();
System.out.println("How many multiples of " + integer + " would you like to know?");
numberMultiples = keyboard.nextInt();
System.out.println("Listing the first " + numberMultiples + " multiples of " + integer + ": ");
for (int i=1; i<=numberMultiples; i++)
{
System.out.println(integer + " * " + i + " = " + i*integer );
}
System.out.println("Would you like to do this again? Enter yes or no: ");
repeat = keyboard.nextLine();
repeat = keyboard.nextLine();
}

how to get a number to add like 12 is 3

i cant get the code right to ask for a number like 12 and then say the sum of the number is 3 in java netbeans
i got this so far
public class Exercise2_6 {
public static void main(String[] args) {
java.util.Scanner in = new java.util.Scanner( System.in );
System.out.println("Enter a number between 0 and 1000");
// Enter a number between 0 and 1000
Scanner input = new Scanner( System.in );
int x = in.nextInt( );
System.out.println(" The sum of the digits is "n" ");
System.out.println("n" = (in.nextInt( ) /100)); //this give you first digit
System.out.println("n" = in.nextInt( )%100); //this gives a number representing the remaining two digits
}
}
and it gives me back
run:
Enter a number between 0 and 1000
12
The sum of the digits is
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement
at Exercise2_6.main(Exercise2_6.java:55)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)
Strings must be concatenated using the + operator.
Therefore, your statement of "n" = .... is not correct.
Replace
System.out.println("n" = (in.nextInt( ) /100)); //this give you first digit
System.out.println("n" = in.nextInt( )%100); //this gives a number representing the remaining two digits
with
System.out.println("n = " + in.nextInt()/100);
System.out.println("n = " + in.nextInt()%100);
However, the above statements will refer to TWO different ints, one for each time nextInt() is called. I don't know the purpose of your code but you should get into the practice of storing variables incase you need to use them again.
If you stored each int locally, for example
int n = in.nextInt();
you could then refer to it again later, for example by appending the above statements to
System.out.println("n = " + n/100); ....
This will probably not be the most efficient or 'correct' way of doing it, but I'm a complete noob myself. So, this is how I managed it. By using a while loop.
import java.util.Scanner;
public class SumOfDigits {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int i, a = 0, x = 0;
System.out.println("Enter a number between 0 and 1000: ");
i = input.nextInt();
while(i != 0) {
a = i % 10;
i = i / 10;
x = x + a;
}
System.out.println("The sum of the digits in your number is: " + x);
}
}

Why do the points for each player keep reseting?

My point tracker is reseting the points for both players to the original number every time I do it more than once.
I can't figure out why whenever I take away more life-points from either player, instead of using the previous number of life-points, it just resets to whatever I made the life-points start out on and goes form there.
EX:
player-1 has 100 life points.
I take away 1 life-point.
player-1 now has 99 life-points.
Now I do it again.
I take away 1 more life-point from player-1.
But now, instead of taking 1 away from the previous life-point count of 99, it takes 1 away from 100 again and gives me 99 a second time.
I can't tell where I made a mistake that keeps reseting the scores.
package yu.gi.oh.life.point.counter;
import java.util.Scanner;
public class YuGiOhLifePointCounter {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
double choice_1;
System.out.print("\nEnter the starting life points for player-1: ");
choice_1 = sc.nextDouble();
double storage_1;
storage_1 = choice_1;
double choice_2;
System.out.print("\nEnter the starting life points for player-2: ");
choice_2 = sc.nextDouble();
double storage_2;
storage_2 = choice_2;
double lp1;
System.out.print("\nEnter a 6 to change the life-points of either player: ");
lp1 = sc.nextDouble();
while (lp1 == 6) {
double choose_1_2;
System.out.print("\nEnter a 1 to change player-1's life-points, or enter a 2 to change player-2's life-points: ");
choose_1_2 = sc.nextDouble();
if (choose_1_2 == 1) {
double ch_1;
System.out.print("\nEnter the number subtracted from or added to player-1's life-points: ");
ch_1 = sc.nextDouble();
double c_1;
System.out.print("\nEnter a 1 to subtract this number from player-1's life-points, or enter a 2 to add this number to player-1's life-points: ");
c_1 = sc.nextDouble();
double display_1;
if (c_1 == 1) {
display_1 = storage_1 - ch_1;
System.out.println("\nPlayer-1's life-points are currently " + display_1);
}
if (c_1 == 2) {
display_1 = storage_1 + ch_1;
System.out.println("\nPlayer-1's life-points are currently " + display_1);
}
}
if (choose_1_2 == 2) {
double ch_2;
System.out.print("\nEnter the number subtracted from or added to player-2's life-points: ");
ch_2 = sc.nextDouble();
double c_2;
System.out.print("\nEnter a 1 to subtract this number from player-2's life-points, or enter a 2 to add this number to player-1's life-points: ");
c_2 = sc.nextDouble();
double display_2;
if (c_2 == 1) {
display_2 = storage_2 - ch_2;
System.out.println("\nPlayer-2's life-points are currently " + display_2);
}
if (c_2 == 2) {
display_2 = storage_2 + ch_2;
System.out.println("\nPlayer-2's life-points are currently " + display_2);
}
}
lp1 = 6;
}
}
}
You never change the storage_1/2 values.
A line like display_2 = storage_2 - ch_2; will keep calculating the difference with the original number of life points (100), instead of subtracting from the previously calculated amount. Try updating these storage_x values after you calculate the display_x values.

Categories