Okay. I'm in need of desperate help and really hope someone can help me type out the entire code for a simple basic java project.

1)adherence to java naming conventions
2)At least one static method other than main
3)providing int, double and string inputs
4)proper selection construct (switch-case or if-else)
5)giving error messages for wrong entry
6)Array declaration and storing/retrieving of array elements.

The pictures will show how the program should run. Thank you

Everything should be basic and if it is important i will be using jcreator

Attachments CIMG1511.JPG 210.57 KB CIMG1512.JPG 241.15 KB CIMG1513.JPG 240.1 KB
/* @(#)Ranking.java
 *
 *
 * @author 
 * @version 1.00 2009/1/7
 */
import java.util.*;
import java.text.*;
public class Ranking20 
    {
        static Scanner input=new Scanner(System.in).useDelimiter("\r\n");
        static DecimalFormat fmt=new DecimalFormat("0.00");

        static int[] salary= new int[7];
        static String[] person_name= new String[7];
        static String[] month = new String[12]; 
        static boolean loop=true;




    public static void main(String[] args) throws Exception
        {       
            month[0] =  "Jan";
            month[1] =  "Feb";
            month[2] =  "Mar";
            month[3] =  "Apr";
            month[4] =  "May";
            month[5] =  "Jun";
            month[6] =  "Jul";
            month[7] =  "Aug";
            month[8] =  "Sep";
            month[9] =  "Oct";
            month[10] =  "Nov";
            month[11] =  "Dec";


            char selection = 'x';
            do
            {
                System.out.println("**********Family Income**********");
                System.out.println("");
                System.out.println("1) Enter monthly salary");
                System.out.println("2) Display detailed salary by month");
                System.out.println("3) Quick glance at monthly salary");
                System.out.println("4) Exit");

                System.out.print("Please select your choice (1-4) : ");
                selection=(char)System.in.read();
                System.in.read();
                System.in.read();


                switch(selection)
                {
                    case '1':       

                        {
                                int month2=0;
                                        System.out.print("Enter month (1 for Jan - 12 for dec): ");
                                        month2 = input.nextInt();

                                        System.out.print("Enter person 1 (Press ENTER to exit) : ");
                                        person_name[month2] = input.next();

                                        System.out.print("Enter Salary : ");
                                        salary[month2]=input.nextInt();
                                        break;
                    }




                    case '2':   
                                {
                                    System.out.println(""+salary[5]);
                                    break;
                                }






                    case '3':   
                    {
                            System.out.println("test");
                                break;
                    }




                    case'4': break;



                    default: System.out.println("Invalid choice.Please select choice <1-4> only!Stupid!");
                }



    }while(loop);
}//main
    }//class

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I would really urge you to simplify your code for your self and for everyone else. Programming your way would never work for a large application/program because it makes it look way too complex and annoying to read....

Your project shows 3 pictures, one for each option, so automatically you should plan on making at least 3 main functions or groups of code that print out that information like those pictures. This will help you simplify what you need to do, in your mind, before you actually start typing... You should think to yourself "ok, its like 3 little programs into 1, so lets start working at it 1 at a time".

On top of that, look at where you will be using repetitive code, and shrink that information into a single function, so where you would usually have 3-4 lines, 1 line will suffice. An example from your situation would be the fact that you must take in information from the user a lot AND define that information to one of your arrays. I would do something like this:

public static int askForInt(String text) {
		System.out.print(text);
		int given = input.nextInt();
		System.out.println();
		return given;
	}
	
	public static String askForString(String text) {
		System.out.print(text);
		String given = input.nextLine();
		System.out.println();
		return given;
	}
	
	public static long askForLong(String text) {
		System.out.print(text);
		Long given = input.nextLong();
		System.out.println();
		return given;
	}

Where text is your question and the return type is the value you are looking to receive... That way, your first code group would shrink down to something like this:

while (true) {
	System.out.println("**********Family Income**********");
	System.out.println("");
	System.out.println("1) Enter monthly salary");
	System.out.println("2) Display detailed salary by month");
	System.out.println("3) Quick glance at monthly salary");
	System.out.println("4) Exit");

	int choice = askForInt("Please select your choice (1-4) : ");
	switch(choice) {
	case 1:
		month[len] = askForInt("Enter month (1 for Jan - 12 for dec): ");
		person[len] = askForString("Enter person 1 (Press ENTER to exit) : ");
		salary[len] = askForLong("Enter Salary : ");
                len++;
		break;
	case '2':
		......
	}
}

len would be initialized at 0 and grow depending on how many people you add.... Remember, (and I didn't add this for you) case 1 should use "continue;" through the while loop if len > 8 before asking for month OR a "" person is given by the user.

Doing things like this wouldn't hurt the eyes so much either:

String[] moNums = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

The first one is just "" because we DON'T want to use index 0 for Jan, rather have it associated as the first month (or index 1) for simplification.


Also, USE CONSTRUCTORS!

public class helper
{
	static Scanner input;
	static DecimalFormat fmt;
	
	static long[] salary;
	static String[] person;
	static int[] month;
	static int len;

	public helper() {
		input = new Scanner(System.in).useDelimiter("\r\n");
		fmt = new DecimalFormat("0.00");
		
		salary = new long[8];    //remember, u need 8 max, not 7
		person = new String[8];
		month = new int[8];
		len = 0;
	}
       ......
       ......
}

Constructors are used to "construct" your variables. You should definitely use it!

The White has done it for you. Also if you know OOP incorporate that. Also use try and catches.

public static int askForInt(String text) {
    System.out.print(text);
    int given = input.nextInt();
    System.out.println();
    return given;
}

public static String askForString(String text) {
    System.out.print(text);
    String given = input.nextLine();
    System.out.println();
    return given;
}

public static long askForLong(String text) {
    System.out.print(text);
    Long given = input.nextLong();
    System.out.println();
    return given;
}

i don't understand what this part is supposed to do and does in go into the main or before the main in the class?

Edited 3 Years Ago by Dani: Formatting fixed

They would go outside of main, but inside the class. These are functions of the class.

As far as what they do, that should be self explained by the functions name.

String askForString (String text) for example, would ask the the question, print a new line, and return the input.

public static String askForString(String text) {
		System.out.print(text); //what is the persons name?
		String given = input.nextLine(); //his name is Charles
		System.out.println();  //new line
		return given;  //return Charles
	}

You would call it like this:

askForString("What is the persons name?   ");

if you want to save the input into an array so u can print it later, you would do:

person[0] = askForString("What is the persons name?   ");

Console:

What is the persons name? Charles

so if i were to type out the function then there wouldn't be a need for the "system.out.println" in my case 1,2 etc?? because then i would be able to just ask for the month and person in the static function

case 1:
		month[len] = askForInt("Enter month (1 for Jan - 12 for dec): ");
		person[len] = askForString("Enter person 1 (Press ENTER to exit) : ");
		salary[len] = askForLong("Enter Salary : ");
                len++;
		break;
	case '2':

Why is case 2 within ' ' and case 1 isn't.

int choice = askForInt("Please select your choice (1-4) : ");
	switch(choice) {
	case 1:
		month[len] = askForInt("Enter month (1 for Jan - 12 for dec): ");
		person[len] = askForString("Enter person 1 (Press ENTER to exit) : ");
		salary[len] = askForLong("Enter Salary : ");
                len++;
		break;

Also why are month, person and len all initialized to "len" should it not be like

month[0] = askForInt("Enter month (1 for Jan - 12 for dec): ");

For the first question, you wouldnt ask the person again for parts 2 and 3, instead you would fetch the information from the 3 arrays and print those.

2nd question, that was my mistake, case '2': should be case 2:
Your code used characters. Mine used ints because the functions made it easy to ask for an int when choosing from the menu. I just forgot to change case '2' from your way to my way. :) ---- btw, keep in mind that I never tested my code before posting it. Some other things might be wrong. If there are, u can refine them as you build your project.

3rd question, your constructor should initialize len to 0. Every time someone adds a month, person, and salary, len would then increment (len++). Like I said in my first post, you need to handle "if len == 8" "print: sorry, 8 people maximum" "continue; the while loop". That I'll leave for you to handle so I dont write all your code.

/**
 * @(#)FAMILY.java
 *
 *
 * @author 
 * @version 1.00 2009/8/13
 */
import java.util.*;
import java.text.*;
public class FAMILY {

            static Scanner input;
            static DecimalFormat fmt;

            static long[] salary;
            static String[] person;
            static int[] month;
            static int len;


            {

                input = new Scanner(System.in).useDelimiter("\r\n");
                fmt = new DecimalFormat("0.00");

                salary = new long[8];    
                person = new String[8];
                month = new int[8];
                len = 0;
            }

            public static int askForInt(String text) {
            System.out.print(text);
            int given = input.nextInt();
            System.out.println();
            return given;

      }

            public static String askForString(String text) {
            System.out.print(text);
            String given = input.nextLine();
            System.out.println();
            return given;

      }


            public static long askForLong(String text) {
            System.out.print(text);
            Long given = input.nextLong();
            System.out.println();
            return given;
      }

    public static void main(String[] args) {

        String[] moNums = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };




                while (true) {
                System.out.println("**********Family Income**********");
                System.out.println("");
                System.out.println("1) Enter monthly salary");
                System.out.println("2) Display detailed salary by month");
                System.out.println("3) Quick glance at monthly salary");
                System.out.println("4) Exit");

                    int choice = askForInt("Please select your choice (1-4) : ");
                    switch(choice) {
                            case 1:
                            {
                                month[len] = askForInt("Enter month (1 for Jan - 12 for dec): ");
                                person[len] = askForString("Enter person 1 (Press ENTER to exit) : ");
                                salary[len] = askForLong("Enter Salary : ");
                                len++;
                            }
        break;
                            case '2':
                            {
                                System.out.println(""+salary[5]);
                                    break;
                            }

                 }//end of switch


  }//end of while

  }//end of main
}//end of class

so this is what i have so far

Edited 3 Years Ago by Reverend Jim: Fixed formatting

[INDENT]/**
 * @(#)sdfs.java
 *
 *
 * @author Bervin Manoharan
 * @version 1.00 2009/8/13
 */


import java.util.*;
import java.text.*;

public class FamilyIncome
{
	    static Scanner input = new Scanner(System.in).useDelimiter("\r\n");
  		static DecimalFormat fmt = new DecimalFormat("0.00");
	    static float [] dbSalary = new float[8];
        static String[] dbName = new String[8];
    	static String[] dbMonth = new String[8];
    	static int counter = 0;	//declare & initialize
    	static int choice;			//declare
    	static int choice2;			//declare
    	static int noOfPerson=1;
    	static String familyName="";//declare
    	static int salary;
    		
    public static void main(String[] args)throws Exception	//method exceution or call
    {    
    displayMenu();	
     
 	}//end of static void main
    	static void displayMenu()
 		{
 			do
    {
    	        System.out.println("**********Family Income**********");
    			System.out.println("");
    			System.out.println("1) Enter monthly salary");
    			System.out.println("2) Display detailed salary by month");
    			System.out.println("3) Quick glance at monthly salary");
    			System.out.println("4) Exit");
    		
    			System.out.print("Please select your choice (1-4) : ");
   				choice2=input.nextInt(); //system will capture & read user input
    
    
    switch(choice2)
    {
        		 case 1:
				 monthSalary();//method execution or call				
       			 break;

        		 case 2:
        		 detailSalary();//method execution or call
        		 break;

    			 case 3:
				reportSalary();//method execution or call
    			 break;

    			 case 4:
    			 exit();//method execution or call	
             	 return;

        		 default:          
                 System.out.println("Invalid choice");

     }//end of switch case
   }while (choice!=4); //end of do-while 
 		}
		static void monthSalary()
 		{
		System.out.print("Enter month (1 for Jan - 12 for dec): ");
		choice2=input.nextInt();

			if(choice2==1)
			{
			dbMonth[counter]="January";	
			}
			if(choice2==2)
			{
			dbMonth[counter]="Febuary";	
			}
			if(choice2==3)
			{
			dbMonth[counter]="March";	
			}
			if(choice2==4)
			{
			dbMonth[counter]="April";	
			}
			if(choice2==5)
			{
			dbMonth[counter]="May";	
			}
			if(choice2==6)
			{
			dbMonth[counter]="June";	
			}
			if(choice2==7)
			{
			dbMonth[counter]="July";	
			}
			if(choice2==8)
			{
			dbMonth[counter]="August";	
			}
			if(choice2==9)
			{
			dbMonth[counter]="September";	
			}
			if(choice2==10)
			{
			dbMonth[counter]="October";	
			}
			if(choice2==11)
			{
			dbMonth[counter]="November";	
			}
			if(choice2==12)
			{
			dbMonth[counter]="December";	
			}
			
			System.out.println(""+dbMonth[counter]+"  Salary <max 8 person>" );
			for (int i = 0; i<8; i++) 
    		{
    		System.out.println("Enter person "+ (i + 1) +" <Press ENTER to exit > :");
    		familyName=input.next();//system will capture & read user input
      		
      		if (familyName.length()== 0)
      		{
      			//dbName[counter] = familyName; //sytem will read data & store in specific array;
      			displayMenu();
      		} 
      		else
      		{
      			dbName[counter] = familyName; //sytem will read data & store in specific array
      			counter++;
      			//displayMenu();
      		}   
    		System.out.println("Enter Salary:");
    		salary=input.nextInt();
    		dbSalary[counter] = salary;
    		
    		noOfPerson += 1;//when user add more information code + 1
      		counter++;
    		}
			
		counter++;
 		}
 		static void detailSalary()
		{
			System.out.print("Enter month (1 for Jan - 12 for dec): ");
			choice2=input.nextInt();
			
			if(choice2==1)
			{
				System.out.println("Family Income for January");
				for (int i = 0; i<counter; i++) 
    			{
    			System.out.println(""+dbName[counter]+""+dbMonth[counter]+ ""+dbSalary[counter]+"" );	
    			}
			}
			if(choice2==2)
			{
			dbMonth[counter]="Febuary";	
			}
			if(choice2==3)
			{
			dbMonth[counter]="March";	
			}
			if(choice2==4)
			{
			dbMonth[counter]="April";	
			}
			if(choice2==5)
			{
			dbMonth[counter]="May";	
			}
			if(choice2==6)
			{
			dbMonth[counter]="June";	
			}
			if(choice2==7)
			{
			dbMonth[counter]="July";	
			}
			if(choice2==8)
			{
			dbMonth[counter]="August";	
			}
			if(choice2==9)
			{
			dbMonth[counter]="September";	
			}
			if(choice2==10)
			{
			dbMonth[counter]="October";	
			}
			if(choice2==11)
			{
			dbMonth[counter]="November";	
			}
			if(choice2==12)
			{
			dbMonth[counter]="December";	
			}
				
 		}
 static void reportSalary()
 {
 }
  static void exit()
 {
 				 System.out.println("===============================================================================");
        		 System.out.print("\n\t\t\t\tEXITING THE PROGRAM");
        		 System.out.print("\n\t\tThank You For Using the Program.Good Bye!");
        		 System.out.print("\n\n");
        		 System.out.println("===============================================================================");
 }
}//end of class FamilyIncome [/INDENT]

Or this if i used what i know

You are over complicating things.... You gotta think a little bit differently when programming...

You do not have to make several different choice variables. Remember, the user makes 1 and only 1 choice per menu cycle. Once the choice is read in and acted on, you can leave it....... When the user is asked again you can replace the SAME choice with a new one which will then be acted on again and left.

It looks like you are still extremely new to programming and I have a hard time believing your teacher would give you this level of a project when you have so little understanding.

I'm going to give you what I did in a nutshell. Try to study it so you can better understand the better way of doing it.

import java.util.Scanner;

public class Family {
	static Scanner input = new Scanner(System.in).userDelimiter("\n");

	static int[] month = new int[8];
	static String[] person = new String[8];
	static int[] salary = new int[8];
	static int len = 0;

	public static void main(String[] args) {
		String[] mos = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

		while (true) {
			System.out.println("\n**********Family Income**********");
			System.out.println("1) Enter monthly salary");
			System.out.println("2) Display detailed salary by month");
			System.out.println("3) Quick glance at monthly salary");
			System.out.println("4) Exit");

			int choice = askForInt("Please select your choice (1-4) : ");
			switch(choice) {
			case 1:
				if (len >= 8) {
					System.out.println("Too many entries");
					continue;
				}
				month[len] = askForInt("Enter month (1 for Jan - 12 for dec): ");
				person[len] = askForString("Enter person "+ (len+1) +" (Press ENTER to exit) : ");
				if (person[len] == "") {
					month[len] = 0;
					continue;
				}
				salary[len] = askForInt("Enter Salary : ");
				break;
			case 2:
				break;
			case 3:
				break;
			}
		}
	}

	private static int askForInt(String text) {
		System.out.print(text);
		return input.nextInt();
	}

	private static String askForString(String text) {
		System.out.print(text);
		return input.next();
	}
}

I took out the constructor. looks like you have yet to learn that.

You also need try catch statements to catch the exceptions when someone enters something wrong. I wish ya the best of luck.

This article has been dead for over six months. Start a new discussion instead.