Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Split Method

  1. #1
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post

    Split Method

    Hi all,
    I am constructing a Java program which acts as shop managing system. Staff run the software and it collects the information from a file which is named data.txt. The information contained in this file is layed out as so:
    Item name, Item price
    I need to use the split method to seperate the name and the price and probably store them in an ArrayList, however I am not confident with the split method.
    ***This is the split method from the java.lang.String class***

    Here is the code below. I have written in red where the problem is.

    Code:
    import java.util.HashMap;
    import java.util.ArrayList;
    
    public class CoffeeShop
    {
        private HashMap<String, SalesPerson> employees;
        private ArrayList<Category> categories;
        private Reader menu;
        
        public CoffeeShop()
        {
            employees = new HashMap<String, SalesPerson>();
            categories = new ArrayList<Category>();
            menu = new Reader("data.txt");
            String[] items = menu.split(","); Error here...and don't know what to do.
        }
    }
    Any help is appreciated!
    Last edited by webguy08; 12-06-2008 at 07:25 PM.

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    In your code, menu is not a String is an object of type Reader. This is why your split method does not work, because you are not using the one in the String class. Depending on how your Reader class is intended, I think you have missed an intermediate step between reading the file and splitting the Strings.

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Perhaps like menu.toString();
    I added that step but I still get an error: "cannot find symbol - method split(java.lang.String)."

  • #4
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    Post more of your code. The problem is, you are not using the split method on a String. I don't know what a Reader object is in the context of your program so I cannot tell what options you have regarding your menu variable. Split will work fine if you actually have a String, for example:

    Code:
    String myString = "blah,blah,blah";
    String[] stringArray = myString.split(",");
    This code should work fine if I didn't mistype it. You will need an actual String object to use the split method on, and unless a Reader is a subclass of String, it doesn't appear that what you are trying to do will work. Post your Reader class and it will shed more light on the solution.

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Code:
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    
    public class Reader
    {
        private Scanner scanner;
        private boolean echo;
    
        public Reader()
        {
            scanner = new Scanner(System.in);
            echo = false;
        }
        
        public Reader(String inFile) {
            try {
                scanner = new Scanner(new File(inFile));
            } catch (FileNotFoundException f) {
                System.out.println("Error: The test file cannot be found.");
                System.exit(1);
            }
            echo = false;
        }
        
        public void echoOn() {
            echo = true;
        }
        
        public void echoOff() {
            echo = false;
        }
    
        public String readLine()
        {
            String text = scanner.nextLine();
            if (echo) {
                System.out.println("> " + text);
            }
            return text;
        }
        
        public int readInt(int max)
        {
            int i = 0;
            while (i<1 || i>max) {
                System.out.println("enter a number between 1 and " + max +
                  " and press return");
                i = scanner.nextInt();
                scanner.nextLine();
            }
            if (echo) {
                System.out.println("> " + i);
            }
            return i;
        }
    
        public void close() {
            if (echo) {
                System.out.println("> input closed");
            }
            scanner.close();
        }
    }

  • #6
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    Much easier to understand. You are skipping the step of reading the line from the file. Try something like this:

    Code:
    String line = menu.readLine();
    This will give you the next line of the file and you can use the split method on that value, because it will actually be a String.

  • Users who have thanked brad211987 for this post:

    webguy08 (12-07-2008)

  • #7
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Thanks for that!

    Now it reads as so...
    Code:
    public CoffeeShop()
        {
            employees = new HashMap<String, SalesPerson>();
            categories = new ArrayList<Category>();
            menu = new Reader("data.txt");
            String line = menu.readLine();
            String[] items = line.split(",");
        }
    ...and it compiles!


  •  

    Posting Permissions

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