Question

Using Java; Consider the following text file format. Ruminant Shaman 30 Elentriel Shaman 70 Aelkine Druid...

Using Java; Consider the following text file format.

Ruminant
Shaman
30
Elentriel
Shaman
70
Aelkine
Druid
29
Barbatus
Druid
70
Ukaimar
Mage
47
Mariko
Priest
33
Farmbuyer
Priest
70
Valefar
Warlock
42
Teslar
Paladin
64
Nerdsbane
Hunter
12

We wish to store information regarding several characters in a video game. Each character has a name, and a class, and a level. This information is repeated, 3 lines per character, for any number of characters. To simplify processing, the very first line of the file will be an integer that provides the number of characters stored in the file. Note that this number represents the number of records, not the number of lines (each record consists of one name, one class, and one level, i.e. three lines in the file).   

Write a program that opens the file shown above and reads in the contents. Use 3 separate arrays to hold the character names (String), classes (String), and levels (int). Once you have read in the entire file, print the information to the screen in the following format.

DESIRED OUTPUT:

Name           Class          Level

Ruminant       Shaman         30

Elentriel      Shaman         70

Aelkine        Druid          29

Barbatus       Druid          70

Ukaimar        Mage           47

Mariko         Priest         33

Farmbuyer      Priest         70

Valefar        Warlock        42

Teslar         Paladin        64

Nerdsbane      Hunter         12

Here is my code so far; however I am unsure how to read in and allocate the strings with each other and then to the respective level so all i have is reading in the character level and allocating that to an array.

public static void main(String[] args) throws FileNotFoundException {
  
int [] charLevel;
String [] charName;
String [] charClass;

try {
//read in levels(integer values) of the characters from the file
Scanner level = new Scanner(new File("character.txt"));

int count = 0;
while(level.hasNextLine()) {
level.nextLine();
count++;
}
level.close();

//assign levels to an array
charLevel = new int[count];
level = new Scanner(new File("character.txt"));
//declare scanner to read levels from start of file
for (int i=0; i<count; i++) {
//add each level from the file to the array charLevel
charLevel[i] = level.nextInt();
}
level.close();
  
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("Unable to open file!");
  
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN JAVA:

import java.util.Scanner;
import java.io.File;
class Characters
{
public static void main(String args[])
{
try
{
Scanner in=new Scanner(new File("character.txt")); //opening the file
int n=in.nextInt(); //reading number of characters to be read from file
//creating arrays of the size read
String names[]=new String[n];
String classes[]=new String[n];
int levels[]=new int[n];
//reading the different characters into the arrays
for(int i=0;i<n;i++)
{
names[i]=in.next();
classes[i]=in.next();
levels[i]=in.nextInt();
}
in.close();
//output
System.out.println("Name Class Level");
for(int i=0;i<n;i++)
System.out.printf("%-10s %-10s %d\n",names[i],classes[i],levels[i]);
  
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Using Java; Consider the following text file format. Ruminant Shaman 30 Elentriel Shaman 70 Aelkine Druid...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Suppose you define a file format to describe an array of java.awt.Rectangle objects. You start with...

    Suppose you define a file format to describe an array of java.awt.Rectangle objects. You start with an integer saying the length of the array, then a sequence of integers describing the x, y, width, and height parameters that can feed into the constructor for java.awt.Rectangle (according to the documentation). Let's define a method that will return an array of Rectangles given a String representing a filename. You could test the method by creating a file such as this: 2 0...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this...

    Step 1: Getting Started Create a new .java file named Lab12.java. At the beginning of this file, include your assignment documentation code block. After the documentation block, you will need several import statements. import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; Next, declare the Lab12 class and define the main function. public class Lab12 { public static void main (String [] args) { Step 2: Declaring Variables For this section of the lab, you will need to declare...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT