Page 1 of 2 12 LastLast
Results 1 to 20 of 34
Like Tree3Likes

Thread: Help me please

  1. #1
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Question Help me please

    Hi, Im working on an assignment which is about searching for some data in a file and I got stuck in the process. Please someone should help urgently.

    Thanks in advance

    Java Code:
    package whizzCar;
    
    
    
    import javax.swing.JFrame;
    
    public class Index{
    	
    	private static void createAndShowGUI() {
    	    
    
    		 JFrame.setDefaultLookAndFeelDecorated(true);
    	  
    		 JFrame frame = new JFrame("Whizz Car Sales Ltd. ");
    		 
    		GUI demo = new GUI();
    		//Create and set up the content pane.
    	    frame.setContentPane(demo.createContentPanel());
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    frame.setSize(1100, 700);
    	    
    	    
    	    frame.setVisible(true);
    	}
    	
    public static void main(String args[]) {
    	//creating and showing this application's GUI.
    createAndShowGUI();
    
            }
    }
    Java Code:
    package whizzCar;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Scanner;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class GUI{
    	int x = 0;
    	Car car[] = new Car[1];
    	
    	DefaultListModel searchdlm;
    	
    	JScrollPane pane;
    	JList list;
    	JTextArea searchArea;
    
    	//stream to read file
    	File file;
    	FileInputStream fin;
    	
    	//Declaring all variables 
    	Font titleFont, searchFont;
    	
    	JButton searchButton;
    	
    	JComboBox typeOfCar, make, engineSize,
    			  fuel, age, colour, customerTitle ;
    	
    	JLabel titleLabel, typeOfCarLabel, makeLabel,
    	   	   EngineSizeLabel, fuelLabel, ageLabel,
    	   	   colourLabel, searchLabel, customerDetailLabel,
    	   	   customerNameLabel, emp, customerTitleLabel;
    	
    	JPanel mainPanel, titlePanel, searchPanel,
    		   bodyPanel, specPanel, customerPanel ;
    	
    	JTextField customerNameField;
    	
    	//USE CHARAT
    	
    	public JPanel createContentPanel (){
    		
    		//Creating the title fonts
    		titleFont= new Font("Cambria (Headings)",Font.ITALIC,60);
    		searchFont= new Font("Adobe Garamond Pro Bold",Font.BOLD,20);
    		
    		mainPanel = new JPanel(new BorderLayout());
    		titlePanel = new JPanel();
    		searchPanel = new JPanel(null);
    		bodyPanel = new JPanel(new GridLayout(1, 1));
    		specPanel = new JPanel(new GridLayout(8, 2));
    		customerPanel = new JPanel(null);
    		
    		// adding color to some panels
    		titlePanel.setBackground(Color.blue);
    		searchPanel.setBackground(Color.red);
    		bodyPanel.setBackground(Color.white);
    				
    		// arranging the panels
    		mainPanel.add(titlePanel, BorderLayout.NORTH);
    		mainPanel.add(searchPanel, BorderLayout.CENTER);
    				
    		searchPanel.add(specPanel);
    		specPanel.setSize(250, 270);
    		specPanel.setLocation(5, 225);
    		
    		searchPanel.add(customerPanel);
    		customerPanel.setSize(250, 200);
    		customerPanel.setLocation(5, 20);
    		
    		searchPanel.add(bodyPanel);
    		bodyPanel.setSize(800, 475);
    		bodyPanel.setLocation(280, 20);
    		
    		searchdlm = new DefaultListModel();  
    		list = new JList(searchdlm);
    		pane = new JScrollPane(list);
    		pane.setBackground(Color.green);
    		
    		bodyPanel.add(pane);
    		
    		
    		//adding the title
    		titleLabel = new JLabel ("Whizz Car Sales Ltd.");
    		titleLabel.setFont(titleFont);
    		titlePanel.add(titleLabel);
    		
    		typeOfCarLabel = new JLabel (" Type of Car"); 
    		makeLabel = new JLabel (" Make");
    		EngineSizeLabel = new JLabel (" Engine Size"); 
    		fuelLabel = new JLabel (" Fuel"); 
    		ageLabel = new JLabel (" Age");
    		colourLabel = new JLabel (" Colour"); 
    		
    		emp = new JLabel ("");
    		searchLabel = new JLabel ("Search");
    		searchLabel.setFont(searchFont);
    		
    		
    		//string of customer title
    		String[] customertitle = {"Mr", "Mrs"};
    		
    		//adding strings of all possible values of the search parameters
    		String[] typesOfCar = {"Any", "Saloon", "Estate", "SUV", "Sports" };
    		String[] carMake = {"Any", "Ford", "Toyota", "BMW", "Honda" };
    		String[] carEngineType = {"Any", "1.2 litre", "1.4 litre", "1.6 litre", "1.8 litre", "2.0 litre", "> 2.0 litre" };
    		String[] carFuel = {"Any", "Unleaded", "Diesel"};
    		String[] carAge = {"Any", "0-12 months", "13-24 months", "25-36 months", "> 36 months" };
    		String[] carColour = {"Any", "Silver", "White", "Black", "Red", "Blue", "Green" };
    		
    		//adding the string of values to combo boxes
    		customerTitle = new JComboBox(customertitle); 
    		typeOfCar = new JComboBox(typesOfCar);
    		make = new JComboBox(carMake); 
    		engineSize = new JComboBox(carEngineType); 
    		fuel = new JComboBox(carFuel); 
    		age = new JComboBox(carAge); 
    		colour = new JComboBox(carColour);
    		searchButton = new JButton("Search");
    		
    		//adding labels and combo boxes to a panel
    		specPanel.add(searchLabel);
    		specPanel.add(emp);
    		
    		specPanel.add(typeOfCarLabel);
    		specPanel.add(typeOfCar);
    		
    		specPanel.add(makeLabel);
    		specPanel.add(make);
    		
    		specPanel.add(EngineSizeLabel);
    		specPanel.add(engineSize);
    		
    		specPanel.add(fuelLabel);
    		specPanel.add(fuel);
    		
    		specPanel.add(ageLabel);
    		specPanel.add(age);
    		
    		specPanel.add(colourLabel);
    		specPanel.add(colour);
    		
    		specPanel.add(searchButton);
    		
    		//creating parameters for customer details
    		customerDetailLabel = new JLabel("Customer Details");
    		customerNameLabel = new JLabel("Name: ");
    		customerTitleLabel = new JLabel("Title: ");
    		customerNameField = new JTextField();
    		customerTitle = new JComboBox(customertitle);
    		
    		customerDetailLabel.setFont(searchFont);
    		
    		//Arraigning the customer's details
    		customerPanel.add(customerDetailLabel);
    		customerDetailLabel.setSize(180, 20);
    		customerDetailLabel.setLocation(5, 10);
    		
    		customerPanel.add(customerTitleLabel);
    		customerTitleLabel.setSize(60, 20);
    		customerTitleLabel.setLocation(5, 50);
    		
    		customerPanel.add(customerTitle);
    		customerTitle.setSize(80, 25);
    		customerTitle.setLocation(50, 45);
    		
    		customerPanel.add(customerNameLabel);
    		customerNameLabel.setSize(60, 20);
    		customerNameLabel.setLocation(5, 80);
    		
    		customerPanel.add(customerNameField);
    		customerNameField.setSize(150, 25);
    		customerNameField.setLocation(50, 75);
    		
    		actions action = new actions();
    		customerTitle.addActionListener(action);
    		typeOfCar.addActionListener(action);
    		make.addActionListener(action);
    		engineSize.addActionListener(action);
    		fuel.addActionListener(action);
    		age.addActionListener(action);
    		colour.addActionListener(action);
    		searchButton.addActionListener(action);
    		
            mainPanel.setOpaque(true);
    		return mainPanel;
            
    	}
    
    	private class actions implements ActionListener{
    
    		private String a;
    
    		@SuppressWarnings("deprecation")
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    			if (e.getSource() == searchButton) {
    				
    				if(customerNameField.getText().trim().compareTo("") == 0) //trim is to remove the spaces 
    	            {														//in front or behind texts in the text field	
    					//customerNameField.setBackground(Color.red);
    					JOptionPane.showMessageDialog(null, "Please enter a name.");
    	            }
    	            else
    	            {
    	            	searchdlm.clear();
    	            	try
    	        		{
    	            		file = new File("/Users/bello_dbello/Dropbox/JavaEE/WhizzCar/src/whizzCar/carList.txt");
    	        			//open an input stream
    	        			fin = new FileInputStream(file);
    	        			
    	        			Scanner scan = new Scanner(fin);
    	        			scan.useDelimiter(";");
    	        			
    	        			String strType = typeOfCar.getSelectedItem().toString();
    	        			String strMake = make.getSelectedItem().toString();
    	        			String strEngine = engineSize.getSelectedItem().toString();
    	        			String strFuel = fuel.getSelectedItem().toString();
    	        			String strAge = age.getSelectedItem().toString();
    	        			String strColor = colour.getSelectedItem().toString();
    	        			
    	        			char quote = '"';
    	        			while (scan.hasNext()){
    	        				String search = scan.next();//.replace(quote, ' ');
    	        				if (search.contains(strType) || search.contains(strMake) || search.contains(strEngine) || search.contains(strFuel)
    	        				   || search.contains(strAge) || search.contains(strColor)) 
    	        						{
    
    	        					
    	    	        			
    	    	        			
    	        					for(int i = 0; i< car.length; i ++){
    	        						
    	    	        			String ddd = search;
    
    	    	        			car[i] = new Car(ddd);
    	    	        			
    	    	        			searchdlm.addElement(car[i]);
    	    	        			
    	    	        			System.out.println(ddd);
    	        					}
    	        					
    	        					//searchdlm.addElement(search);
    	        				}
    	        				else
    	        					searchdlm.removeElement(search);
    	        			}
    	        				
    	        			
    	        			
    	        			/* This method reads a byte from stream. This method returns next byte of data
    	        		     * from file or -1 if the end of the file is reached.
    	        		     */
    	        			
    	        			/*
    	        			int ch;
    	        			StringBuffer strContent = new StringBuffer("");
    	        			
    	        			while( (ch = fin.read()) != -1)
    	        		        strContent.append((char)ch);
    	        			System.out.println(strContent);
    	        			
    	        			searchdlm.addElement(strContent);
    	        			a = ";";
    	        			//if (((CharSequence) fin).charAt(4) == 0)
    	        			//	searchdlm.addElement(strContent);
    	        			
    	        			/*
    	        			for(int i = 0; i< file.length(); i ++)
    	        				for(x = 0; x< car.length; x ++) {
    	        					
    	        				//	String y = (String) (new DataInputStream(fin).readLine());
    	        				
    	        			//car[x] = new Car(y, i, null);
    	        			
    	        			//searchdlm.addElement(car[x]);
    	        					
    	        					searchdlm.addElement(new DataInputStream(fin).readLine());
    							} */
    	        			
    	        			
    	        			fin.close();
    	        		}
    	        		catch (IOException ex)
    	        		{
    	        		System.err.println("Unable to read file!");
    	        		System.exit(-1);
    	        		}
    	            }
    			}
    		}
    	}
    
    }
    Java Code:
    package whizzCar;
    
    public class CarSpec {
    
    //Declaring variables	
    private String type, make, model, fuel, colour;
    private float engineSize;
    private int ageInMonths;
    
    //creating constructor for the variables 
    public CarSpec(String type, String make, String model,float engineSize,
    		String fuel, int ageInMonths, String colour) {
    	super();
    	this.type = type;
    	this.make = make;
    	this.model = model;
    	this.fuel = fuel;
    	this.colour = colour;
    	this.engineSize = engineSize;
    	this.ageInMonths = ageInMonths;
    }
    
    
    @Override //getting the values of all the variables
    public String toString() {
    	return " Type: " + type + ". Make: " + make + ". Model: " + model
    			+ ". Fuel: " + fuel + ". Colour: " + colour + ". EngineSize: "
    			+ engineSize + ". Age (Months): " + ageInMonths ;
    }
    
    }
    Java Code:
    package whizzCar;
    
    public class Car {
    	
    //Declaring variables	
    private String regNumber;
    private double price;
    private CarSpec spec;
    
    
    //creating constructor 
    public Car(String regNumber, double price, CarSpec spec) {
    	super();
    	this.regNumber = regNumber;
    	this.price = price;
    	this.spec = spec;
    }
    
    
    @Override //getting the values of all the variables
    public String toString() {
    	String ddd ="Car Reg No.: " + regNumber + ", Price: " + price +". "+ spec ;
    	return ddd;
    	}
    
    public Car(String dd) {
    	dd = toString();
    	
    	}
    
    }
    This is the carList that data should be read from, Changes are not allowed to be make to it. The file is .txt format. Thanks again

    ("MA06 XVU", 8999.0, new CarSpec("Saloon","Ford","Mondeo", (float)1.6,"Diesel", (int)12,"Blue")); ("MA06
    UXW", 12900.0, new CarSpec("Estate","Ford","Mondeo", (float)1.8,"Petrol",
    (int)6,"Red")); ("MA04 EST", 15999.0, new CarSpec("SUV","Honda","CR-V Sport",
    (float)2.0,"Diesel", (int)17,"Silver")); ("VZX 9512", 15499.0, new
    CarSpec("Sports","Toyota","Celica Coupe", (float)1.8,"Petrol",
    (int)18,"Black")); ("DV06 UHQ", 25499.0, new CarSpec("Saloon","BMW","Series 3",
    (float)2.2,"Diesel", (int)14,"Black")); ("DV55 WVX", 21499.0, new
    CarSpec("Sports","BMW","Z4 Coupe", (float)3.0,"Petrol", (int)8,"Silver"));
    ("BN06 DHQ", 65499.0, new CarSpec("Saloon","BMW","Series 6 M5 5.0",
    (float)5.0,"Petrol", (int)5,"Black")); ("LA54 VUN", 4499.0, new
    CarSpec("Saloon","Honda","Jazz", (float)1.2,"Diesel", (int)25,"Green")); ("DV06
    MHQ", 65499.0, new CarSpec("Saloon","BMW","Series 6 M5 5.0",
    (float)5.0,"Petrol", (int)5,"Black")); ("MW04 CXW", 6900.0, new
    CarSpec("Estate","Ford","Focus C-Max", (float)1.6,"Petrol", (int)24,"Blue"));
    ("VZ55 LRT", 5499.0, new CarSpec("Saloon","Toyota","Aygo", (float)1.0,"Petrol",
    (int)8,"White")); ("GH03 RPK", 4000.0, new CarSpec("Saloon","Toyota","Aygo",
    (float)1.1,"Diesel", (int)28,"Green")); ("VH03 ZUK", 17500.0, new
    CarSpec("SUV","Toyota","Landcruiser Amazon", (float)4.7,"Diesel",
    (int)38,"Black")); ("WT05 KXW", 3900.0, new CarSpec("Estate","Ford","Fiesta",
    (float)1.4,"Petrol", (int)39,"Red")); ("SA06 JTX", 17499.0, new
    CarSpec("SUV","BMW","X3 Sport", (float)2.5,"Petrol", (int)26,"Blue"));

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    Quote Originally Posted by DK Bello View Post
    Hi, Im working on an assignment which is about searching for some data in a file and I got stuck in the process. Please someone should help urgently.

    Thanks in advance

    Java Code:
    package whizzCar;
    
    
    
    import javax.swing.JFrame;
    
    public class Index{
    	
    	private static void createAndShowGUI() {
    	    
    
    		 JFrame.setDefaultLookAndFeelDecorated(true);
    	  
    		 JFrame frame = new JFrame("Whizz Car Sales Ltd. ");
    		 
    		GUI demo = new GUI();
    		//Create and set up the content pane.
    	    frame.setContentPane(demo.createContentPanel());
    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    frame.setSize(1100, 700);
    	    
    	    
    	    frame.setVisible(true);
    	}
    	
    public static void main(String args[]) {
    	//creating and showing this application's GUI.
    createAndShowGUI();
    
            }
    }
    Java Code:
    package whizzCar;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Scanner;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class GUI{
    	int x = 0;
    	Car car[] = new Car[1];
    	
    	DefaultListModel searchdlm;
    	
    	JScrollPane pane;
    	JList list;
    	JTextArea searchArea;
    
    	//stream to read file
    	File file;
    	FileInputStream fin;
    	
    	//Declaring all variables 
    	Font titleFont, searchFont;
    	
    	JButton searchButton;
    	
    	JComboBox typeOfCar, make, engineSize,
    			  fuel, age, colour, customerTitle ;
    	
    	JLabel titleLabel, typeOfCarLabel, makeLabel,
    	   	   EngineSizeLabel, fuelLabel, ageLabel,
    	   	   colourLabel, searchLabel, customerDetailLabel,
    	   	   customerNameLabel, emp, customerTitleLabel;
    	
    	JPanel mainPanel, titlePanel, searchPanel,
    		   bodyPanel, specPanel, customerPanel ;
    	
    	JTextField customerNameField;
    	
    	//USE CHARAT
    	
    	public JPanel createContentPanel (){
    		
    		//Creating the title fonts
    		titleFont= new Font("Cambria (Headings)",Font.ITALIC,60);
    		searchFont= new Font("Adobe Garamond Pro Bold",Font.BOLD,20);
    		
    		mainPanel = new JPanel(new BorderLayout());
    		titlePanel = new JPanel();
    		searchPanel = new JPanel(null);
    		bodyPanel = new JPanel(new GridLayout(1, 1));
    		specPanel = new JPanel(new GridLayout(8, 2));
    		customerPanel = new JPanel(null);
    		
    		// adding color to some panels
    		titlePanel.setBackground(Color.blue);
    		searchPanel.setBackground(Color.red);
    		bodyPanel.setBackground(Color.white);
    				
    		// arranging the panels
    		mainPanel.add(titlePanel, BorderLayout.NORTH);
    		mainPanel.add(searchPanel, BorderLayout.CENTER);
    				
    		searchPanel.add(specPanel);
    		specPanel.setSize(250, 270);
    		specPanel.setLocation(5, 225);
    		
    		searchPanel.add(customerPanel);
    		customerPanel.setSize(250, 200);
    		customerPanel.setLocation(5, 20);
    		
    		searchPanel.add(bodyPanel);
    		bodyPanel.setSize(800, 475);
    		bodyPanel.setLocation(280, 20);
    		
    		searchdlm = new DefaultListModel();  
    		list = new JList(searchdlm);
    		pane = new JScrollPane(list);
    		pane.setBackground(Color.green);
    		
    		bodyPanel.add(pane);
    		
    		
    		//adding the title
    		titleLabel = new JLabel ("Whizz Car Sales Ltd.");
    		titleLabel.setFont(titleFont);
    		titlePanel.add(titleLabel);
    		
    		typeOfCarLabel = new JLabel (" Type of Car"); 
    		makeLabel = new JLabel (" Make");
    		EngineSizeLabel = new JLabel (" Engine Size"); 
    		fuelLabel = new JLabel (" Fuel"); 
    		ageLabel = new JLabel (" Age");
    		colourLabel = new JLabel (" Colour"); 
    		
    		emp = new JLabel ("");
    		searchLabel = new JLabel ("Search");
    		searchLabel.setFont(searchFont);
    		
    		
    		//string of customer title
    		String[] customertitle = {"Mr", "Mrs"};
    		
    		//adding strings of all possible values of the search parameters
    		String[] typesOfCar = {"Any", "Saloon", "Estate", "SUV", "Sports" };
    		String[] carMake = {"Any", "Ford", "Toyota", "BMW", "Honda" };
    		String[] carEngineType = {"Any", "1.2 litre", "1.4 litre", "1.6 litre", "1.8 litre", "2.0 litre", "> 2.0 litre" };
    		String[] carFuel = {"Any", "Unleaded", "Diesel"};
    		String[] carAge = {"Any", "0-12 months", "13-24 months", "25-36 months", "> 36 months" };
    		String[] carColour = {"Any", "Silver", "White", "Black", "Red", "Blue", "Green" };
    		
    		//adding the string of values to combo boxes
    		customerTitle = new JComboBox(customertitle); 
    		typeOfCar = new JComboBox(typesOfCar);
    		make = new JComboBox(carMake); 
    		engineSize = new JComboBox(carEngineType); 
    		fuel = new JComboBox(carFuel); 
    		age = new JComboBox(carAge); 
    		colour = new JComboBox(carColour);
    		searchButton = new JButton("Search");
    		
    		//adding labels and combo boxes to a panel
    		specPanel.add(searchLabel);
    		specPanel.add(emp);
    		
    		specPanel.add(typeOfCarLabel);
    		specPanel.add(typeOfCar);
    		
    		specPanel.add(makeLabel);
    		specPanel.add(make);
    		
    		specPanel.add(EngineSizeLabel);
    		specPanel.add(engineSize);
    		
    		specPanel.add(fuelLabel);
    		specPanel.add(fuel);
    		
    		specPanel.add(ageLabel);
    		specPanel.add(age);
    		
    		specPanel.add(colourLabel);
    		specPanel.add(colour);
    		
    		specPanel.add(searchButton);
    		
    		//creating parameters for customer details
    		customerDetailLabel = new JLabel("Customer Details");
    		customerNameLabel = new JLabel("Name: ");
    		customerTitleLabel = new JLabel("Title: ");
    		customerNameField = new JTextField();
    		customerTitle = new JComboBox(customertitle);
    		
    		customerDetailLabel.setFont(searchFont);
    		
    		//Arraigning the customer's details
    		customerPanel.add(customerDetailLabel);
    		customerDetailLabel.setSize(180, 20);
    		customerDetailLabel.setLocation(5, 10);
    		
    		customerPanel.add(customerTitleLabel);
    		customerTitleLabel.setSize(60, 20);
    		customerTitleLabel.setLocation(5, 50);
    		
    		customerPanel.add(customerTitle);
    		customerTitle.setSize(80, 25);
    		customerTitle.setLocation(50, 45);
    		
    		customerPanel.add(customerNameLabel);
    		customerNameLabel.setSize(60, 20);
    		customerNameLabel.setLocation(5, 80);
    		
    		customerPanel.add(customerNameField);
    		customerNameField.setSize(150, 25);
    		customerNameField.setLocation(50, 75);
    		
    		actions action = new actions();
    		customerTitle.addActionListener(action);
    		typeOfCar.addActionListener(action);
    		make.addActionListener(action);
    		engineSize.addActionListener(action);
    		fuel.addActionListener(action);
    		age.addActionListener(action);
    		colour.addActionListener(action);
    		searchButton.addActionListener(action);
    		
            mainPanel.setOpaque(true);
    		return mainPanel;
            
    	}
    
    	private class actions implements ActionListener{
    
    		private String a;
    
    		@SuppressWarnings("deprecation")
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			
    			if (e.getSource() == searchButton) {
    				
    				if(customerNameField.getText().trim().compareTo("") == 0) //trim is to remove the spaces 
    	            {														//in front or behind texts in the text field	
    					//customerNameField.setBackground(Color.red);
    					JOptionPane.showMessageDialog(null, "Please enter a name.");
    	            }
    	            else
    	            {
    	            	searchdlm.clear();
    	            	try
    	        		{
    	            		file = new File("/Users/bello_dbello/Dropbox/JavaEE/WhizzCar/src/whizzCar/carList.txt");
    	        			//open an input stream
    	        			fin = new FileInputStream(file);
    	        			
    	        			Scanner scan = new Scanner(fin);
    	        			scan.useDelimiter(";");
    	        			
    	        			String strType = typeOfCar.getSelectedItem().toString();
    	        			String strMake = make.getSelectedItem().toString();
    	        			String strEngine = engineSize.getSelectedItem().toString();
    	        			String strFuel = fuel.getSelectedItem().toString();
    	        			String strAge = age.getSelectedItem().toString();
    	        			String strColor = colour.getSelectedItem().toString();
    	        			
    	        			char quote = '"';
    	        			while (scan.hasNext()){
    	        				String search = scan.next();//.replace(quote, ' ');
    	        				if (search.contains(strType) || search.contains(strMake) || search.contains(strEngine) || search.contains(strFuel)
    	        				   || search.contains(strAge) || search.contains(strColor)) 
    	        						{
    
    	        					
    	    	        			
    	    	        			
    	        					for(int i = 0; i< car.length; i ++){
    	        						
    	    	        			String ddd = search;
    
    	    	        			car[i] = new Car(ddd);
    	    	        			
    	    	        			searchdlm.addElement(car[i]);
    	    	        			
    	    	        			System.out.println(ddd);
    	        					}
    	        					
    	        					//searchdlm.addElement(search);
    	        				}
    	        				else
    	        					searchdlm.removeElement(search);
    	        			}
    	        				
    	        			
    	        			
    	        			/* This method reads a byte from stream. This method returns next byte of data
    	        		     * from file or -1 if the end of the file is reached.
    	        		     */
    	        			
    	        			/*
    	        			int ch;
    	        			StringBuffer strContent = new StringBuffer("");
    	        			
    	        			while( (ch = fin.read()) != -1)
    	        		        strContent.append((char)ch);
    	        			System.out.println(strContent);
    	        			
    	        			searchdlm.addElement(strContent);
    	        			a = ";";
    	        			//if (((CharSequence) fin).charAt(4) == 0)
    	        			//	searchdlm.addElement(strContent);
    	        			
    	        			/*
    	        			for(int i = 0; i< file.length(); i ++)
    	        				for(x = 0; x< car.length; x ++) {
    	        					
    	        				//	String y = (String) (new DataInputStream(fin).readLine());
    	        				
    	        			//car[x] = new Car(y, i, null);
    	        			
    	        			//searchdlm.addElement(car[x]);
    	        					
    	        					searchdlm.addElement(new DataInputStream(fin).readLine());
    							} */
    	        			
    	        			
    	        			fin.close();
    	        		}
    	        		catch (IOException ex)
    	        		{
    	        		System.err.println("Unable to read file!");
    	        		System.exit(-1);
    	        		}
    	            }
    			}
    		}
    	}
    
    }
    Java Code:
    package whizzCar;
    
    public class CarSpec {
    
    //Declaring variables	
    private String type, make, model, fuel, colour;
    private float engineSize;
    private int ageInMonths;
    
    //creating constructor for the variables 
    public CarSpec(String type, String make, String model,float engineSize,
    		String fuel, int ageInMonths, String colour) {
    	super();
    	this.type = type;
    	this.make = make;
    	this.model = model;
    	this.fuel = fuel;
    	this.colour = colour;
    	this.engineSize = engineSize;
    	this.ageInMonths = ageInMonths;
    }
    
    
    @Override //getting the values of all the variables
    public String toString() {
    	return " Type: " + type + ". Make: " + make + ". Model: " + model
    			+ ". Fuel: " + fuel + ". Colour: " + colour + ". EngineSize: "
    			+ engineSize + ". Age (Months): " + ageInMonths ;
    }
    
    }
    Java Code:
    package whizzCar;
    
    public class Car {
    	
    //Declaring variables	
    private String regNumber;
    private double price;
    private CarSpec spec;
    
    
    //creating constructor 
    public Car(String regNumber, double price, CarSpec spec) {
    	super();
    	this.regNumber = regNumber;
    	this.price = price;
    	this.spec = spec;
    }
    
    
    @Override //getting the values of all the variables
    public String toString() {
    	String ddd ="Car Reg No.: " + regNumber + ", Price: " + price +". "+ spec ;
    	return ddd;
    	}
    
    public Car(String dd) {
    	dd = toString();
    	
    	}
    
    }
    This is the carList that data should be read from, Changes are not allowed to be make to it. The file is .txt format. Thanks again

    ("MA06 XVU", 8999.0, new CarSpec("Saloon","Ford","Mondeo", (float)1.6,"Diesel", (int)12,"Blue")); ("MA06
    UXW", 12900.0, new CarSpec("Estate","Ford","Mondeo", (float)1.8,"Petrol",
    (int)6,"Red")); ("MA04 EST", 15999.0, new CarSpec("SUV","Honda","CR-V Sport",
    (float)2.0,"Diesel", (int)17,"Silver")); ("VZX 9512", 15499.0, new
    CarSpec("Sports","Toyota","Celica Coupe", (float)1.8,"Petrol",
    (int)18,"Black")); ("DV06 UHQ", 25499.0, new CarSpec("Saloon","BMW","Series 3",
    (float)2.2,"Diesel", (int)14,"Black")); ("DV55 WVX", 21499.0, new
    CarSpec("Sports","BMW","Z4 Coupe", (float)3.0,"Petrol", (int)8,"Silver"));
    ("BN06 DHQ", 65499.0, new CarSpec("Saloon","BMW","Series 6 M5 5.0",
    (float)5.0,"Petrol", (int)5,"Black")); ("LA54 VUN", 4499.0, new
    CarSpec("Saloon","Honda","Jazz", (float)1.2,"Diesel", (int)25,"Green")); ("DV06
    MHQ", 65499.0, new CarSpec("Saloon","BMW","Series 6 M5 5.0",
    (float)5.0,"Petrol", (int)5,"Black")); ("MW04 CXW", 6900.0, new
    CarSpec("Estate","Ford","Focus C-Max", (float)1.6,"Petrol", (int)24,"Blue"));
    ("VZ55 LRT", 5499.0, new CarSpec("Saloon","Toyota","Aygo", (float)1.0,"Petrol",
    (int)8,"White")); ("GH03 RPK", 4000.0, new CarSpec("Saloon","Toyota","Aygo",
    (float)1.1,"Diesel", (int)28,"Green")); ("VH03 ZUK", 17500.0, new
    CarSpec("SUV","Toyota","Landcruiser Amazon", (float)4.7,"Diesel",
    (int)38,"Black")); ("WT05 KXW", 3900.0, new CarSpec("Estate","Ford","Fiesta",
    (float)1.4,"Petrol", (int)39,"Red")); ("SA06 JTX", 17499.0, new
    CarSpec("SUV","BMW","X3 Sport", (float)2.5,"Petrol", (int)26,"Blue"));
    • "Help me please" is not a good title for a question here. Your title should be thought of as like a newspaper headline -- a short summary of your actual problem. "help me please" tells us nothing, other than the obvious -- you need help. But we knew that already. Why else would you ask a question here?
    • You are posting gobs and gobs of code, but don't ever tell us what exactly you need help with, but instead state "I got stuck in the process". Stuck where? How? Are you getting error messages? I doubt you'll get a decent answer until you do. Please put as much effort into asking your question as you'd like some to put in answering it.

  3. #3
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    I'm having Problem getting the string of from the file and putting it into an array list. In the try method is a scanner which reads the file and try to put it in the array, it is displaying the search but its returning the values as null.

    Thanks for the correction.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    Quote Originally Posted by DK Bello View Post
    I'm having Problem getting the string of from the file and putting it into an array list. In the try method is a scanner which reads the file and try to put it in the array, it is displaying the search but its returning the values as null.
    To save us time, please tell us, where is this offending code?

  5. #5
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    Starting from the while statement in the GUI class

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,339
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help me please

    A large amount of code doesn't make the question 'advanced'; I moved this thread.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    yes it is, it reads whether the fine still have a next line, then it prints the line. it stops printing when the all the lines in the file have been read.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    What debugging println statement?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,339
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help me please

    Quote Originally Posted by DK Bello View Post
    What debugging println statement?
    System.out.println( ... ) or System.err.println( ... )

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    .err.println(...)
    its almost the same thin coz it olso prints the file not found exception on the console

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    So, first thing you need to do is change this
    Java Code:
    catch (IOException ex)
    {
       System.err.println("Unable to read file!");
       System.exit(-1);
    }
    to this:

    Java Code:
    catch (IOException ex)
    {
       ex.printStackTrace();
       System.exit(-1);
    }
    to allow us to be absolutely sure that its the file reading that is your problem. Please come back and post here your stack trace that this produces. Also please indicate which lines are causing the exceptions to be thrown (the stack trace will tell you the line numbers -- or if you're confused we'll help you find the line numbers).

  13. #13
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    there is no error printed, because String ddd does takes the value of each line of string that is printed by search, but it does not place it in the array (car[]). that is the reason why the array is returning the values as null.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    OK, so I'm confused. Previously you posted that you had a file not found error, but now you're saying that your file *is* being found and being read in, is that correct? So where is your problem now?

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    By the way, what are you trying to do with this Car constructor?
    Java Code:
    public Car(String dd) {
        dd = toString();
         
    }
    Whatever you're trying it's not working because this will not create a valid Car object in any shape or form.

  16. #16
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    it is of no use use now, I wanted to return the toString using the dd, but I then found a way by using the ddd.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  18. #18
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    No.
    My problem is this array
    car[i] = new Car(ddd);
    Should return something like this
    car[i] = new Car("MA06 XVU", 8999.0, new CarSpec("Saloon","Ford","Mondeo", (float)1.6,"Diesel", (int)12,"Blue"));
    It does return it because you can see that by printing ddd on the console. But it stile returns the value of the regnumb, price and spec as null, and its not suppose to be null, it is suppose to be the values inside the string.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me please

    Quote Originally Posted by DK Bello View Post
    No.
    My problem is this array
    car[i] = new Car(ddd);
    Should return something like this
    car[i] = new Car("MA06 XVU", 8999.0, new CarSpec("Saloon","Ford","Mondeo", (float)1.6,"Diesel", (int)12,"Blue"));
    No it won't. I just told you that your Car constructor that takes a single String is borked.

    You're going to have to take the ddd String, split it apart and create your Car object from it. Print out the contents of that String to see what it holds.
    And again, delete your Car constructor that takes a single String as it's useless.

  20. #20
    DK Bello is offline Member
    Join Date
    Dec 2011
    Location
    UAE
    Posts
    29
    Rep Power
    0

    Default Re: Help me please

    I do not understand what you mean,
    if I make the return method of the constructor to be
    public String toString() {
    return "Car Reg No.: " + regNumber + ", Price: " + price +". "+ spec ;
    }
    then the car array cannot take the value because it have to be in this format car[i] = new Car (..., ..., ...)
    which means I have to put in 3 sets of strings in the array, and I find it impossible to divide to strings form the fine into set of threes for a single array

Page 1 of 2 12 LastLast

Posting Permissions

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