Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

New member - To creating a file and reading, adding new entries.

$
0
0
Hello people,

Im new here. It will very grateful if someone can help me with my program. Below is my code.

I have put a menu for the user, and I need to provide those options to user to do with the file for CARS.

I have managed to make a menu with options, and have made a file (not sure if correct or not).

When I want to display the current file (of 3 different cars, cost and make), it is only showing me 1.

I have also attached the output for you to have a look.

import java.io.*;
import javax.swing.JOptionPane;

public class File 
{
	public static void main (String args []) throws IOException
	{
		Object[] selectionValues = {"Add a new object manually", "Search and display details",
				"Edit an object", "Sort the objects", "Display all", "Save objects to a file", 
				"Retreive objects from a file", "Quit"};
	    
		String initialSelection = "Add a new object manually";
		
		Object selection = JOptionPane.showInputDialog(null, "Please select option.",
		"Cars Database Menu", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
		
		JOptionPane.showMessageDialog(null,"You have choosen to " + selection + ".");
		
		if (selection == "Display all")
		{
		final FileWriter outputFile = new FileWriter("Cars.txt");
		final BufferedWriter output = new BufferedWriter(outputFile);
		final PrintWriter printstream = new PrintWriter(output);
		
		String Model[]={"Gallardo", "Mustang", "Skyline"};
		int Cost [] = new int [3];
		Cost[0] = 1000;
		Cost[1] = 100;
		Cost[2] = 10;
		String Make[]={"Lamborghini", "Ford", "Nissan"};
		int i = 0;
		
		for(i=0; i<3; i++)
		{
			printstream.println(Model[i]);
			printstream.println(Cost[i]);
			printstream.println(Make[i]);
		}
		printstream.close();
		
		FileReader inputFile = new FileReader("Cars.txt");
		BufferedReader inputBuffer = new BufferedReader(inputFile);
		
		String line1 = inputBuffer.readLine();
		String line2= inputBuffer.readLine();
		String line3= inputBuffer.readLine();
		
		JOptionPane.showMessageDialog(null, line1 + line2 + line3);
		
		inputBuffer.close();
		System.exit(0);
		}
	}
}



Unable to upload the screenshot for output.....

Error The server returned an error during upload

Anyways output is as below:

Gallardo1000Lamborghini

Output expected:

Model, Cost, Make
Gallardo, 1000, Lamborghini
Mustang, 100, Ford.
Skyline, 10, Nissan

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>