Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Working on reading / editing file data for my program.. Need HELP!

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Working on reading / editing file data for my program.. Need HELP!

    Okay so I have been making a lot of changes and upgrades to my program as I continue to design it. Currently I am trying to implement a lot of features, which I am going to tackle 1 at a time. As of right now this is reading and displaying from a .txt file.


    I need a little direction and clarification. I have been studying a lot on reading and writing to a .txt file.

    Right now my program currently runs and operates fine.

    I want my program to display the information from a .txt file instead of my Object array. This I have figured out somewhat. I did not provide the .txt file, as it may not have been needed. However I have my program to the point where it will read from a text file.

    This I have not implemented into my GUI yet. This is what I need some direction with.. Essentially I need help on where to go from here.. I can read information from the .txt but how can I get the program to display this information in my GUI?

    Currently I need this information of this format to display..
    Red Hot Chili Peppers Alternative 1 35 16.99 (CDName, Genre, ID#, Stock, Price)

    From my understanding I can simply have spaces in between my information to display. but I do not know how to make it so it will display Red Hot Chili Peppers as it has spaces (it wants to call Red as one then Hot, then Chili, etc...)


    HERE IS WHAT I SPECIFICALLY NEED>>>> tips, tricks, and a direction to head. I beleive I am on the right track from here but perhaps need a bit of clarification on how to work this into my program.



    *** IMPORTANT QUESTION - PLEASE ANSWER ***

    This change to my program seems as if I may be able to delete not only my Object CD array but all the getters and setters as well?

    OR

    Will I still need all of that?



    ** CDs Class (contains main method)
    package inventory;
     
    import javax.swing.*;
     
    import java.util.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.lang.*;
     
     
     
     
     
    public class CDs extends JFrame{
    private static final long serialVersionUID = 1L; // serial version
     
     
    		//// this area controls the open, add, and closing of the file
    		private Scanner x;
     
    		public void openFile(){
    			try{
    				x = new Scanner(new File("test.txt"));
    			}catch(Exception e){
    				System.out.println("An error occured.");
    			}
    		}// end openFile
     
    		public void readFile(){
    			while(x.hasNext()){
    				String a = x.next();
    				String b = x.next();
    				String c = x.next();
    				String d = x.next();
                                    String e = x.next();
    				System.out.printf("%s %s %s %s %s\n", a, b, c, d, e);
    			}
    		}
    		public void closeFile(){
    			x.close();
    		}
     
    		/// end file open, add, and close
     
     
     
    // declares var for currentIndex for button functionality
    	public int currentIndex = 0;
    	private JLabel labelName;
    	private JLabel labelID;
    	private JLabel labelStock;
    	private JLabel labelGenre;
    	private JLabel labelPrice;
    	private JLabel labelReStock;
    	private JLabel labelFval;
    	private JLabel labelTval;
    	private InvMore[] CD;
    	private JLabel imageLabel;
     
     
     
     
     
    	public CDs(){// sets the GUI 
     
     
    		CD = new InvMore[5]; // creates the variable CD for the array
    		/// CD inventory objects
    		CD[0] = new InvMore("Red Hot Chili Peppers", "Alternative", 1, 35, 16.99);
    		CD[1] = new InvMore("Nirvana", "Classic Rock", 2, 25, 16.99);
    		CD[2] = new InvMore("Styx", "Classic Rock", 3, 28, 16.99);
    		CD[3] = new InvMore("Jay Z", "Hip Hop/Rap", 4, 37, 16.99);
    		CD[4] = new InvMore("Nelly", "Hip Hop/Rap", 5, 32, 16.99);
     
     
     
     
    		// sets the values for the window
     
    		setTitle("Your Inventory Program");
    		setSize(600,550);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
     
    		JPanel panel1 = new JPanel();
    		getContentPane().add(panel1);
    		panel1.setLayout(new FlowLayout(FlowLayout.CENTER));
    		// creates image logo
    		imageLabel = new JLabel();// creates the label for the logo
    		imageLabel.setIcon(new ImageIcon("D:\\UOP111\\JavaWork\\inventory\\src\\inventory\\TMDlogo.png")); // found out that you need to have the  \\ when calling the directory
    		panel1.add(imageLabel);
     
     
     
     
    		//creates the panel and layout
    		JPanel panel = new JPanel();
    		getContentPane().add(panel, BorderLayout.NORTH);
    		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
     
    		/// fields numbered 1-8 for easy matching with other functions and adding
    		labelName = new JLabel ("CD Name: ");//1
    		labelName.setFont(new Font("Serif", Font.PLAIN, 14));
    		panel.add(labelName);//1
     
    		labelID = new JLabel ("CD ID: ");//2
    		labelID.setFont(new Font("Serif", Font.PLAIN, 14));
    		panel.add(labelID);//2
     
    		labelGenre = new JLabel ("CD genre: ");//3
    		labelGenre.setFont(new Font("Serif", Font.PLAIN, 14));
    		panel.add(labelGenre);//3
     
    		labelStock = new JLabel ("Stock amount: ");//4
    		labelStock.setFont(new Font("Serif", Font.PLAIN, 14));
    		panel.add(labelStock);//4
     
    		labelPrice = new JLabel ("CD Price: ");//5
    		labelPrice.setFont(new Font("Serif", Font.PLAIN, 14));
    		panel.add(labelPrice);//5
     
    		labelReStock = new JLabel ("ReStock value: ");//6
    		labelReStock.setFont(new Font("Serif", Font.PLAIN, 14));
    		panel.add(labelReStock);//6
     
    		labelFval = new JLabel ("CD stock value: ");//7
    		labelFval.setFont(new Font("Serif", Font.PLAIN, 14));
    		panel.add(labelFval);//7
     
     
     
    		/// creates the buttons
    		JButton previous = new JButton("Previous");
    		panel.add(previous);
    		JButton next = new JButton("Next");
    		panel.add(next);
     
     
     
     
     
     
     
     
     
     
    		//**** working on implementing this **** 
    		labelTval = new JLabel ("Total CD inventory value: ");//8
    		labelTval.setFont(new Font("Serif", Font.ITALIC, 14));
     
    		panel.add(labelTval);//8
     
     
     
     
    		///// these are the active listeners for the button functions
    			previous.addActionListener(new ActionListener() {
    				@Override
    				public void actionPerformed(ActionEvent e) {
    					showPrevious();
    				}
    			});
     
    		next.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				showNext();
    			}
    		});
     
     
     
     
     
    		/// calls the GUI display for object results
    		updateGUI();
    		InvMore item = CD[currentIndex];
    		labelTval.setText(String.format("The Total CD Inventory Value: $%.2f", item.gettotValue(CD)));//8
     
     
    	}//end GUI
     
     
     
     
     
     
    	public static void main(String[] args){ // start main
    		new CDs().setVisible(true); // this calls the GUI
     
     
    		// calls the file open, add, and close
     
    		CDs g = new CDs();
     
    		g.openFile();
    		g.readFile();
    		g.closeFile();
     
    		// end the file calls
     
    	}// end main
     
     
     
     
     
     
     
    	public void updateGUI() {
    		InvMore item = CD[currentIndex];
    			labelName.setText(String.format("Name: %s",item.getName()));//1
    			labelID.setText(String.format("ID: %s",item.getID()));//2
    			labelGenre.setText(String.format("Genre: %s",item.getGenre()));//3
    			labelStock.setText(String.format("In-Stock: %s",item.getStock()));//4
    			labelPrice.setText(String.format("Price: $%.2f",item.getPrice()));//5
    			labelReStock.setText(String.format("ReStock Value: $%.2f",item.getReStock()));//6
    			labelFval.setText(String.format("Total Stock Value: $%.2f",item.getValStock()));//7
     
    	}// ends updateGUI
     
     
     
    	/// methods for the buttons
    	private void showNext() {  // method for the next button
    		if (currentIndex < CD.length - 1) {
    			currentIndex++;
    			updateGUI();
    		}
    	}
    	private void showPrevious() { // method for the previous button
    		if (currentIndex > 0) {
    			currentIndex--;
    			updateGUI();
    		}
    	}
     
     
     
    }// end class

    ** Inventory Class
    package inventory;
     
    public class Inventory implements Comparable<Inventory>{
     
     
     
    		// variables
    		public String Name;
    		public int ID;
    		public int Stock;
    		public double Price;
    		public double ReStock;
    		public String Genre;
    		public double cdVal;
    		public double totValue;
     
     
     
    			public Inventory(String Name, String Genre, int ID, int Stock, double Price) { // constructor
    				this.Name = Name;
    				this.ID = ID;
    				this.Stock = Stock;
    				this.Price = Price;
     
    			}// end constructor
     
     
    			public void setName (String Name){ // setter for name
    				this.Name = Name;
    			} // end setter
    			public String getName(){ // getter for name
    				return Name;
    			} // end getter
     
    			public void setID (int ID){ // setter for id
    				this.ID = ID;
    			}// end setter
    			public int getID(){ // getter for ID
    				return ID;
    			} // end getter
     
     
    			public void setStock (int Stock){ // setter for stock
    				this.Stock = Stock;
    			}// end setter
    			public int getStock(){ // getter for stock
    				return Stock;
    			}// end getter
     
     
    			public void setPrice (double Price){ // setter for stock
    				this.Price = Price;
    			}// end setter
    			public double getPrice(){ // getter for stock
    				return Price;
    			}// end getter
     
     
    			public double getValStock(){ // getter for stock
    				return (Price * .05) * Stock + (Stock * Price);
    			}// end getter
     
     
    			/// this WAS the sort method used for my program before it needed a GUI
    			public int compareTo(Inventory other) {
    				if (getName().compareTo(other.getName()) > 0){
    					return 1;
    				}else if (getName().compareTo(other.getName()) < 0){
    					return -1;
    				}else
    					return 0;
    			}
     
     
     
     
     
    	}// end class


    ** InvMore Class
    package inventory;
     
    public class InvMore extends Inventory {
    	public Object testValue;
    	public InvMore(String Name, String Genre, int ID, int Stock, double Price) {
    		super(Name, Genre, ID, Stock, Price);
    		this.Genre = Genre;
    		this.ReStock = ReStock;	
    		this.cdVal = cdVal;
    		this.totValue = totValue;
     
     
    	}
     
    	public void setGenre (String Genre){ // setter for name
    		this.Genre = Genre;
    	} // end setter
    	public String getGenre(){ // getter for name
    		return Genre;
    	} // end getter
     
    	public double getReStock(){ // getter for stock
    		return (Price * .05);
    	}// end getter
     
     
    	public void settotValue (){
    		this.totValue = totValue;
    	}
    	public double gettotValue (InvMore[] CD){
     
    		double totValue = 0;
     
    		for (int i = 0; i < CD.length;) {
     
    			totValue = totValue + getValStock();
     
    			i++;
    		}
     
    		return totValue;
    	}
    }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Working on reading / editing file data for my program.. Need HELP!

    Quote Originally Posted by travis07 View Post
    I can read information from the .txt but how can I get the program to display this information in my GUI?

    Currently I need this information of this format to display..
    Red Hot Chili Peppers Alternative 1 35 16.99 (CDName, Genre, ID#, Stock, Price)
    This is a "structured" information but .... it's not really structured at the best.

    If the line was:
    Red Hot Chili Peppers;Alternative;1;35;16.99;

    It would be better structured (and more easily manageable).

    But supposing you can't (or don't want) to change the line format, it's possible to extract all the values. The first thing that comes to mind is the use of a regular-expression. Supposing that the genre is always a 1 word, it would be possible to extract the CDName part as a single text.
    And at GUI level a good solution is the use of JTable.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Working on reading / editing file data for my program.. Need HELP!

    What is the existing format of the text file? Can you show a couple lines? Are you in control of the format of the text file? It sounds like you are, and if that's true, you should delimit the data so that it can be read and separated into the desired parts as easily and reliably as possible.

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Working on reading / editing file data for my program.. Need HELP!

    well my text formatting of my file would look just like I have below

    Red Hot Chili Peppers Alternative 1 20 16.99
    Nirvana Alternative 2 20 16.99
    etc....


    I would like to use the structure that andbin had there. I would much rather use a format like
    Red Hot Chili Peppers; Alternative; 1; 20; 16.99;
    Nirvana; Alternative; 2; 20; 16.99;

    as stated it is much easier to manage and it makes more sense for the programming aspect. The biggest deal is that I do not understand how to make my program read a file like that...

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Working on reading / editing file data for my program.. Need HELP!

    Quote Originally Posted by travis07 View Post
    The biggest deal is that I do not understand how to make my program read a file like that...
    First, you need to read "lines". You can use readLine() of BufferedReader or nextLine() of Scanner. Obviously in a loop, in order to read all lines.

    Second, for each line you need to extract all the tokens. You can use split() of String or a StringTokenizer (however, they have a different semantics).

    Third, having the tokens you need to convert (parse) string numeric values in order to have primitive values.

    Fourth, having all informations for an item, put them in an object that "models" these informations.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Reading File and Outputting Data
    By ChrisMessersmith in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 13th, 2011, 01:40 PM
  2. I/O stream for reading and editing a file full of numbers
    By Harry Blargle in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 5th, 2011, 09:00 AM
  3. Help with Scanner class - Reading Data from a file
    By billias in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 28th, 2011, 12:07 PM
  4. Help reading data from file, manipulating then rewriting out.
    By Nismoz3255 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 19th, 2011, 09:13 AM
  5. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM