Hi guys So i have this problem.. i wanted to print a calendar, utilizing the Calendar API.
condition:
user inputs a Year, anInteger between 1000 - 9999.
then it will output the calendar of that year and 2 more succeeding years.
so if i input 2012 it will output the Calendar of 2012 , 2013 and 2014.
here is the catch it must be SIDEWAYS!
package lessons;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class lesson3 {
static String[] days = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
static int counter = 0;
public static void main(String[] args) {
GregorianCalendar gcal = new GregorianCalendar();
Scanner scanner = new Scanner(System.in);
System.out.print("Year: ");
int input = scanner.nextInt(); //month input
//System.out.print("YEAR (): ");
//int input2 = scanner.nextInt();
scanner.close();
int year = input;
gcal.set(Calendar.YEAR,input);
for(int monthCount = 0;monthCount<12;monthCount++){
gcal.set(Calendar.MONTH,monthCount); // month counter 0 - 11 producing jan to dec.
monthCheck(monthCount); // converting months to readable names
int x = gcal.get(Calendar.DAY_OF_WEEK);
int max = gcal.getActualMaximum(Calendar.DAY_OF_MONTH);
for(int i = 0;i<days.length;i++){
System.out.printf("%8s",days[i]);
}
System.out.println("");
for(int i = 0; i < x-1;i++){
System.out.printf("%8s","");
counter++;
if(counter%7==0){
System.out.println("");
}
}
for(int i = 1;i<=max;i++){
System.out.printf("%8s",i);
counter++;
if(counter%7==0){
System.out.println("");
}
}
System.out.println("");
//System.out.println(x);
}
}
static void monthCheck(int monthCount){
ArrayList monthsList = new ArrayList<String>();
String[] months = new DateFormatSymbols().getMonths();
for (int i = 0; i < months.length-1; i++) {
String monthArr = months[i];
monthsList.add(months[i]);
}
System.out.println(months[monthCount]);
}
}
This is what i can do with the code Above, click here!
this is what i am aiming for now. CLICK HERE!
Related
I want to have a new random number every time to print it, but it prints the same on. I tried so many thing, but I can't figure out what's wrong. Help me, please!
import 'dart:math';
int next_int() { return new Random().nextInt(100); }
void main()
{
List<int> list = [next_int(), next_int(), next_int()];
// expected new int each time but got the same one
for (var i = 0; i < 3; i++)
{
List<int> cur_list = new List.from(list);
print(cur_list[0]);
}
}
This code will work as you expect:
import 'dart:math';
int next_int() { return new Random().nextInt(100); }
void main()
{
List<int> list = [next_int(), next_int(), next_int()];
// expected new int each time but got the same one
for (var i = 0; i < 3; i++)
{
List<int> cur_list = new List.from(list);
print(cur_list[i]); // <= Use the index value stored in "i" instead of 0
}
}
import java.util.Scanner;
public class error{
private static class punto{
int x, y;
}
private static class lados{
punto inicio = new punto();
public lados(punto inicio1){
inicio=inicio1;
}
public punto getInicio(){
return inicio;
}
public void setInicio(punto inicio){
this.inicio = inicio;
}
}
public static void main(String[]args){
Scanner leer = new Scanner(System.in);
punto inicio = new punto();
lados arreglo[] = new lados[100];
for(int i=0; i<3; i++){
inicio.x = leer.nextInt();
inicio.y = leer.nextInt();
arreglo[i] = new lados(inicio);
}
for(int i=0; i<3; i++){
System.out.println(arreglo[i].getInicio().x);
System.out.println(arreglo[i].getInicio().y);
}
}
}
what am I doing wrong?
I want to storage ponits(x,y) in an index on an array
but just the last input is storaged in all index...
maybe there is other way to do what I want to do, if someone share it I'd love it.
input:
1
2
3
4
5
6
output:
5
6
5
6
5
6
expected output:
1
2
3
4
5
6
You use the same inicio instance in all lados instances you are creating :
for(int i=0; i<3; i++){
inicio.x = leer.nextInt();
inicio.y = leer.nextInt();
arreglo[i] = new lados(inicio);
}
You should create a new instance of punto for each lados if you want that the information to not be overwritten at each iteration.
Try that :
for(int i=0; i<3; i++){
inicio = new punto()
inicio.x = leer.nextInt();
inicio.y = leer.nextInt();
arreglo[i] = new lados(inicio);
}
By convention, classes should begin by an uppercase letter : Punto, Lados, etc...
Here's my code, i do not know what I'm doing wrong seriously. I tried many different things like taking the public modifier away from get. but I still get the same thing. This program is supposed to print out the Nth number line in the pascal triangle do to that I am using recursion a little bit.
import java.util.*;
public class Triangle{
private int lineNumber, count;
private int[] num;
public Triangle(){
lineNumber = 1;
}
public Triangle(int n){
set(n);
}
public void set(int n){
if(n < 1){
lineNumber = 1;
}
else{
lineNumber = n;
}
public int get()//Triangle.java:26: error: ';' expected //
{
return lineNumber;
}
private void pascal(int[] row){ //Triangle.java:30: error: illegal start of expression
if(count >= lineNumber){
return;
}
num = new int[row.length + 1];
num[0] = 1;
for(int i = 1; i < row.length; i++){
num[i] = row[i - 1] + row[i];
}
num[row.length] = 1;
count ++;
pascal(num);
return;
}
public int[] output(){
count = 1;
num = new int[count];
num[0] = 1;
pascal(num);
return num;
}
public static void main(String[] args){
int i,userNum;
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a number to get the nth line of"+
" Pascal's Triangle." );
userNum = input.nextInt();
PascalTriangle triangle = new Triangle(userNum);
int[] result = triangle.output();
System.out.println("\n Line " + triangle.get() + " of "
+ "Pascal's Triangle is ");
for(i = 0; i < result.length; i++){
System.out.println(result[i] + " ");
}
}
}
}
You need one more closing bracket after the else statement in the set() method.Try to add the closing } before
else{
lineNumber = n;
}
}
public int get()//Triangle.java:26: error: ';' expected //
My application have to calculate XIRR, but I cannot use excel, because it runs on Linux, can any one share the logic or java code to calculate XIRR without using excel.
as stated in the post : xirr-calculation in c#
According to XIRR function openoffice documentation (formula is same as in excel) you need to solve for XIRR variable in the following f(xirr) equation:
xirr equation
I just traduce the C# code in Java and it work well without any sup libraries. Even for date difference. But in production code, you will use something like apache common.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class XirrDate {
public static final double tol = 0.001;
public static double dateDiff(Date d1, Date d2){
long day = 24*60*60*1000;
return (d1.getTime() - d2.getTime())/day;
}
public static double f_xirr(double p, Date dt, Date dt0, double x) {
return p * Math.pow((1.0 + x), (dateDiff(dt0,dt) / 365.0));
}
public static double df_xirr(double p, Date dt, Date dt0, double x) {
return (1.0 / 365.0) * dateDiff(dt0,dt) * p * Math.pow((x + 1.0), ((dateDiff(dt0,dt) / 365.0) - 1.0));
}
public static double total_f_xirr(double[] payments, Date[] days, double x) {
double resf = 0.0;
for (int i = 0; i < payments.length; i++) {
resf = resf + f_xirr(payments[i], days[i], days[0], x);
}
return resf;
}
public static double total_df_xirr(double[] payments, Date[] days, double x) {
double resf = 0.0;
for (int i = 0; i < payments.length; i++) {
resf = resf + df_xirr(payments[i], days[i], days[0], x);
}
return resf;
}
public static double Newtons_method(double guess, double[] payments, Date[] days) {
double x0 = guess;
double x1 = 0.0;
double err = 1e+100;
while (err > tol) {
x1 = x0 - total_f_xirr(payments, days, x0) / total_df_xirr(payments, days, x0);
err = Math.abs(x1 - x0);
x0 = x1;
}
return x0;
}
private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
public static Date strToDate(String str){
try {
return sdf.parse(str);
} catch (ParseException ex) {
return null;
}
}
public static void main(String... args) {
double[] payments = {-1151250,232912,233123,233336,233551,233768}; // payments
Date[] days = {strToDate("11/11/2015"),strToDate("25/11/2015"),strToDate("25/12/2015"),strToDate("25/01/2016"),strToDate("25/02/2016"),strToDate("25/03/2016")}; // days of payment (as day of year)
double xirr = Newtons_method(0.1, payments, days);
System.out.println("XIRR value is " + xirr);
}}
You can use org.decampo library for java implementation.
Note: The code which has been provided by #Doukya is showing wrong answer if you cross check with excel sheet calculated value.
This is a program that prints out all the even numbers between any given integer.
import java.util.*;
public class Question1
{
private int i;
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Give me a number!");
int i = scanner.nextInt();
if ((i % 2) != 0)
{
i = i - 1;
do
{
System.out.println(i);
i = i - 2;
} while (i != -2);
}
}
}
So, if I give the number 11, it will print out 10, 8, 6, 4, 2. Why won't it print 0 as well, since my while statement contains i!= -2 and 0 counts as an even number?
Because after scanner.nextInt(); you must put scanner.nextLine(); else, the last element the scanner gets from nextInt(); will be ignored.
Even so, your algorithm is extremely dizzy. why not try:
Scanner in = new Scanner( System.in );
int number = in.nextInt(); in.nextLine();
for( int i = 0; i <= number; i += 2 ) {
System.out.println( i );
}
?