| | |
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 |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows





