| | |
Main displaying values twice
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hello,
I have a program called Date. While this program compiles and runs, I have noticed something weird. I have an output line that displays my values twice.
Sample Run:
The word date output is: February 12 2009
The numeric output of the date is: 2/12/2009
The next incremented date is: 2/12/2009
The next incremented date is: 2/13/2009
Why is that? I have the same issue in my other program. There is something i am overlooking on the consistent basis
Any help will be appreciated:
Code:
// Main
// Class Date
I have a program called Date. While this program compiles and runs, I have noticed something weird. I have an output line that displays my values twice.
Sample Run:
The word date output is: February 12 2009
The numeric output of the date is: 2/12/2009
The next incremented date is: 2/12/2009
The next incremented date is: 2/13/2009
Why is that? I have the same issue in my other program. There is something i am overlooking on the consistent basis
Any help will be appreciated:
Code:
// Main
import java.io.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args)throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); // Calling on java class which allow to read user input
// Local Variables
String temp;
int month;
int day;
int year;
Date myDate = new Date();
// Reading user input for month
System.out.println("Please enter month in numeric format 1-12:");
temp = stdin.readLine();
month = Integer.parseInt(temp); // Converting a string into int
// Reading user input for day of the month
System.out.println("Please enter day of the month in numeric format 1-31:");
temp = stdin.readLine();
day = Integer.parseInt(temp); // Converting a string into int
// Reading user input for the year
System.out.println("Please enter year:");
temp = stdin.readLine();
year = Integer.parseInt(temp); // Converting a string into int
// Calling methods from a class Date
myDate.outputDate(month,day,year);
myDate.increment(month,day,year);
}
}// Class Date
public class Date
{
// instance variables - month date and a year
final int MAX_DAYS = 31; // Constant that cannot be changed is the number of days in one month
private int month;
private int day;
private int year;
/**
* Constructor for objects of class Date
*/
// 3 argument Constructor
public Date()
{
// 3 argument Constructor
int month = 0;
int day = 0;
int year = 0;
}
/**
* increment method
* Incrementing the date to the next day
* @param month, day, year
* @return i (incremented date)
*/
public int increment(int arg1, int arg2, int arg3)
{
int i;
month = arg1;
day = arg2;
year = arg3;
for (i = day; i <= day+1; i++)
{
System.out.println( "The next incremented date is: " + month + "/" + i + "/" + year);
}
return i;
}
/**
* Output date method
* This method gives an output in word and numeric format simultaneously
*
* @param arg1, arg2, arg3
* @return date
*/
public int outputDate(int arg1, int arg2, int arg3)
{
// Local variables
int date =0;
month = arg1;
day = arg2;
year = arg3;
// This loop provides user with word and numeric output of a date
if(month <= 12)
{
if (month == 1)
{
System.out.println("The word date output is: January " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 2)
{
System.out.println("The word date output is: February " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 3)
{
System.out.println("The word date output is: March " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 4)
{
System.out.println("The word date output is: April " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 5)
{
System.out.println("The word date output is: May " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 6)
{
System.out.println("The word date output is: June " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 7)
{
System.out.println("The word date output is: July " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 8)
{
System.out.println("The word date output is: August " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 9)
{
System.out.println("The word date output is: September " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 10)
{
System.out.println("The word date output is: October " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 11)
{
System.out.println("The word date output is: November " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
else if (month == 12)
{
System.out.println("The word date output is: December " + day + " " + year);
System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
}
}
else {
System.out.println("There are only 12 months in one year, Please eneter number between 1 and 12: ");
}
return date;
}
} You use a for loop in your increment method, so it prints out both lines. Removing the for loop and using a simple println will take out the duplicated line. Just be sure to increment the day.
Java Syntax (Toggle Plain Text)
public void increment(int arg1, int arg2, int arg3) { int i; month = arg1; day = arg2; year = arg3; //for (i = day; i < day+1; i++) // { System.out.println( "The next incremented date is: " + month + "/" + (day+1) + "/" + year); //} //return i; }
Last edited by LevelSix; May 2nd, 2009 at 8:40 pm.
![]() |
Similar Threads
- retreving value from database and displaying them in tree menu. (PHP)
- Looking up and displaying values in linked lists (C++)
- library program (C++)
- Again please someone check my code (C++)
- Homework help - displaying a Chart (C++)
- Retrieving values from pop up and displaying into asp page (ASP)
- Need help displaying when user enters negative number? (C++)
- Problems with displaying total payroll and allowing user to quit by entering a neg. (C++)
Other Threads in the Java Forum
- Previous Thread: Binary Tree and recursion
- Next Thread: Collaboration between java and vb
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) calculator chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows





