PDA

View Full Version : Anyone know any java ?



Psyco Groupie
26-02-04, 22:17
WTF is wrong with this:


import java.util.*;
import java.text.*;
import java.io.*;
import javax.swing.*;

class FindDayOfWeekOneInput {

public static void main( String[] args ) throws IOException {

String inputStr;
int month, day, year;

GregorianCalendar cal;
SimpleDateFormat sdf;

BufferedReader bufReader;

bufReader = new BufferedReader(
new InputStreamReader( System.in));
String monthStr;
String dayStr;
String yearStr;

inputStr = JOptionPane.showInputDialog(null, "Enter the date mm/dd/yyyy: ");

monthStr = inputStr.substring(0,2);
dayStr = inputStr.substring(3,5);
yearStr = inputStr.substring(6,10);

String dateStr;
dateStr = monthStr + dayStr + yearStr;
/*
System.out.print(dateStr);
*/
month = Integer.parseInt(dateStr);
day = Integer.parseInt(dateStr);
year = Integer.parseInt(dateStr);

cal = new GregorianCalendar(month-1, day, year);
sdf = new SimpleDateFormat("EEEE");

System.out.print("");
System.out.print("Day of Week: " + sdf.format(cal.getTime()));

}

}


Its supposed to take the input of date, get the date from between the brackets and then output which day of the week that date would be ... the output makes sense, yet its not always right about the day .. sooo weird

help apreciated O.o

Mighty Max
26-02-04, 22:24
Heyo,

ive not much exp in Java, but in some other languages. I would search the error belong these lines:

monthStr = inputStr.substring(0,2);
dayStr = inputStr.substring(3,5);
yearStr = inputStr.substring(6,10);

month = Integer.parseInt(dateStr);
day = Integer.parseInt(dateStr);
year = Integer.parseInt(dateStr);

monthStr is creating a substring from 0-2 right? then it is "mm/" (3 chars) an integer parse will fail for the "/" not being translateable, and the month as result may become invalid or random (depends how java handels this) Same applies to the day then.

Psyco Groupie
26-02-04, 22:27
thats what i thought, but as it is it outputs only numbers !

lemme have a fiddle

how i have it seems to create the right output but is wrong in theory .. this is so weird

Mighty Max
26-02-04, 22:29
Simple test it with outputting the date by the numbers. Rather then the string parts.

Psyco Groupie
26-02-04, 22:30
how it is in the first post gives "02262004"

with 'correct' substrings it gets "02200"

this is fricken anoying :(

Mighty Max
26-02-04, 22:35
right, i just looked it up. the "end" is not added to the substring.

Uhm, but yeah, i think i got it:

month = Integer.parseInt(dateStr);
day = Integer.parseInt(dateStr);
year = Integer.parseInt(dateStr);


should not refer to dateStr each but its corresponding monthStr,dayStr and yearStr ...

Psyco Groupie
26-02-04, 22:38
Nope, ive tried that - thats why dateStr is there, basically each substring stuck together

this is soo frustrating o_O

Strych9
26-02-04, 22:40
Originally posted by Psyco Groupie
Nope, ive tried that - thats why dateStr is there, basically each substring stuck together

this is soo frustrating o_O But these three variables...

month = Integer.parseInt(dateStr);
day = Integer.parseInt(dateStr);
year = Integer.parseInt(dateStr);

would all be exactly the same. They are each assigned the exact same value...

Dont you want those three to be different?

Mighty Max
26-02-04, 22:42
not for the output for calculating the numbers relative to this. atm you can translate these 3 lines in

month = Integer.parseInt(dateStr) ;
day = month ;
year = month ;

It cant work, when you calc month day year out of the same value.

Nidhogg
26-02-04, 22:44
They're trying to say you need this. ;)



month = Integer.parseInt(monthStr);
day = Integer.parseInt(dayStr);
year = Integer.parseInt(yearStr);



N

Psyco Groupie
26-02-04, 22:46
hmmm .. yeah that makes sense, ive just checked the output of those strings and they output '02' '26' '2004' repsectively .. so the error is somewhere beyond that ...

this sux still o_O

Nidhogg
26-02-04, 22:48
The signature for GregorianCalendar is as follows:

public GregorianCalendar(int year,
int month,
int date)
So you have the parameters in the wrong order. Should be:

cal = new GregorianCalendar(year, month-1, day);

N

Mighty Max
26-02-04, 22:52
PS (month-1) might create problems too. i dont think it could handle the month 0. You'd to test for the zero value and exchange it with 12 and decrease the year if it is intended to take the month before

or does it handle it from 0..11?

Psyco Groupie
26-02-04, 22:53
omg im such a tired fool, im been doing this stuff all afternoon so i followed the input format in the calendar ... doh!

thanks nid, i guess theres a use for you after all :lol:













j/k! :lol:

thanks to max too!

*edit bajillion typos and you -1 cos of the calendar format .. its just a rule afaik

craio
26-02-04, 22:56
Probably won't be this but according to my java book GregordianCalendar is
GregordianCalendar ( year,month,day) not GregorianCalendar(month, day, year) like you have it.Dunno if that makes a difference

Nidhogg
26-02-04, 22:57
You can also just do


System.out.println("Day = " + cal.get(Calendar.DAY_OF_WEEK));
N

Mighty Max
26-02-04, 22:57
yw, im a nice guy at all, i can only get a bitch if someone wants to steal my LE :p

Psyco Groupie
26-02-04, 23:05
Thanks everyone again

Yeah I know theres any easier way Nid but the task was converting another prog, all together alot more lengthy :)

:angel: