0% found this document useful (0 votes)
4 views

Java Lab 13

The document outlines a Java lab assignment focused on writing to files, specifically for creating a character creator for a role-playing game. Users will input character traits such as name, age, strength, intelligence, and dexterity, which must be saved in a specific text format. It also includes constraints on user inputs to ensure data integrity and prevent corruption of the saved file.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Lab 13

The document outlines a Java lab assignment focused on writing to files, specifically for creating a character creator for a role-playing game. Users will input character traits such as name, age, strength, intelligence, and dexterity, which must be saved in a specific text format. It also includes constraints on user inputs to ensure data integrity and prevent corruption of the saved file.

Uploaded by

arya.santosh2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Lab 13 – Writing to Files Introduction

 Create a new Java file (empty Java file), save it to your H: drive, call it lab13
 Insert the following code to set up your program:
 Be sure to copy the test text files to the location where you saved your Java file
import java.util.Scanner;

import java.io.*;

public class lab13 {

public static void main (String args[]) throws IOException{

Scanner input = new Scanner(System.in);

FileWriter fw = new FileWriter("lab13out.txt");

// Insert your solution here

fw.close();

100pt:

You are going to make a character creator for a role playing game. You will write a program which will let the user
determine the following traits of their new character:

1) Name
2) Age
3) Strength
4) Intelligence
5) Dexterity

The user choices for these must be saved into a text document, in a specific format. If they aren’t saved correctly,
the save will be corrupted and all the user’s hard work will be for nothing!

The output file will contain information in the following layout:

firstname=Ian
secondname=Edgeley
age=30
strength=4
intelligence=1
dexterity=5
There are important boundaries on user choices:

- Character names must be longer than 3 letters and shorter than 20. The user must enter the name as
one input which is then split for the file.
- Age must be between 1 and 200 (hey, I don’t know how long orcs live)
- The combination of strength, intelligence and dexterity must not exceed 10.

The user must be able to continue to enter information until they provide an acceptable answer.
Notes:

To write a String to a file:

FileWriter fw = new FileWriter("example.txt");


String test = "Hello World";
fw.write(test);
fw.write("\r\n"); //new line

To get the length of a String:

String test = "Hello World";

int len = test.length(); // len will have a value of 11

You might also like