Jump to content

Newb -_- (Java help)


pg94
 Share

Recommended Posts

So I'm in an Intro to java class and I'm struggling to write a program.

Basically I need to write a program that when given 09/26/2012 or ANY DATE (1-12/1-31/2000-2015) gives an output of September 26th, 2012.

This is what I've done so far for the months. I'm trying to use the switch method. But I don't know how to ask for user input and make it into a variable so that it would became a month.

package javaapplication1;
public class JavaApplication1 {
public static void main(String[] args) {
//Scanner sc = new scanner(System.in);
System.out.println("Day of the month?");
{
int month == ;
String monthString;
switch (month)
{
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}
Link to comment
Share on other sites

Please wrap your code the code tabs [ "code" ] [ "/code" ] (just remove the "")

To read the input we create a new Scanner object:

Scanner in = new Scanner(System.in);

Then we prompt the user to enter the month. We don't want to make a new line so instead of doing println() just do print();

System.out.print("What month is it? (number form): ");

Next we get the input as an integer:

month = in.nextInt();

Finally we do your switch statement:

		switch (month) {
			case 1:
				monthString = "January";
				break;
	
			case 2:
				monthString = "February";
				break;
	
			case 3:
				monthString = "March";
				break;
	
			case 4:
				monthString = "April";
				break;
	
			case 5:
				monthString = "May";
				break;
	
			case 6:
				monthString = "June";
				break;
	
			case 7:
				monthString = "July";
				break;
	
			case 8:
				monthString = "August";
				break;
	
			case 9:
				monthString = "September";
				break;
	
			case 10:
				monthString = "October";
				break;
	
			case 11:
				monthString = "November";
				break;
	
			case 12:
				monthString = "December";
				break;
	
			default:
				monthString = "Invalid month";
				break;
		}

Here it is all together:

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1 {

	public static void main(String[] args) {
		// var
		String monthString = null;
		int month = -1;
		Scanner in = new Scanner(System.in);
		// var

		// Prompt the user to input the month
		System.out.print("What month is it? (number form): ");
		
		// Get the input from in
		month = in.nextInt();

		// Here is your switch statement
		switch (month) {
			case 1:
				monthString = "January";
				break;
	
			case 2:
				monthString = "February";
				break;
	
			case 3:
				monthString = "March";
				break;
	
			case 4:
				monthString = "April";
				break;
	
			case 5:
				monthString = "May";
				break;
	
			case 6:
				monthString = "June";
				break;
	
			case 7:
				monthString = "July";
				break;
	
			case 8:
				monthString = "August";
				break;
	
			case 9:
				monthString = "September";
				break;
	
			case 10:
				monthString = "October";
				break;
	
			case 11:
				monthString = "November";
				break;
	
			case 12:
				monthString = "December";
				break;
	
			default:
				monthString = "Invalid month";
				break;
		}

		// Print the month
		System.out.println("The month is: " + monthString);

	}
}

One thing to note is that you are expecting the user to enter an integer value so if they into a string the program will crash. I am not sure if you have learned about try and catch statements yet but if you have i suggest adding one around where you get the input

Link to comment
Share on other sites

Are there any other programming languages that you feel you are sufficiently proficient in, to be able to complete the same assignment, or is this your first programming course?

I'm asking because there are a rather large number of ways by which to complete the assignment, and it might make it easier to relate when you see similar concepts at work within the Java language.

And for what it's worth, I would:

1) not read from System.in but simply require a parameter to the java program which would then appear as a value in your args array.

2) wrap System.in in an InputStreamReader (and that in turn in a BufferedReader as the javadoc suggests, for efficiency reasons), read the characters the user provided from there into a CharBuffer, invoke toString on that CharBuffer which would yield me the input in String form, and go from there. I'm kinda low-level like that... :-/

Edited by Cooper
Link to comment
Share on other sites

I'm not really proficient in any other languages, I'm just starting to learn about it and its a pretty fast moving course. I greatly appreciate the help everyone! Im going to try and complete the code tonight or tomorrow and will post it!

Link to comment
Share on other sites

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1 {

	public static void main(String[] args) {
		// var
		String monthString = null;
		int month = -1;
		Scanner in = new Scanner(System.in);
                
                System.out.print("What month is it? (1-12) ");
		
		month = in.nextInt();
                
		switch (month) {
			case 1:
				monthString = "January";
				break;
	
			case 2:
				monthString = "February";
				break;
	
			case 3:
				monthString = "March";
				break;
	
			case 4:
				monthString = "April";
				break;
	
			case 5:
				monthString = "May";
				break;
	
			case 6:
				monthString = "June";
				break;
	
			case 7:
				monthString = "July";
				break;
	
			case 8:
				monthString = "August";
				break;
	
			case 9:
				monthString = "September";
				break;
	
			case 10:
				monthString = "October";
				break;
	
			case 11:
				monthString = "November";
				break;
	
			case 12:
				monthString = "December";
				break;
	
			default:
				monthString = "Invalid month";
				break;
		}

		// Print the month
		System.out.println(" " + monthString);
        }
        
        public  static void daymain(String[] args) {
            
                String suffix = null;
		int day = -1;
		Scanner in = new Scanner(System.in);
                
                System.out.print("What day is it? (1-31) ");
		day = in.nextInt();
                switch (day) {
                    case 1:
                    case 21:
                    case 31:
				suffix = "st";
			break;
	
                    case 2:
                    case 22:
                    
				suffix = "nd";
			break;
	
                    case 3:
                    case 23:
				suffix = "rd";
			break;
                        
                    default: 
				suffix = "th";
			break;
		}

	
		System.out.println( day + suffix);
        
        }
        
        public static void yearmain(String[] args) {
    
            String suffix = null;
		int year = -1;
		Scanner in = new Scanner(System.in);
                
                System.out.print("What year is it? (2000-2015) ");
		year = in.nextInt();
                switch (year) {
                
                case 2015:
                       System.out.print("2015");
                    break;
                    
                case 2014:
                       System.out.print("2014");
                    break;
                    
                case 2013:
                       System.out.print("2013");
                    break;
                    
                case 2012:
                       System.out.print("2012");
                    break;
                    
                 case 2011:
                       System.out.print("2011");
                    break;
                    
                case 2010:
                       System.out.print("2010");
                    break;
                    
                case 2009:
                       System.out.print("2009");
                    break;
                    
                case 2008:
                       System.out.print("2008");
                    break;
                    
                case 2007:
                       System.out.print("2007");
                    break;
                    
                case 2006:
                       System.out.print("2006");
                    break;
                    
                case 2005:
                       System.out.print("2005");
                    break;
                    
                case 2004:
                       System.out.print("2004");
                       break;
                   
                case 2003:
                       System.out.print("2003");
                    break;
                    
                case 2002:
                       System.out.print("2002");
                    break;
                    
                case 2001:
                       System.out.print("2001");
                    break;
                    
                case 2000:
                       System.out.print("2000");
                    break;
                       
                       
                default:
                       System.out.print("Invalid Year, Try Again.");
                }
        
            

So I know there is probably an easier way to go about the years but I found copying and pasting to be easiest for my skill level. ONLY problem is how would I combine all of the public static void groups together to get it to run as on continuous program?

So it would ask for the month, day, year, then at the end spit in out all together in MM/DD/YY order?

Link to comment
Share on other sites

To combine, consider each method you currently have as having 2 parts : The switch statement and everything after it versus anything before the switch statement.

What you want is 1 big method (for now) that first does the stuff before the switch statement of each of your 3 methods performed consecutively and then you want the switch statements and everything after it parts of each of your 3 methods to be performed consecutively.

Just dunk it into 1 long method for now. We'll work on getting things more managable later.

As for the year, you can simply send it to System.out using

""+year

to format the number as a string. Or the slightly cleaner and more performant version:

Integer.toString( year )

You should not have to initialize your 'suffix' variable to anything. By not initializing it, you get a compiler warning when you accidentally do something with it before it was set whereas your current, pre-initialized version would be accepted.

There is no need to first declare a variable at the start of a method before using it, so your 'day' variable could've been easily declared on the line where it's actually set, like so:

int day = in.nextInt();

Did you check already what happens when what you enter is in fact an invalid number (day of 32 or over, or maybe not even a number at all)?

Edited by Cooper
Link to comment
Share on other sites

package javaapplication1;

import java.util.Scanner;

public class JavaApplication1 {

	public static void main(String[] args) {
		// var
		String monthString = null;
		int month = -1;
		Scanner in = new Scanner(System.in);
                
                System.out.print("What month is it? (1-12) ");
		
		month = in.nextInt();
           
                String suffix = null;
		int day = -1;
		Scanner iu = new Scanner(System.in);
                
                System.out.print("What day is it? (1-31) ");
		day = in.nextInt();
                
                String yearString = null;
		int year = -1;
		Scanner it = new Scanner(System.in);
                
                System.out.print("What year is it? (2000-2015) ");
		year = in.nextInt();
                
                
                
		switch (month) {
			case 1:
				monthString = "January";
				break;
	
			case 2:
				monthString = "February";
				break;
	
			case 3:
				monthString = "March";
				break;
	
			case 4:
				monthString = "April";
				break;
	
			case 5:
				monthString = "May";
				break;
	
			case 6:
				monthString = "June";
				break;
	
			case 7:
				monthString = "July";
				break;
	
			case 8:
				monthString = "August";
				break;
	
			case 9:
				monthString = "September";
				break;
	
			case 10:
				monthString = "October";
				break;
	
			case 11:
				monthString = "November";
				break;
	
			case 12:
				monthString = "December";
				break;
	
                        default:
				monthString = "Invalid month";
                                break;
                }
                     switch (day) {
                    case 1:
                    case 21:
                    case 31:
				suffix = "st";
			break;
	
                    case 2:
                    case 22:
                    
				suffix = "nd";
			break;
	
                    case 3:
                    case 23:
				suffix = "rd";
			break;
                        
                    default: 
				suffix = "th";
                                break;
                     }
                        
                        switch(year) {
                case 2015:
                       yearString = "2015";
                    break;
                    
                case 2014:
                       yearString = "2014";
                    break;
                    
                case 2013:
                       yearString = "2013";
                    break;
                    
                case 2012:
                       yearString = "2012";
                    break;
                    
                 case 2011:
                       yearString = "2011";
                    break;
                     
                 case 2010:
                        yearString = "2010";
                     break;
                
                 case 2009:
                       yearString = "2009";
                    break;
                    
                case 2008:
                       yearString = "2008";
                    break;
                    
                case 2007:
                       yearString = "2007";
                    break;
                    
                case 2006:
                       yearString = "2006";
                    break;
                    
                 case 2005:
                       yearString = "2005";
                    break;
                     
                 case 2004:
                        yearString = "2004";
                     break; 
                     
                       case 2003:
                       yearString = "2003";
                    break;
                    
                case 2002:
                       yearString = "2002";
                    break;
                    
                case 2001:
                       yearString = "2001";
                    break;
                    
                 case 2000:
                       yearString = "2000";
                    break;
                  
                 default:
                     System.out.print("Invalid input, try again.");
                 
                
                }
        
        
System.out.println( monthString + day + suffix + year);
        }                
}
        

SWWEEEETTT, I give you the ugliest code but yet functioning! Thanks for the help! Definitely excited to learn to code!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...