3 Replies - 286 Views - Last Post: 01 February 2013 - 02:06 AM Rate Topic: ***-- 2 Votes

#1 jaymaan888  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-January 13

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

Posted 31 January 2013 - 02:51 AM

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

Is This A Good Question/Topic? 0
  • +

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

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3727
  • View blog
  • Posts: 13,015
  • Joined: 12-December 12

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

Posted 31 January 2013 - 05:57 AM

You are only reading the first three lines, and Gallardo, 1000, Lamborghini are each on a separate line.

The following will retrieve all lines from the file:

            BufferedReader inputBuffer = new BufferedReader(inputFile);
            
            String aLine, allLines = "";
            while ((aLine = inputBuffer.readLine()) != null) {
                allLines += aLine + '\n';
            }

If you then want the three items to appear on separate lines of the messsage-dialog then you'll need a new variable to count up-to every third line. That is, append commas unless it is a third line, in which case append a newline.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,098
  • Joined: 27-December 08

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

Posted 31 January 2013 - 08:06 AM

Don't compare Strings using the == operator, as it compares their locations in memory rather than their literal values. Compare them using the equals() method instead.
Was This Post Helpful? 1
  • +
  • -

#4 jaymaan888  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-January 13

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

Posted 01 February 2013 - 02:06 AM

Thank you Guys, I have made a new class and put all the codes there and in the main just openFile, readFile and closeFile. It much tidy now.

However I have another issue to write to my file, below is my code and the descriptive errors which I am getting. Have a look if you know what it means.

		if(selection == "Add a new car manually")
		{
			Cars A = new Cars();
		
			String c = JOptionPane.showInputDialog(null,"Please enter the Model of the car.");
			String a = JOptionPane.showInputDialog(null,"Please enter the Cost of the car.");
			String b = JOptionPane.showInputDialog(null,"Please enter the Make of the car.");
		
			double aa;
			aa = Double.parseDouble(a);
		
			A.setCost(aa);
			A.setMake(B)/>;
			A.setModel(c);
		
			JOptionPane.showMessageDialog(null,"You have entered the following:-" + "\n"
											+"Model: " + A.getModel() + "\n"
											+ "Cost: " + A.getCost() + "\n"
											+ "Make : " + A.getMake());
			
			FileWriter outputFile = new FileWriter("carsDB.csv");
			BufferedWriter outputBuffer = new BufferedWriter(outputFile);
			PrintWriter printstream = new PrintWriter(outputBuffer);
			
			printstream.println(aa + b + c);
			printstream.close();
		}



ERRORS

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at readfile.readFile(readfile.java:22)
at Main.main(Main.java:58)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1