Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7595585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:42:27+00:00 2026-05-30T21:42:27+00:00

I am making a simple Java program for class that is supposed to output

  • 0

I am making a simple Java program for class that is supposed to output the variable petName, petType, and numVisits to a txt file called “PatientData.txt”. I have petType and numVisits printing correctly, but not petName. I am almost positive it has something to do with my first junk statement since petType is the only String that has to capture 2+ words. Here is my code:

import java.util.Scanner;
import java.io.*;
public class AcmeClinic
{
 public static void main(String[] args )
 {
  Scanner keyboard = new Scanner(System.in);
  PrintWriter outputStream = null;

  try
  {
   outputStream = new PrintWriter(new FileOutputStream("PatientData.txt"));
  }

  catch(FileNotFoundException e)
  {
   System.out.println("Unable to create the output file.");
   System.exit(0);
  }

  System.out.println("Enter the number of pets to store information for:");
  int amount = keyboard.nextInt();
  String [] petNames = new String [amount];
  String [] petTypes = new String [amount];
  int [] numVisits = new int [amount];
  int index;
  String junk;
  outputStream.println("Patient Data:");
  outputStream.println("Pet Name Pet Type Number of Visits");
  if (amount >= 1)
  {
   for (index = 0; index < amount; index++)
   {
    System.out.println("Type the pet name, then press Enter:");
    petNames[index] = keyboard.nextLine();
    junk = keyboard.nextLine();
    System.out.println("Type the animal type (dog, cat, bird, rodent), then press Enter:");
    petTypes[index] = keyboard.nextLine();
    System.out.println("Type the number of visits last year, then press Enter:");
    numVisits[index] = keyboard.nextInt();
    outputStream.printf("%8s %-8s %-8d%n",petNames[index],  petTypes[index],numVisits[index]);
   }
  }

  outputStream.close();
 }
}

Example Input:

Enter the number of pets to store information for:
4
Type the pet name, then press Enter:
Champ
Type the animal type (dog, cat, bird, rodent), then press Enter: 
dog
Type the number of visits last year, then press Enter:
8
Type the pet name, then press Enter:
Bob
Type the animal type (dog, cat, bird, rodent), then press Enter:
cat
Type the number of visits last year, then press Enter:
3
Type the pet name, then press Enter:
Mickey
Type the animal type (dog, cat, bird, rodent), then press Enter:
rodent
Type the number of visits last year, then press Enter:
1
Type the pet name, then press Enter:
Polly
Type the animal type (dog, cat, bird, rodent), then press Enter:
bird
Type the number of visits last year, then press Enter:
6

Example Output:(PatientData.txt)

Patient Data:
Pet Name Pet Type Number of Visits
         dog      8       
         cat      3       
         rodent   1       
         bird     6       
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:42:28+00:00Added an answer on May 30, 2026 at 9:42 pm

    The nextInt() was causing the immediate nextLine() so avoided using it. This will work for 2 or more words as well…

    System.out.println("Enter the number of pets to store information for:");
    int amount = Integer.parseInt(keyboard.nextLine());
    String [] petNames = new String [amount];
    String [] petTypes = new String [amount];
    int [] numVisits = new int [amount];
    outputStream.println("Patient Data:");
    outputStream.println("Pet Name Pet Type Number of Visits");
    
    for (int index=0;index < amount; index++) {
        System.out.println("Type the pet name, then press Enter:");
        petNames[index] = keyboard.nextLine();
        System.out.println("Type the animal type (dog, cat, bird, rodent), then press Enter:");
        petTypes[index] = keyboard.nextLine();
        System.out.println("Type the number of visits last year, then press Enter:");
        numVisits[index] = Integer.parseInt(keyboard.nextLine());
        outputStream.printf("%8s %-8s %-8d%n", petNames[index], petTypes[index], numVisits[index]);
    }
    

    As mentioned earlier, you don’t have to use arrays here. You can do this instead…

    System.out.println("Enter the number of pets to store information for:");
    int amount = Integer.parseInt(keyboard.nextLine());
    outputStream.println("Patient Data:");
    outputStream.println("Pet Name Pet Type Number of Visits");
    
    String petName = new String();
    String petType = new String();
    int numVisit = 0;
    
    for (int index = 0; index < amount; index++) {
        System.out.println("Type the pet name, then press Enter:");
        petName = keyboard.nextLine();
        System.out.println("Type the animal type (dog, cat, bird, rodent), then press Enter:");
        petType = keyboard.nextLine();
        System.out.println("Type the number of visits last year, then press Enter:");
        numVisit = Integer.parseInt(keyboard.nextLine());
        outputStream.printf("%8s %-8s %-8d%n", petName, petType, numVisit);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a program that allows you to add any class file that extends
I made a simple java program that sends bytes to the parallel port, which
I am making a simple program with Java OpenGL (jogl). In the display() method
I'm writing a simple program that reads and processes file content using a BufferedReader
Here is my code currently. I'm making a java program that seaches Active Directory
I'm making a simple program in Java. Given a set of letters it'll list
Goal I am making a Java class that will give enhanced usability to arrays,
I'm making a simple program that takes text entered in a text box, and
I'm making a very simple 2D RPG in Java. My goal is to do
I'm making a simple tool that will get a string of MySQL commands and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.