Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default New to Java and Java Forums - need help.

    Hi Guys,

    Im new to java and this site, please help me if anyone.

    Im trying to make a file which includes 3 or more different types of data such as (car, make, and cost).
    I have searched many forums and tutorials but seems not very helpful or maybe im not understanding.

    My program should enable the user to add new data, edit current, search, sort and display.

    Below is so far im trying to do...

    Java Code:
    import java.io.*;
    
    import javax.swing.JOptionPane;
    
    public class File 
    {
    	public static void main (String args []) throws IOException
    	{
    		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);
    	}
    }
    Last edited by jaymaan; 01-31-2013 at 09:18 AM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default Re: New to Java and Java Forums - need help.

    Where are you stuck?
    Errors?
    Exceptions?
    Please post them in full, highlighting the line(s) on which they occur.

    And please, when posting code, use [code] tags [/code] so that it retains its formatting.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Quote Originally Posted by Tolls View Post
    Where are you stuck?
    Errors?
    Exceptions?
    Please post them in full, highlighting the line(s) on which they occur.

    And please, when posting code, use [code] tags [/code] so that it retains its formatting.
    Thank you, i didnt know how to put it that way , in the future would do so.

    Actually, my code only displays the first model, cost, and make....i want to know how can i display all three of them in each line.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default Re: New to Java and Java Forums - need help.

    So what are you seeing in your output, and what do you want to see.
    You can also edit your original post to put code tags in there so the code is readable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Below is the output I get when I run the program, Im not able to figure out how can I display all three of cars in this format in each line.

    New to Java and Java Forums - need help.-screen-shot-2013-01-31-3.21.11-pm.png

  6. #6
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Here I have added a Menu for the program to choose different options, I have started with the option to display all first, and I not able figure out how can I display all the 3 cars as the output is same as the above post i have posted which displays only the Lamborghini.

    If anyone have idea how to do that please let me know.

    I have yet to start working on other options as search sort edit and creating new entry into file.

  7. #7
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Quote Originally Posted by jaymaan View Post
    Here I have added a Menu for the program to choose different options, I have started with the option to display all first, and I not able figure out how can I display all the 3 cars as the output is same as the above post i have posted which displays only the Lamborghini.

    If anyone have idea how to do that please let me know.

    I have yet to start working on other options as search sort edit and creating new entry into file.
    Java Code:
    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);
    		}
    	}
    }

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default Re: New to Java and Java Forums - need help.

    Build up the message as a String (use a StringBuilder).
    Separate the lines by appending a "\n".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Quote Originally Posted by Tolls View Post
    Build up the message as a String (use a StringBuilder).
    Separate the lines by appending a "\n".
    Sorry I didnt get what you mean, and how to do it.

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default Re: New to Java and Java Forums - need help.

    "\n" is the new line character.
    So build the lines as you are at the moment for the single line, and join them together.
    Make sure there is a "\n" between each line:
    Java Code:
    String line1 = "this is line1";
    String line2 = "this is line2";
    StringBuilder sb = new StringBuilder();
    sb.append(line1);
    sb.append("\n");
    sb.append(line2);
    JOptionPane.showMessageDialog(null, sb.toString());
    That will show those two lines witha line break in between.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Thanks a lot Tolls.

    I have now 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.

    Java Code:
    		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)

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default Re: New to Java and Java Forums - need help.

    That exception is not thrown (as far as I can tell) from that code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Following is my class to write new row to the .csv file. I am able to put the inputs from the user using the JOption Dialog box. The problem is as there is COST which is double I cannot put into the string. Is there other to write the user inputs into a new row at the end of current data???

    Even if I manage to do this my readfile is not able to read the new row as I will need to manually add another String for the new row to display lol....i have no idea yet how to do that....but first I would like to know if there is anyother way to write the user inputs into a new row at the end of current data.

    THANKS ALOT

    Java Code:
    public class writefile {
    	
    	public void writedata() throws IOException{
    	
    		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",true);
    			BufferedWriter outputBuffer = new BufferedWriter(outputFile);
    	
    			String Array [] = {b, c};
    			String Writeable = "," + "\n";
    	
    			for(String item: Array){
    			Writeable += item + ",";
    	}
    			
    	Writeable = Writeable.substring(0, Writeable.length() - 1);
    
    	outputBuffer.write(Writeable);
    	outputBuffer.close();
    	
    	}
    }

  14. #14
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default Re: New to Java and Java Forums - need help.

    Have you looked at what the carsDB.csv file contains after you've written to it?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Yes Tolls,

    It adds new data to the last row correctly but as I cannot(currently not able to) add the cost (double) to the string, the updated .csv file shows something like below:

    New to Java and Java Forums - need help.-screen-shot-2013-02-01-7.12.30-pm.png
    New to Java and Java Forums - need help.-screen-shot-2013-02-01-7.12.50-pm.png
    New to Java and Java Forums - need help.-screen-shot-2013-02-01-7.13.10-pm.png

  16. #16
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Below is my file reader class for reading the file.

    As you can see I have used String a , b, c, d, for each row. After I have added a new line and try to read the file it will not read the new row as obivously there is no String e. Can this be done by any loop to read until the rows end?

    Thanks.

    Java Code:
    import java.io.File;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    
    
    public class readfile 
    {
    	private Scanner s;
    	
    	public void openFile() {
    	try{
    	s = new Scanner(new File("carsDB.csv"));
    	}
    	catch (Exception e){
    		JOptionPane.showMessageDialog(null,"File not found!");
    		}
    	}
    	
    	public void readFile(){
    		while(s.hasNext()){
    			String a = s.next();
    			String b = s.next();
    			String c = s.next();
    			String d = s.next();
    		
    			JOptionPane.showMessageDialog(null,"Following cars are currently available:-" + "\n"
    										+ a + "\n" + b + "\n" + c + "\n" + d, 
    										"Car Database",JOptionPane.INFORMATION_MESSAGE);
    		}
    	}
    	public void closeFile(){
    		s.close();
    	}
    }

  17. #17
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default Re: New to Java and Java Forums - need help.

    So it's not showing the last line?

    OK.
    How does the other code call this?

    Write some debug println's so show what a, b, c, and d are:
    Java Code:
    System.out.println("-" + d + "-");
    The '-' are so you can see if there's a blank in there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    Its not showing the last line which I have used the program to write to the .csv file.

    As you can see my readfile class, which i have used to read the file from has Strings to read the file.

    Problem I'm having is whenever each time I add new entry to database, I have to manually add another String to display the new entry, else it only displays the previous same entries. Is there any better way or method I can use so that it will automatically done?

  19. #19
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: New to Java and Java Forums - need help.

    I can successfully write String , double , String accordingly to my file and is working great. I was not able to convert double into string but now I can with help of

    String convertDouble = Double.toString(aa);

    The problem only remains with my read file method. I'm not able to display the new added entries to the .csv file with my current read file class unless I manually add a String for them to display.

  20. #20
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: New to Java and Java Forums - need help.

    Read the file into a dynamic data structure, like a List or StringBuilder. It doesn't matter how many lines your csv file has. Your program will crash if you deleted the last two lines from the file as you're calling s.next() four times.
    Java Code:
    public void readFile(){
            //list... stringbuilder.. whatever
            while(s.hasNext()){
                // fill list           
            }
            // convert list to string
            JOptionPane.showMessageDialog(null,converted_string);
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Page 1 of 2 12 LastLast

Similar Threads

  1. Hi, Java Forums
    By SolidCobra in forum Introductions
    Replies: 1
    Last Post: 10-06-2008, 02:37 AM
  2. Replies: 7
    Last Post: 07-22-2008, 06:54 PM
  3. new to java new to forums
    By mctruck in forum Introductions
    Replies: 5
    Last Post: 02-16-2008, 04:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •