Note that there are some explanatory texts on larger screens.

plurals
  1. POJava outputStream not Printing Correctly
    primarykey
    data
    text
    <p>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:</p> <pre><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 &gt;= 1) { for (index = 0; index &lt; 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(); } } </code></pre> <p>Example Input:</p> <pre><code>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 </code></pre> <p>Example Output:(PatientData.txt)</p> <pre><code>Patient Data: Pet Name Pet Type Number of Visits dog 8 cat 3 rodent 1 bird 6 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload