Writing to text file

Writing to a text file

Page 1 of 1

4 Replies - 2759 Views - Last Post: 06 January 2009 - 06:51 AM Rate Topic: -----

#1 aLders  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-November 08

Writing to text file

Posted 06 January 2009 - 05:17 AM

This is my code below, i am just wondering how to save my output in a text file:


package Booking;
import java.awt.Component;
import javax.swing.JOptionPane;


public class testing {
	static Component frame;
	/**
	 * @param args
	 * @param frame 
	 */
	public static void main(String[] args) {
		String output, usersNum, name, address, date, lengthOfHire, numPeople, mountHelOutput, mountSexOutput, mountFrameOutput, raceSexOutput, raceFrameOutput, raceAccessOutput;
		int num = 0; 
		int mountBike = 0, mountSmall = 0, mountMed = 0, mountLarge = 0, mountHelmet = 0, mountMale = 0, mountFemale = 0, sexOutput;
		int raceBike = 0, raceSmall = 0, raceMed = 0, raceLarge = 0, raceAccess = 0, raceMale = 0, raceFemale = 0;
		int childBike = 0, numLoop;
		
		
			name = JOptionPane.showInputDialog("Please enter your FULL name");
			address = JOptionPane.showInputDialog("Please enter your FULL address");
			date = JOptionPane.showInputDialog("Please enter date for hire");
			lengthOfHire = JOptionPane.showInputDialog("Please enter how long the hire will be for \n\n Enter in Number of days");
			numPeople = JOptionPane.showInputDialog("Please enter how many people the booking is for");

			numLoop = Integer.parseInt(numPeople);
			int count = 1;
	while ( count <= numLoop) {
	count = count + 1;	
		do {
			
		
				output = "";
			output = "Select the bike you wish to purchase \n\n";
			output = output + "1 - Mountain Bike \n";
			output = output + "2 - Racing Bike \n";
			output = output + "3 - Childrens Bike \n";
			
			usersNum = JOptionPane.showInputDialog(output);
			num = Integer.parseInt(usersNum);
			
			if (num == 1) {
				
				mountBike = mountBike + 1;
				mountSexOutput = "Are you Male or Female?";
				Object[] options = {"Male", "Female"};

				sexOutput = JOptionPane.showOptionDialog(frame, mountSexOutput, "Please Answer",
						JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
						options, options[0]);
				if (sexOutput == JOptionPane.YES_OPTION) {
					mountMale = mountMale + 1;
				} else if (sexOutput == JOptionPane.NO_OPTION) {
					mountFemale = mountFemale + 1;
				}
				  
				mountFrameOutput = "Would you like a Small, Medium or Large Frame?";
				Object[] optionsFrame = {"Small", "Medium", "Large"};
				int mFrame = JOptionPane.showOptionDialog(frame, mountFrameOutput, "Please Answer",	
					JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,							  
					optionsFrame, optionsFrame[2]);
				if (mFrame == JOptionPane.YES_OPTION) {
					mountSmall = mountSmall + 1;
				} else if (mFrame == JOptionPane.NO_OPTION) {
					mountMed = mountMed + 1;
				} else if (mFrame == JOptionPane.CANCEL_OPTION) {
					mountLarge = mountLarge + 1;
				} 

				mountHelOutput = "Do you require a Helmet?";
				Object[] optionsHel = {"Yes I do", "No I dont"};
				int hel = JOptionPane.showOptionDialog(frame, mountHelOutput, "Please Answer",
						JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
						optionsHel, optionsHel[0]);
				if (hel == JOptionPane.YES_OPTION) {
					mountHelmet = mountHelmet + 1;
				} else if (hel == JOptionPane.NO_OPTION) {
					mountHelmet = mountHelmet + 0;
				}
				
				
				
			}
				
				
					
			
			if (num == 2) {
				
				raceBike = raceBike + 1;
				raceSexOutput = "Are you Male or Female?";
				Object[] optionsRace = {"Male", "Female"};

				int raceSex = JOptionPane.showOptionDialog(frame, raceSexOutput, "Please Answer",
						JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
						optionsRace, optionsRace[0]);
				if (raceSex == JOptionPane.YES_OPTION) {
					raceMale = raceMale + 1;
				} else if (raceSex == JOptionPane.NO_OPTION) {
					raceFemale = raceFemale + 1;
				}
				
				raceFrameOutput = "Would you like a Small, Medium or Large Frame?";
				Object[] optionsRFrame = {"Small", "Medium", "Large"};
				
				int rFrame = JOptionPane.showOptionDialog(frame, raceFrameOutput, "Please Answer",	
					JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,							  
					optionsRFrame, optionsRFrame[2]);
				if (rFrame == JOptionPane.YES_OPTION) {
					raceSmall = raceSmall + 1;
				} else if (rFrame == JOptionPane.NO_OPTION) {
					raceMed = raceMed + 1;
				} else if (rFrame == JOptionPane.CANCEL_OPTION) {
					raceLarge = raceLarge + 1;
				} 
				
				
				raceAccessOutput = "Do you require Helmet and Panniers?";
				Object[] optionsPan = {"Yes I do", "No I dont"};
				int rHel = JOptionPane.showOptionDialog(frame, raceAccessOutput, "Please Answer",
						JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
						optionsPan, optionsPan[0]);
				if (rHel == JOptionPane.YES_OPTION) {
					raceAccess = raceAccess + 1;
				} else if (rHel == JOptionPane.NO_OPTION) {
					raceAccess = raceAccess + 0;
				}
				
			}
			
			if (num==3) {
				childBike = childBike + 1;
				
				JOptionPane.showMessageDialog(null, "Helmet is included with bike and must be worn!", "A Message",
						JOptionPane.INFORMATION_MESSAGE);
		}	
		}
		while (num <1 || num > 3);
		}
		
			String finalBooking;
			finalBooking = "		  Booking Information" + 
			"\n\nName: " + name +
			"\nAddress: " + address +
			"\nDate of Hire: " + date +
			"\nNumber of People: " + numPeople +
			"\nThe Hire is for: " + lengthOfHire + " Days" +
			
			"\n\nYou Have Booked:" +
			"\n\n" + childBike + " Childrens Bikes" +
			
			"\n\n" + mountBike + " Mountain Bikes:" +
			"\n	  " + mountMale + " Male" +
			"\n	  " + mountFemale + " Female" +
			"\n	  " + mountSmall + " Small Frames" +
			"\n	  " + mountMed + " Medium Frames" +
			"\n	  " + mountLarge + " Large Frames" +
			"\n	  " + mountHelmet + " Helmets" +
			
			"\n\n" + raceBike + " Racing Bikes:" +
			"\n	  " + raceMale + " Male" +
			"\n	  " + raceFemale + " Female" +
			"\n	  " + raceSmall + " Small Frames" +
			"\n	  " + raceMed + " Medium Frames" +
			"\n	  " + raceLarge + " Large Frames" +
			"\n	  " + raceAccess + " Helmet and Panniers" +
			
			"\n\n\n You may wish to keep a copy of this \n\n Click OK to complete order";
			
		JOptionPane.showMessageDialog(null, finalBooking, "Booking Details",
				JOptionPane.INFORMATION_MESSAGE);
		
		
		System.exit(0);
		}
		}
	
			
		// TODO Auto-generated method stub





Thanks in advance

x

Is This A Good Question/Topic? 0
  • +

Replies To: Writing to text file

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 125
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Writing to text file

Posted 06 January 2009 - 05:33 AM

Check out this link, it shows you an example of how to write into text files:
http://www.java2s.co...ngintoafile.htm

I hope this helps, good luck in your program ;)

This post has been edited by ayman_mastermind: 06 January 2009 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 aLders  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-November 08

Re: Writing to text file

Posted 06 January 2009 - 05:48 AM

It helps slightly.... but i dont really understand what its doing and where its doing it lol.

I am very very basic when it comes to java, would you be able to explain it?

Thanks x
Was This Post Helpful? 0
  • +
  • -

#4 aLders  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 12-November 08

Re: Writing to text file

Posted 06 January 2009 - 06:07 AM

Its fine i got it done :D

Thanks for your help mate x
Was This Post Helpful? 0
  • +
  • -

#5 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 125
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Writing to text file

Posted 06 January 2009 - 06:51 AM

Your welcome :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1