Java Help

Discussion in 'Mac Programming' started by MBP123, Sep 22, 2007.

  1. MBP123 macrumors regular

    Joined:
    May 26, 2006
    #1
    So I am just getting in to Java, and was having a little difficulty with it. I was wondering if anyone would be willing to IM me over AIM or something, or exchange through email and help me out. The questions are pretty simple, but they I would really appreciate it. And there are numerous ways in which I can repay the favor to them. Please respond if someone can help me, itd be much appreciated.

    Thanks
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    Why not just post here? The flaw in what you are suggesting is that when you are ready to work and need help, said volunteer might not want to be interrupted. By psoting here, you get the miracle of random surfer help, and responses are usually pretty fast.

    Todd
     
  3. x704 macrumors regular

    x704

    Joined:
    Apr 15, 2006
    #3
    Indeed.

    However if you _really_ want instant help you can download x-chat aqua, connect to the freenode server (irc.freenode.net - it will be in a long list) and join the Java channel. Just to let you know... generally people don't like to help in the IRC channel unless you are really truly stuck with whatever tutorial you are going through - at least in my experience.
     
  4. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #4
    Well I would just prefer to use AIM as it would be quick feedback and I would have someone there to ask questions with if need be. But I will post it here anyway. basically I am in an intro to Java class, and have an assignment which I will post:

    PART ONE:
     
  5. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    So far, so good.

    First, some hints, and we'll go from there.

    You are working with strings, so you need some methods to work with strings. You need to suck out parts of the string, convert those parts into some numeric data type, and then put them into their own variables so you can do the comparisons you need.

    Do you know where to go to look up methods that work with strings?

    Todd
     
  6. Wes macrumors 68020

    Wes

    Joined:
    Jun 22, 2001
    Location:
    London
    #6
    A sizable prod in the direction of the solution, interesting bits included here:

    Code:
    
       String datetext;
       int year1;
       int month1;
       int day1;
       
       
       /* Integer.parseInt is a method which you pass a String and it will convert it to an Integer for you. */
       int value1  = Integer.parseInt(JOptionPane.showInputDialog (null, "What is your birthday YYYYMMDD?"));  
       
       /* 
        * This is a bit of devious arithmetic to split the number up at the correct places.
        * % is the modulo operator (http://en.wikipedia.org/wiki/Modulo_operation)
        * Use the example 19880130 and work through what each of these lines does.
        */ 
       year1 = value1 / 10000;
       month1 = (value1 / 100) % 100;
       day1 = value1 % 100;
       
    .
    .
    .
    .
    
        System.out.println (name1 + " was born on " + month1 + "/" + day1 + "/" + year1);
    
     
  7. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #7
    well i have another assignment due, and i think i am getting the hang of things
    what i have to do is determine whether when a user inputs a specific date, if that date is a school day or not

    here is the code i have now


    import javax.swing.JOptionPane;

    public class asgn2 {
    public static void main(String args[]) {


    String numString = JOptionPane.showInputDialog(null,
    "Pick a month!! (1-12):", "Input Window Demo",
    JOptionPane.QUESTION_MESSAGE);

    String dayString = JOptionPane.showInputDialog(null,
    "Pick a day!! (1-31):", "Input Window Demo",
    JOptionPane.QUESTION_MESSAGE);



    // Convert the string into an int value
    int month = Integer.parseInt(numString);
    System.out.print("The date you entered is ");
    switch ( month ) {
    case 1:
    System.out.print("January " + dayString + "\n" );
    break;
    case 2:
    System.out.print("February " + dayString + "\n" );
    break;
    case 3:
    System.out.print("March " + dayString + "\n" );
    break;
    case 4:
    System.out.print("April " + dayString + "\n" );
    break;
    case 5:
    System.out.print("May " + dayString + "\n" );
    break;
    case 6:
    System.out.print("June " + dayString + "\n" );
    break;
    case 7:
    System.out.print("July " + dayString + "\n" );
    break;
    case 8:
    System.out.print("August " + dayString + "\n" );
    break;
    case 9:
    System.out.print("September " + dayString + "\n" );
    break;
    case 10:
    System.out.print("October " + dayString + "\n" );
    break;
    case 11:
    System.out.print("November " + dayString + "\n" );
    break;
    case 12:
    System.out.print("December " + dayString + "\n" );



    default:

    System.out.print("That month does not exist you fool! \n");
    } // end switch

    System.exit( 0 ); // terminate application

    } // end main
    }

    according to the assignment:

    The student should be prompted to enter an integer for the month and then prompted to enter an integer for the day. (For example, entering "2" for the month and "14" for the day would represent February 14th.) (WHICH I HAVE DONE ALREADY)

    The program will tell the student if that date is a vacation day and if so, which one, by printing out a message such as:
    February 18 is Presidents' Day.

    If the day selected is not a holiday, the program should print out a message such as:
    February 17 is not a school holiday.


    Now for the second part, i am pretty sure i have to do an if/else statement, but how would i get that to display or compile since i have 2 different variables? would it be something like


    case 1:
    System.out.print("January " + dayString + "\n" );
    break;

    if dayString + numString == January 8
    else System.out.print("January" + dayString + " is a holiday";


    i know thats not right, but am i somewhat close?

    thanks in advance
     
  8. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    You still need a few things:

    1) You need to add some constants to your program that represent your school's holidays. If you have used arrays or Vectors that would be the easier (most succinct) way to do it. However, if not, follow your instructor's lead as he did for President's day.

    2) You probably need to validate the day the user entered. Here, you have to validate it based on the month - Jan has 31, Feb has 28, etc.

    3) For the comparison of the user's month & day value to any given holiday, you can use the logical AND operator, as such:

    if (month==presidentsday_month && day==presidentsday_day) {
    blah, blah
    }

    Todd

    P.S. if you:

    import java.lang.System ;

    you can get by with:

    out.println(......) ;
     
  9. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #9
    1) we have not done arrays or vectors yet, so how would i then go about it?
    2) what would i do in order to validate? like say for instance february 30 is not a date
    3) what does import.java.lang.System do?
     
  10. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    OK, forget about that stuff for now. Post the format your instructor showed you for President's Day.

    Todd
     
  11. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #11
    they didnt show us how to do it...

    my guess would be that for each month id have to do an if/else statement regarding the user inputting that specific date, in which case it would print out whether its a holiday or not?
     
  12. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #12
    That would be an excellent way to start. Start on that, and post here with your code and any additional questions.

    Todd

    EDIT: as a hint, you could add the holiday checking to your current case ladder:

    Code:
    case 7:
    System.out.print("July " + dayString + "\n" );
    if (Integer.parseInt(dayString) == 4) {  
        System.out.println("July " + dayString + " is Independence Day") ; 
        is_a_holiday = true ;   // Flag that they picked a holiday 
    }
    break;
    
     
  13. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #13
    so i am having trouble with january as there are 2 separate situations that i need to account for. i think i just have a few too many parenthesis but i cant seem to figure it out. also when i put in the part about martin luther, all the other case below it become orphaned, whereas when i have it just as the winter break situations, they are fine

    int month = Integer.parseInt(numString);
    System.out.print("The date you entered is ");
    switch ( month ) {
    case 1:
    System.out.print("January " + dayString + "\n" );
    if (((Integer.parseInt (dayString) >= 1 ) &&
    Integer.parseInt (dayString) <= 19 ))
    System.out.println (("January " + dayString + " is Winter Break" ))
    || Integer.parseInt (dayString) == 21 )));
    System.out.println ("January " + dayString + " is Martin Luther King's Birthday");
    break;
     
  14. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #14
    Oh, YUK! Gross.

    Here's your code, reformatted to illustrate my response:

    Code:
    int month = Integer.parseInt(numString);
    
    System.out.print("The date you entered is ");
    
    switch ( month ) {
    case 1:
    	System.out.print("January " + dayString + "\n" );
    	if 	(	
    		(	
    		( Integer.parseInt (dayString) >= 1  ) &&
       		  Integer.parseInt (dayString) <= 19 )
    		)
    		System.out.println 
    		(
    		("January " + dayString + " is Winter Break" )
    		)
    		|| 
    		Integer.parseInt (dayString) == 21 
    		)
    		)
    		) ;
    	System.out.println ("January " + dayString + " is Martin Luther King's Birthday");
    	break;
    
    Me thinks you kinda get the picture by looking at it in this manner.

    Simplify.

    First, write out your logic in simple terms:

    Code:
    if 1 <= dayString and dayString <= 19  then Winter Break.  
    if day == 21 then Martin Luther 
    
    Then, start wrapping it in language syntax:

    Code:
    if ( 1 <= dayString && dayString <= 19) System.out..... ; 
    if ( dayString == 21) System.out..... ; 
    
    You'll obviously have to use the parseInt() method, but again, that's just wrapping syntax around your simple logic.

    (BTW, you can use the [code] and [/code] tags to keep your code formatted when posting here.

    Todd
     
  15. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #15
    Actually, maybe this paints a clearer picture.

    Code:
    int month = Integer.parseInt(numString);
    
    System.out.print("The date you entered is ");
    
    switch ( month ) {
    case 1:
    	System.out.print("January " + dayString + "\n" );
    	if 	
    	(	
    		(	
    			( 
    				Integer.parseInt (dayString) >= 1  
    			) 
    			&&
    			Integer.parseInt (dayString) <= 19 
    		)
    	)
    	System.out.println 
    	(
    		("January " + dayString + " is Winter Break" )
    	)
    	|| 
    	Integer.parseInt (dayString) == 21 
    )
    )
    ) ;
    System.out.println ("January " + dayString + " is Martin Luther King's Birthday");
    break;
    
     
  16. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #16
    alright thanks ill give that a shot!

    sorry about posting the code like i did, i didnt know there was a way to keep it formatted
     
  17. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #17
    ok i just finished it. it works great!

    thanks a ton guys youre so much help
     
  18. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #18
    okay so its time for assignment three. this place is the best help, even more than the TAs.

    PART ONE:

    Write a program that prompts the user to enter a sentence from the keyboard using JOptionPane.showInputDialog.

    The program will print the characters back with the first letter of each word changed from lower case into upper case. If you have a capital letter in the original line and it is not the first letter of a word, then this letter should be switched from upper case to lower case. The only capital letters that should appear in the line must be the begining letter of every word in the line. All other characters will remain the same. At the end, the program will output a summation of:

    How many upper case letters in the original sentence (before any changes)
    How many lower case letters in the orginal sentence (before any changes)
    How many blank spaces
    How many other characters were encountered
    The total number of characters.
    Sample run:

    If the user types in the following string:

    This is JUst a Sample Run of the stupid homeWork.

    Then the program should print out the following:

    This Is Just A Sample Run Of The Stupid Homework.

    Upper case letters : 6
    Lower case letters : 33
    Blank spaces : 9
    Other characters : 1
    Grand total : 49

    Hint and Notes:

    Use the TextString.charAt(position) method to break the string into individual characters within a loop structure.
    Use the TextString.length() method to get the length of your string
    Lower case letters are within a range of characters, as are upper case letters based on ASCII values. See Appendix B in the textbook. A space has an ASCII value of 32. Do not write a gigantic switch statement with a case for each lower case letter and a case for each upper case letter. You should be able to test for the characters based on the ranges of values presented in the ASCII table in appndex B.




    This one I am really at a loss for how to start. should i set each word as a variable in order to capitalize it? And I do not really grasp how to use chars. Do I just do a char for each place in the sentence,,, or does that somehow tie in with loops? Also, if someone could PM me or IM me and hep me through it more step by step if they have a block of free time itd be much appreciated.

    Thanks in advance!
     
  19. sord macrumors 6502

    Joined:
    Jun 16, 2004
    #19
    I'll help out, BUT first - break up your goals into small steps. If you are at a loss on how to start, you are looking at too much of the problem at once. If you don't understand chars, google 'java chars'

    You may want to look at the String API (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html)

    Then, even if it doesn't work or look right, start working on a program. Start by creating a class with just comments of what you need to accomplish. Then start filling in what you can and leave the parts you are completely stuck on commented.

    THEN, post what you have and why you don't understand what's left.

    (Or wait for someone with less faith in you give you the answer...)
     

Share This Page