Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Why are Random Characters appearing in my output??

    I am part way through coding my program and am testing what I have done this far to make sure the data was read from the file correctly and the toString methods are formatting correctly. I am getting random brackets and comma's within the output and I don't understand why. I have the code, data file and output below. Thanks for any help! It's always the simple things like this that seem to give me problems!
    Last edited by dragstang86; 09-22-2011 at 11:53 PM. Reason: So nobody in my class steals my code!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Why are Random Characters appearing in my output??

    It looks like you are using the output from the ArrayList's toString method with your toString stuff in the middle.
    The ArrayList's toString method puts its output in []s and separates the items with commas.

    Where is the statement that does the printing?
    Last edited by Norm; 09-22-2011 at 11:46 PM.

  3. #3
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why are Random Characters appearing in my output??

    I defined toString() methods in each of the Furniture and Manufacturer classes which are the methods I am attempting to use. For debugging purposes I have been out putting the ArrayLists to the screen using the following code from within the Project1 class.

    Java Code:
            for(Object item : furn1)
    	{
    	    output += furn1.toString();
    			 
    	}//end of for loop
            
            for(Object item : man1)
    	{
    	    output += man1.toString();
    			 
    	}//end of for loop
            
            System.out.println(output);

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

    Default Re: Why are Random Characters appearing in my output??

    What are you printing out to display your results? What type of object is having toString() called on it?

    Asked another way, do you know of any Java classes that when toString() is called on them, returns several things held in square brackets with commas between them? If you know the answer to this, you'll know what is (actually not) wrong with your program.

    Edit: oops, Norm told you: you're calling toString() on ArrayLists. So I'd ignore the extra characters since the output is for testing purposes only. Else if you want prettier output, iterate through your lists with a for loop.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Why are Random Characters appearing in my output??

    Did we cross posts?
    Do you now know where the [] and , are coming from?

  6. #6
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why are Random Characters appearing in my output??

    I understand now! Thanks for the help!

  7. #7
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why are Random Characters appearing in my output??

    I have one other question that is giving me problems. What is the proper way to access a variable within an ArrayList of objects? I have no problem accessing the toString method but for some reason the same method doesn't work for accessing class variables or accessor methods. What I am trying to do is search through the objects in the ArrayList and search for any objects that have a class variable that matches.

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

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why are Random Characters appearing in my output??

    Java Code:
    List<Foo> myList = // etc
    // ...
    for(Foo foo :myList) {
        if(foo.bar == baz) {
            // do something with the match
        }
    }
    Or you can use the list's get() method in an ordinary for loop.

    -----

    Please don't delete previous posts. One person's stealing is another person's help. Better to use a SSCE which expresses your problem (and that of some future other person) but doesn't provide teh codez to your classmates.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why are Random Characters appearing in my output??

    Java Code:
    List<Foo> myList = // etc
    // ...
    for(Foo foo :myList) {
        if(foo.bar == baz) {
            // do something with the match
        }
    }
    Or you can use the list's get() method in an ordinary for loop.

    -----

    Please don't delete previous posts. One person's stealing is another person's help. Better to use a SSCE which expresses your problem (and that of some future other person) but doesn't provide teh codez to your classmates.

  11. #11
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why are Random Characters appearing in my output??

    I create both a Furniture and Manufacturer ArrayList in Project1Client and pass them to Project1. I am now trying to access the Class variables within Furniture and Manufacturer from with in the ButtonListener method within the Project 1 class and having trouble. I can access the toString() method but not the getIndex method.

    Project1Client
    Java Code:
    package project1.client;
    
    import java.util.*;
    import java.io.*;
    /**
     *
     * @author Bryan
     */
    public class Project1Client {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            ArrayList<Furniture> furn = new ArrayList <Furniture>();
            ArrayList<Manufacturer> man = new ArrayList <Manufacturer>();
            
            String information;
            
            
            try{
                
                File inFile = new File("C:\\Users\\Bryan\\Documents\\NetBeansProjects\\Project1 Client\\furniture inventory.txt");
                FileReader in = new FileReader(inFile);
                BufferedReader bufIn = new BufferedReader(in);
            
                information = bufIn.readLine();
                
                while(information != null){
                    
                    if(information.charAt(0) == 'F'){
                        furn.add(new Furniture (information));
                    }
                    
                    else if(information.charAt(0) == 'M'){
                        man.add(new Manufacturer (information));
                    }
                    
                    information = bufIn.readLine();
                }
                
                bufIn.close();
            }
        
    	catch(IOException ioe)
    	{
                ioe.printStackTrace();
    		 
    	}//end of catch
            
            
            Project1 object = new Project1(furn, man);
            
        }
    }
    Project1 Class

    Java Code:
    package project1.client;
    
    
    import javax.swing.*;
    import java.awt.*;              
    import java.awt.event.*;
    import java.util.*;
    import java.lang.*;
    
    /**
     *
     * @author Bryan
     */
    public class Project1 extends JFrame {
        
        private JButton search;
        private JPanel panel1, panel2;
        private JRadioButton furnIndexSearch, manIndexSearch, typeSearch, styleSearch;
        private JLabel keyword, searchCriteria;
        private JTextField searchBox;
        private JTextArea area;
        private boolean fisSelected, misSelected, typeSelected, styleSelected;
        private ArrayList furn1 = new ArrayList <Furniture>();
        private ArrayList man1 = new ArrayList <Manufacturer>();
        String output, term;
        
        
        public Project1(ArrayList furn, ArrayList man){
            
            super("Furniture Store Inventory Search");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(650,650);
            
            furn1 = furn;
            man1 = man;
            
            output = "";
            
            fisSelected = false;
            misSelected = false;
            typeSelected = false;
            styleSelected = false;
            
            keyword = new JLabel("Enter Keyword to search inventory", SwingConstants.CENTER);
            searchCriteria = new JLabel("Select Search Category", SwingConstants.CENTER);
            
            keyword.setForeground(Color.YELLOW);
            searchCriteria.setForeground(Color.YELLOW);
            
            furnIndexSearch = new JRadioButton("Search by Furniture Index");
            manIndexSearch = new JRadioButton("Search by Manufacturer Index");
            typeSearch = new JRadioButton("Search by Type");
            styleSearch = new JRadioButton("Search by Style");
            
            furnIndexSearch.setBackground(Color.YELLOW);
            manIndexSearch.setBackground(Color.YELLOW);
            typeSearch.setBackground(Color.YELLOW);
            styleSearch.setBackground(Color.YELLOW);
            
            ButtonGroup rbg = new ButtonGroup();
            rbg.add(furnIndexSearch);
            rbg.add(manIndexSearch);
            rbg.add(typeSearch);
            rbg.add(styleSearch);
            
            RadioButtonListener rbl = new RadioButtonListener();
            furnIndexSearch.addItemListener(rbl);
            manIndexSearch.addItemListener(rbl);
            typeSearch.addItemListener(rbl);
            styleSearch.addItemListener(rbl);
            
            search = new JButton("Search Inventory");
            
            ButtonListener bl = new ButtonListener( );
            
            search.addActionListener(bl);
            
            searchBox = new JTextField(20);
            
    
    
            for(Object item : furn1)
    	{
    	    output += furn1.toString();
    			 
    	}//end of for loop
            
            for(Object item : man1)
    	{
    	    output += man1.toString();
    			 
    	}//end of for loop
            
            //System.out.println(output);
            
            //area = new JTextArea(output);
            
            panel1 = new JPanel( );
    	panel1.setLayout(new GridLayout (0,1));
            
            panel2 = new JPanel( );
    	panel2.setLayout(new GridLayout (0,1));
            
            panel1.add(searchCriteria);
            panel1.add(furnIndexSearch);
            panel1.add(manIndexSearch);
            panel1.add(typeSearch);
            panel1.add(styleSearch);
            panel1.add(keyword);
            panel1.add(searchBox);
            panel1.add(search);
            panel2.add(area);
            
            panel1.setBackground(Color.BLUE);
            
            Container content = getContentPane();
            content.setLayout(new GridLayout(0,1));
            
            content.add(panel1);
            content.add(panel2);
            setVisible (true);
            
        }
        
        	public class RadioButtonListener implements ItemListener
    	{
                public void itemStateChanged(ItemEvent e){
                    
                    if(e.getSource() == furnIndexSearch){
    		   fisSelected = true;
     				
    		}//end of if statement
                    
                    if(e.getSource() == manIndexSearch){
    		   misSelected = true;
     				
    		}//end of if statement
                    
                    if(e.getSource() == typeSearch){
    		   typeSelected = true;
     				
    		}//end of if statement
                    
                    if(e.getSource() == styleSearch){
    		   styleSelected = true;
     				
    		}//end of if statement
    
    		
    	}//end of RadioButtonListener Class
            
            }
            public class ButtonListener implements ActionListener
    	{
                public void actionPerformed (ActionEvent e){
                    
                    if (e.getSource() == search){
                                            
                        if(fisSelected == true){
                            
                            Iterator iterator = furn1.iterator();
                            
                            for (Object x: furn1 ){ 
                                if(furn1.getIndex().equals(term)){
                            }
                            
                        }
                        
                        if(misSelected == true){
     
                        }
                        
                        if(typeSelected == true){
                        }
                        
                        if(styleSelected == true){
                            
                        }
                    }	
                }//end of actionPerformed()
    		
    	}//end of ButtonListener Class
        }
    Furniture Class
    Java Code:
    package project1.client;
    
    import java.io.Serializable;
    import java.util.*;
    /**
     *
     * @author Bryan
     */
    public class Furniture implements Serializable{
        
        String f;
        String index;
        String type;
        String style;
        String price;
        String indexOfManufacturer;
        
        public Furniture(){
            
            f = null;
            index = null;
            type = null;
            style = null;
            price = null;
            indexOfManufacturer = null;
            
        }
        
        public Furniture (String information){
            
            StringTokenizer data = new StringTokenizer(information, ":");
            
            f = data.nextToken();
            index = data.nextToken();
            type = data.nextToken();
            style = data.nextToken();
            price = data.nextToken();
            indexOfManufacturer = data.nextToken();
            
        }
        
     	  public String toString()
    	  {  
    	     //local variable to store String return value
    	     String returnValue = "";
                 
                 returnValue = "Furniture Index: " + index + "\n" + "Type: " + type + "\n" + "Style: " + style + "\n" + "Price: " + price + "\n" + "Manufacturer Index: " + indexOfManufacturer + "\n\n";
    		  
    	     return returnValue;
    
    	  }//end of toString()
              
              public String getIndex(){
                  
                  return index;
              }
        
    }
    Manufacturer Class
    Java Code:
    package project1.client;
    
    import java.io.Serializable;
    import java.util.StringTokenizer;
    
    /**
     *
     * @author Bryan
     */
    public class Manufacturer implements Serializable {
        
        String m;
        String index;
        String name;
        String streetAddress;
        String city;
        String state;
        String zip;
        String phone;
        
        public Manufacturer(){
            
            m = null;
            index = null;
            name = null;
            streetAddress = null;
            city = null;
            state = null;
            zip = null;
            phone = null;
            
        }
        
        public Manufacturer (String information){
            
            StringTokenizer data = new StringTokenizer (information, ":");
            
            m = data.nextToken();
            index = data.nextToken();
            name = data.nextToken();
            streetAddress = data.nextToken();
            city = data.nextToken();
            state = data.nextToken();
            zip = data.nextToken();
            phone = data.nextToken();
            
        }
        
        public String toString()
       {  
          //local variable to store String return value
          String returnValue = "";
    	
          returnValue += "Manufacturer Index: " + index + "\n" + "Name: " + name + "\n" + "Address: " + streetAddress + ", " + city + ", " + state + ", " + zip + "\n" + "Phone :" + phone + "\n\n";
    		  
          return returnValue;
          
       }//end of toString()
        
    }

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why are Random Characters appearing in my output??

    Sorry - should be SSCCE.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why are Random Characters appearing in my output??

    furn1 is declared as Object. Declare it as what it really is and you can call its methods. This will involve casting or - better - declaring the list precisely (see my code above).

  14. #14
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why are Random Characters appearing in my output??

    Quote Originally Posted by pbrockway2 View Post
    Java Code:
    List<Foo> myList = // etc
    // ...
    for(Foo foo :myList) {
        if(foo.bar == baz) {
            // do something with the match
        }
    }
    Or you can use the list's get() method in an ordinary for loop.

    -----

    Please don't delete previous posts. One person's stealing is another person's help. Better to use a SSCE which expresses your problem (and that of some future other person) but doesn't provide teh codez to your classmates.
    I have been trying the route you have been suggesting within the buffered reader method but the compiler (NetBeans) is complaining wanting "Foo" to be changed to Object and it still complains about ".bar". I posted what I tried below. I am trying to access the Furniture class variable index from within the ButtonListener method within the Project 1 class.

    What does SSCE refer to?

    Thanks.

  15. #15
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why are Random Characters appearing in my output??

    are you referring to the code within the ButtonListener class? If so that is just some random code left from many different attempts to figure this out. I initially declared it at the top of the class and assigned the ArrayList values passed into the the constructor to those I declared at the top of the class.

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

    Default Re: Why are Random Characters appearing in my output??

    Quote Originally Posted by dragstang86 View Post
    I have been trying the route you have been suggesting within the buffered reader method but the compiler (NetBeans) is complaining wanting "Foo" to be changed to Object and it still complains about ".bar". I posted what I tried below. I am trying to access the Furniture class variable index from within the ButtonListener method within the Project 1 class.
    Not all your ArrayLists are generic so the compiler has no idea what the list holds and requires that you either return an Object or cast the item before using it. Fix that -- make all your Lists and ArrayLists generic.

    What does SSCE refer to?
    Have a look: SSCCE

  17. #17
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Why are Random Characters appearing in my output??

    Google SSCCE, but basically it means posting brief code that just illustrates your problem without all the other stuff which is basically "noise". This involves some work on your part - and is not easy to begin with - but is an invaluable problem solving technique.

  18. #18
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why are Random Characters appearing in my output??

    I read up about SSCCE, thanks. I am still unsure what would or would not make my ArrayList generic? I understand the reasoning about the list not knowing what is in it but I still don't understand how to go about fixing it.

  19. #19
    dragstang86 is offline Member
    Join Date
    Jun 2011
    Posts
    46
    Rep Power
    0

    Default Re: Why are Random Characters appearing in my output??

    Basically what identifies the current item of the list? I am trying to capture the current object in the list into a temp object and then use the temp object to access its members but it is still not right. What I am trying is below:

    Java Code:
            Furniture furnitureItem = new Furniture();
    
            for(Object item : furn1)
            {
                furnitureItem = item;
                
                if (furnitureItem.index == "some index"){
                    //do something
                }
                   		 
    	}//end of for loop

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Why are Random Characters appearing in my output??

    it is still not right
    Please explain what the problem is.

    One problem I see is using == to compare the objects. You should use the equals() method to compare the contents of objects. == tests if the two variables refer to the same object.

Page 1 of 2 12 LastLast

Similar Threads

  1. output characters equal to an intiger value?
    By smokey888 in forum New To Java
    Replies: 5
    Last Post: 12-17-2010, 09:58 AM
  2. How to encode a fileHandler to output french characters ?
    By l.najm@acteos.com in forum Advanced Java
    Replies: 4
    Last Post: 04-22-2010, 09:21 AM
  3. JDialog appearing in the toolbar
    By riddick in forum New To Java
    Replies: 6
    Last Post: 11-06-2009, 02:19 PM
  4. Replies: 12
    Last Post: 09-23-2009, 09:14 AM
  5. jfilechooser not appearing
    By letter5 in forum New To Java
    Replies: 5
    Last Post: 02-15-2009, 04:45 PM

Posting Permissions

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