Results 1 to 20 of 29
- 01-30-2013, 11:21 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
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.
- 01-30-2013, 12:09 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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 **
- 01-30-2013, 03:24 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
- 01-30-2013, 03:44 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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 **
- 01-31-2013, 09:23 AM #5
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
- 01-31-2013, 09:44 AM #6
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
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.
- 01-31-2013, 09:45 AM #7
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
Re: New to Java and Java Forums - need help.
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); } } }
- 01-31-2013, 10:38 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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 **
- 01-31-2013, 11:35 AM #9
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
- 01-31-2013, 01:17 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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());
Please do not ask for code as refusal often offends.
** This space for rent **
- 02-01-2013, 11:08 AM #11
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
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(); }
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)
- 02-01-2013, 11:52 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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 **
- 02-01-2013, 12:34 PM #13
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
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 rowas 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(); } }
- 02-01-2013, 12:52 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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 **
- 02-01-2013, 01:15 PM #15
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
- 02-01-2013, 01:19 PM #16
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
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(); } }
- 02-01-2013, 01:49 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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 + "-");
Please do not ask for code as refusal often offends.
** This space for rent **
- 02-01-2013, 04:28 PM #18
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
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?
- 02-01-2013, 04:41 PM #19
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
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.
- 02-01-2013, 04:42 PM #20
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
Similar Threads
-
Hi, Java Forums
By SolidCobra in forum IntroductionsReplies: 1Last Post: 10-06-2008, 02:37 AM -
hello Java World http://www.java-forums.org/images/smilies/smile.gif
By aniljoo in forum IntroductionsReplies: 7Last Post: 07-22-2008, 06:54 PM -
new to java new to forums
By mctruck in forum IntroductionsReplies: 5Last Post: 02-16-2008, 04:35 AM
Bookmarks