Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: [Beginner]Database project help

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default [Beginner]Database project help

    So I am working on making a code for my Aunt who is a doctor, This is what the code does
    1.Asks Name
    2.Asks Gender(press 1 for male, 2 for female)
    3.Occupation(1-student,2-job,3-Others)
    if we select 3 we are made to write the other occupation
    4.Ask age
    5.Shows the Name,Gender,Occupation and age in a formal manner
    proceeds to ask if we wanna add another patient
    So What I want this code to be able to do is but have no idea how are:-
    1. I want step 5 to be written on a text file in my computer and continue to add as we keep on adding more patients.
    2. I want to be able to search through the patient based on the age,name,etc. You get the drill.
    You don't need to write the full code if you don't want to but it will be really Helpful if anyone can teach me how to write informations in a text with example and how i can read those text files later and search them based on age and name
    Here is the code
    Ignore the Search by name,age etc part of the program I Havnt worked on them yet as I dont know how to write the informations I get to a text file in my computer and then read then in java and search thorough the database
    SUBCLASS
    import java.util.InputMismatchException;
    import java.util.Scanner;
    public class methods {
    	int serial=1;
    	String nameFinal;
    	String occupationFianl;
    	String genderFinal;
    	String drugFinal;
    	String freqFinal;
    	String routeFinal;
    	int ageFinal;
    	long numFinal;
     
    	String durationFinal;
    	//name
    	public void InputName() {
    		Scanner input = new Scanner(System.in);
    		System.out.println("enter the name of the patient.");
    		String name= input.nextLine();
    		nameFinal=name;
    	}
    	//age
    	public void InputAge() {
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter the age of the patient.");
    		int age=input.nextInt();
    		ageFinal=age;
    	}
    	//gender
    	public void Inputgender() {
    		boolean rep=true;
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter the Gender of the patient...1-male, 2-female.");
    		do {
    			try {
    				int gender= input.nextInt();
    			if (gender==1) {
    				genderFinal="Male";
    				rep=false;
     
    			}
    			else if(gender==2) {
    				genderFinal="Female";
    				rep=false;
    			}
    			else throw new InputMismatchException("wrong input");
    			}
    			catch (Exception e)
    			{
    				input.reset();
    					rep=true;
    			}
    		}while(rep==true);
    	}
    	//occupation
    		public void InputOcc() {
     
    			boolean rep=true;
    			Scanner input = new Scanner(System.in);
    			System.out.println("Enter the occupation of student.");
    			System.out.println("1-fulltime Student, 2-Dropout School, 3-employed, 4-Unemployed.");
    			do {
    			try {
    				int Occ= input.nextInt();
    			if (Occ==1) {
    				occupationFianl="Fulltime Student";
    				rep=false;
     
    			}
    			else if(Occ==2) {
    				occupationFianl="Dropout School";
    				rep=false;
    			}
    			else if(Occ==3) {
    				occupationFianl="Employed";
    				rep=false;
    			}
    			else if(Occ==4) {
    				occupationFianl="Unemployed";
    				rep=false;
    			}
    			else throw new InputMismatchException("wrong input");
    			}
    			catch (Exception e)
    			{
    				input.reset();
    					rep=true;
    			}
    		}while(rep==true);
     
     
    		}
    	//drugasbused
    		public void InputDrugUsed() {
    			Scanner input = new Scanner(System.in);
    			System.out.println("Enter the Drugs used");
    		input.reset();
    			String drug= input.nextLine();
     
    			drugFinal=drug;
     
    		}
    	//durationofdrug
    		public void InputDrugDuration() {
    			Scanner input = new Scanner(System.in);
    			boolean rep=true;
    			System.out.println("enter the duration(1-1 year & more, 2- 6months or less.)");
     
     
    			do {
    				try {
    					int duration= input.nextInt();
    				if (duration==1) {
    					durationFinal="1 year& more.";
    					rep=false;
     
    				}
    				else if(duration==2) {
    					durationFinal="6 months or less.";
    					rep=false;
    				}
     
    				else throw new InputMismatchException("wrong input");
    				}
    				catch (Exception e)
    				{
    					input.reset();
    						rep=true;
    				}
    			}while(rep==true);
    		}
    	//frequency
    		public void InputFrequency() {
    			boolean rep = true;
    			Scanner input = new Scanner(System.in);
    		System.out.println("Enter the frequency of drug used");
    		System.out.println("1-daily, 2-twice a week, 3-once a week");
    		do {
    			try {
    				int freq= input.nextInt();
    			if (freq==1) {
    				freqFinal="Daily.";
    				rep=false;
     
    			}
    			else if(freq==2) {
    				freqFinal="twice a week.";
    				rep=false;
    			}
    			else if(freq==3) {
    				freqFinal="Once a week.";
    				rep=false;
    			}
    			else throw new InputMismatchException("wrong input");
    			}
    			catch (Exception e)
    			{
    				input.reset();
    					rep=true;
    			}
    		}while(rep==true);
    		}
    	//route
    		public void Inputroute() {
    			boolean rep=true;
    			Scanner input = new Scanner(System.in);
    			System.out.println("means of route.");
    			System.out.println("1-injection, 2-snorting");
    			do {
    				try {
    					int route= input.nextInt();
    				if (route==1) {
    					routeFinal="Injection";
    					rep=false;
     
    				}
    				else if(route==2) {
    					routeFinal="Snorting";
    					rep=false;
    				}
    				else throw new InputMismatchException("wrong input");
    				}
    				catch (Exception e)
    				{
    					System.out.println("wrong input, Try again");
    					input.reset();
    						rep=true;
    				}
    			}while(rep==true);
    		}
    		//number
    		public void InputNum() {
    			Scanner input = new Scanner(System.in);
    			System.out.println("enter contacnt nnumber.");
    			long num=input.nextLong();
    			numFinal=num;
    		}
    	//getage
    		public int getAge() {
    			return ageFinal;
    		}
    	//getname
    		public String getName() {
    			return nameFinal;
    		}
    	//showlist
    		public void showlist() {
     
    		System.out.println("Name(Abuser): "+nameFinal);
    		System.out.println("Age : "+ageFinal);
    		System.out.println("Sex : "+genderFinal);
    		System.out.println("Occupation(Victim) : "+occupationFianl);
    		System.out.println("Drug Abused : "+drugFinal);
    		System.out.println("Frequency of Dosage  : "+freqFinal);
    		System.out.println("Route  : "+routeFinal);
    System.out.println("Phone number: "+numFinal);
     
     
    		}
    	//search by namePat
    		//searchby occupationPat
    		//searchbyage
    public void Collect() {
    	InputName();
    	InputAge();
    Inputgender();
    	InputOcc();
    	InputDrugUsed();
    	InputDrugDuration();
    	InputFrequency();
    Inputroute();
    	InputNum();
    showlist();
    System.out.println("");
    System.out.println("");
    }
     
    }
    MAIN CLASS
    import java.util.*;
    public class mainbody {
     
    	public static void main(String[] args) {
    	ArrayList<methods> arraysOfObj=new ArrayList<methods>();
    	Scanner input = new Scanner(System.in);
    	String answer;
    	int objNO=0;
    	boolean repeat=true;
    	do {
    		arraysOfObj.add(new methods());
    		arraysOfObj.get(objNO).Collect();
    			objNO++;
    			System.out.println("wanna add more?");
    			answer=input.nextLine();
    			if(answer.equalsIgnoreCase("yes")) {
    				repeat=true;
    			}
    			else repeat=false;
    	}while(repeat==true);
     
    	//searches
    	int arrays[]=new int[arraysOfObj.size()];
    	do {
    		System.out.println("press 1 to search Database by name");
    		System.out.println("press 2 to seach by age");
    		System.out.println("press 3 to search by occupation status");
    		System.out.println("press 4 to search by Duration");
    		System.out.println("press 5 to search by frequency");
    		System.out.println("press 6 to show all list");
    		int choice=input.nextInt();
    		//search by name
    		if(choice==1) {
    			String nameS=input.nextLine();
    			for(int i=0;i<arrays.length;i++) {
    				if(arraysOfObj.get(i).nameFinal.equalsIgnoreCase(nameS)) {
    					arraysOfObj.get(i).showlist();
    				}
    			}
    		}
    		if(choice==2) {
    			int ageS=input.nextInt();
    			for(int i=0;i<arrays.length;i++) {
    				if(arraysOfObj.get(i).getAge()==ageS) {
    					arraysOfObj.get(i).showlist();
    				}
    			}
    		}
    		if(choice==3) {
    			String OccS=input.nextLine();
    			for(int i=0;i<arrays.length;i++) {
    				if(arraysOfObj.get(i).occupationFianl.equalsIgnoreCase(OccS)) {
    					arraysOfObj.get(i).showlist();
    				}
    			}
    		}
    		if(choice==4) {
    			String Duration=input.nextLine();
    			for(int i=0;i<arrays.length;i++) {
    				if(arraysOfObj.get(i).durationFinal.contains(Duration)) {
    					arraysOfObj.get(i).showlist();
     
    	}
     
    }
    }
    		if(choice==5) {
    			String freq=input.nextLine();
    			for(int i=0;i<arrays.length;i++) {
    				if(arraysOfObj.get(i).freqFinal.contains(freq)) {
    					arraysOfObj.get(i).showlist();
    	}
    			}
    		}
    		if(choice==6) {
    			for(int i=0;i<arrays.length;i++) {
    		arraysOfObj.get(i).showlist();
    				}
    			}
    	} while(1!=0);
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [Beginner]Database project help

    how to write informations in a text file
    You can use a PrintWriter class object to write text to a file. The print and println methods will write Strings to the file.

    Before writing any code, you need to design the layout of the file so that the data can be read back and updated later.

    Write a small test program that writes and reads a file with hard coded values to see how to use the classes and methods BEFORE trying to put code into your existing program. After you have worked out the techniques, then look at merging the code into your program.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: [Beginner]Database project help

    Quote Originally Posted by Norm View Post
    You can use a PrintWriter class object to write text to a file. The print and println methods will write Strings to the file.

    Before writing any code, you need to design the layout of the file so that the data can be read back and updated later.

    Write a small test program that writes and reads a file with hard coded values to see how to use the classes and methods BEFORE trying to put code into your existing program. After you have worked out the techniques, then look at merging the code into your program.
    Thank you that helps a lot while leaving space for me to experiment myself
    Can you tell me about how to read too? n search through it?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [Beginner]Database project help

    The Scanner class has methods for reading a simple text file.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. java database project help?
    By Devendra007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 10th, 2013, 09:10 PM
  2. Jar project to use Derby Database
    By Ma_2 in forum JDBC & Databases
    Replies: 1
    Last Post: July 17th, 2012, 11:03 AM
  3. Beginner - Where to put code to connect to database
    By newbiee in forum JDBC & Databases
    Replies: 1
    Last Post: May 17th, 2012, 05:40 AM
  4. Beginner Project
    By Saberunna in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 9th, 2011, 08:49 PM
  5. Best beginners project in Java
    By Fendaril in forum Java Theory & Questions
    Replies: 3
    Last Post: February 10th, 2009, 08:52 AM

Tags for this Thread