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

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

    Hi there,

    I couldnt figure how stringBuilder works, however I tried something new to the code like below. It does read all the file but each line in each dialog box.
    How can I fit all lines in one dialog box?

    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.hasNextLine()){
    			String a = s.nextLine();
    			
    			JOptionPane.showMessageDialog(null,"Following cars are currently available:-\n"
    										+ a, "Car Database",JOptionPane.INFORMATION_MESSAGE);
    		}
    	}
    	public void closeFile(){
    		s.close();
    	}
    }

  2. #22
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

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

    What do you not understand about StringBuilder?
    Please do not ask for code as refusal often offends.

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

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

    Hi, im not familiar with StringBuilder...anyways I have managed to get what output I wish by changing the code as below

    Java Code:
    public void readFile() {
    		
    		String a = "";
    		
    		while (s.hasNextLine()) {
    			
    			a += s.nextLine() + "\n";
    		}
    		
    		JOptionPane.showMessageDialog(null,
    				"Following cars are currently available:-\n" + a,
    				"Car Database", JOptionPane.INFORMATION_MESSAGE);
    	}
    
    	public void closeFile() {
    		s.close();
    	}

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

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

    I got another issue.....

    I can read my .csv file perfectly but if I want to make a dropdown menu out of the file....all 5 lines are shown in one single line.

    How can use .split to split the lines at every third comma "," ???

    Java Code:
    import java.io.File;
    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    
    public class deleteline {
    
    	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() {
    		
    		String a = "";
    		
    		while (s.hasNextLine()) {
    			a += s.nextLine() + "\n";
    		}
    		
    		String menu [] = a.split(" ,");
    		Object[] selectionValues = menu;
    
    		String initialSelection = "Please select";
    		
    		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 + ".", "Cars Database Menu",
    				JOptionPane.INFORMATION_MESSAGE);
    		
    		JOptionPane.showMessageDialog(null,
    				"Following cars are currently available:-\n" + a,
    				"Car Database", JOptionPane.INFORMATION_MESSAGE);
    	}
    
    	public void closeFile() {
    		s.close();
    	}
    }

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

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

    Hi Tol

    I have worked around and got it right.

    As per the program, I need to delete the specific row. I have String a where the file is as the database. I have to remove the selected row from String a and write it back to the csv file.

    I cant figure how to remove this specific row. Below is so far I have reached and stuck for now.

    Java Code:
    public class deleteline {
    
    	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() throws IOException {
    		
    		String a = "";
    		
    		while (s.hasNextLine()) {
    			a += s.nextLine() + "\n";
    		}
    		
    		String menu [] = a.split("\n");
    		Object[] selectionValues = menu;
    		String initialSelection = "Please select";
    		
    		Object selection = JOptionPane.showInputDialog(null,
    				"Please select option.", "Cars Database Menu",
    				JOptionPane.QUESTION_MESSAGE, null, selectionValues,
    				initialSelection);
    
    		JOptionPane.showMessageDialog(null, "You have choosen to delete "
    				+ selection + ".", "Cars Database Menu",
    				JOptionPane.INFORMATION_MESSAGE);
    		
    		if(selection.equals(selection)){
    			String remove = selection.toString();
    			a.matches(remove);
    			//how do I delete this row which matches?
    		}
    	}
    	
    	public void closeFile() {
    		s.close();
    	}
    }
    New to Java and Java Forums - need help.-screen-shot-2013-02-05-3.13.13-pm.png
    New to Java and Java Forums - need help.-screen-shot-2013-02-05-3.13.30-pm.png

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

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

    You have to rewrite the file.
    One method is to write out the csv data to a temp file, skipping the line to delete, then remove the original file, finally rename the temp file.
    Please do not ask for code as refusal often offends.

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

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

    Hi

    I do understand the process in theory. I dont know how to write the same file with skipping the row to delete.


  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

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

    You know the row, surely?
    At least you can find it because you have the text for it.
    Please do not ask for code as refusal often offends.

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

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

    Thank you Guys, I have almost completed my program. Learned a lot during this. Thanks once again.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 0
    Last Post: 04-21-2012, 06:39 AM
  2. Hi, Java Forums
    By SolidCobra in forum Introductions
    Replies: 1
    Last Post: 10-06-2008, 01:37 AM
  3. Replies: 7
    Last Post: 07-22-2008, 05:54 PM
  4. new to java new to forums
    By mctruck in forum Introductions
    Replies: 5
    Last Post: 02-16-2008, 03: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
  •