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 9 of 9

Thread: Array & file

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

    Default Array & file

    Hi guys,

    I have create the following program and I am little stuck here.
    Here is the code:

    import java.io.*;
    import java.util.StringTokenizer;
     
    class ProductNameQuan{
    public static void main(String[] args)
    {
    String fileName = "stockhouse.txt";
    StringTokenizer line;
    String ProdName;
    String quantity;
     
    try {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    line = in.readLine();
    while (line != null) {
    ProdName = line.nextToken();
    quantity = line.nextToken();
     
    System.out.println(line);
    line = in.readLine();
     
    }
     
    in.close();
    } catch (IOException iox)
    {
    System.out.println("Problem reading " + fileName);
    }
    }
    }
    I am trying to find the way to read from the file the first 10 information's through the array (not arraylist)ProdName and the quantity." plus that I stack in the in.readLine(); propably is not compatible with the StringTokenizer.
    Now the other problem is that I need the quantity to be an integer instead of string.

    The file includes something like that:
    Ball 32
    tennis 322
    fireball 54
    ..
    .
    .
    .
    .
    Any ideas?


  2. #2
    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: Array & file

    Please read this topic to learn how to post code correctly and other useful tips for newcomers. Edit your post to fix it, if possible.

    Explain better what you want help with. Which of the parts you described have you stuck? Reading the file, storing the desired data in the array, etc.? Are you getting errors or unsatisfactory results? If so, copy the error from exactly as it appears at your end and paste it into your post. If the results are not as expected, copy a sample run and post that with a description of what needs to change.

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

    Default Re: Array & file

    Let's start with the 1st problem, the specific program gives me the below error:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Type mismatch: cannot convert from String to StringTokenizer
    Type mismatch: cannot convert from String to StringTokenizer

    at Main.main(Main.java:14)

    The second problem is the quantity, i need to change it to integer instead of string. How can I create the code?

    Last question, if I need to read from the file the first 10 line (String, int) and put it in array (not arraylist) how can I do it?

    Any help will be nice.

    Thanks

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

    Default Re: Array & file

    Quote Originally Posted by mikerousse View Post
    while (line != null) {
    ProdName = line.nextToken();
    quantity = line.nextToken();
    line is a String ... not a StringTokenizer.

    while (line != null) {
        StringTokenizer tokenizer = new StringTokenizer(line);
        ProdName = tokenizer.nextToken();
        quantity = tokenizer.nextToken();

    Quote Originally Posted by mikerousse View Post
    The second problem is the quantity, i need to change it to integer instead of string. How can I create the code?
    Use Integer.parseInt(String)

    Quote Originally Posted by mikerousse View Post
    Last question, if I need to read from the file the first 10 line (String, int) and put it in array (not arraylist) how can I do it?
    If you want to have each element of the array containing the String and the int, you have to define an apposite class that models this entity (named Product for example).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

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

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

    Default Re: Array & file

    Quote Originally Posted by andbin View Post
    line is a String ... not a StringTokenizer.

    while (line != null) {
        StringTokenizer tokenizer = new StringTokenizer(line);
        ProdName = tokenizer.nextToken();
        quantity = tokenizer.nextToken();
    The following give me the error:
    Type mismatch: cannot convert from String to StringTokenizer
    The constructor StringTokenizer(StringTokenizer) is undefined
    Type mismatch: cannot convert from String to StringTokenizer

    at Main.main(Main.java:14)

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

    Default Re: Array & file

    Quote Originally Posted by mikerousse View Post
    The following give me the error:
    Type mismatch: cannot convert from String to StringTokenizer
    The constructor StringTokenizer(StringTokenizer) is undefined
    Type mismatch: cannot convert from String to StringTokenizer
    Yes, sorry, I have not told you to change

    StringTokenizer line;

    to

    String line;

    Remember: readLine() returns a String ... not a StringTokenizer!
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

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

  7. The Following User Says Thank You to andbin For This Useful Post:

    mikerousse (December 8th, 2013)

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

    Default Re: Array & file

    Many thank I really appreciate it.

    --- Update ---

    [highlight=java]

    import java.io.*;
    import java.util.StringTokenizer;

    class Main {
    public static void main(String[] args) {
    String fileName = "myfile.txt";
    String line;
    //String ProdName;
    //String quantity;

    try {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    line = in.readLine();
    while (line != null) {
    StringTokenizer tokenizer = new StringTokenizer(line);
    //ProdName = tokenizer.nextToken();
    //quantity = tokenizer.nextToken();


    System.out.println(line);
    line = in.readLine();

    }

    in.close();
    } catch (IOException iox) {
    System.out.println("Problem reading " + fileName);
    }
    }
    }
    [/highlight=java]

    Even I take out the prodname & quantity the program still works which mean is useless.

    --- Update ---

    How can I make them to work?

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

    Default Re: Array & file

    Quote Originally Posted by mikerousse View Post
    Even I take out the prodname & quantity the program still works which mean is useless.

    How can I make them to work?
    Lines you have commented (declaration and assignment of ProdName/quantity) are not bad or unuseful. They serve as the base for the rest you still need to do.

    You have asked for other things:

    1) You want the quantity as an int. I have told you that there is parseInt, which is a static method of Integer class. So invoke Integer.parseInt(quantity) to obtain an int you should assign to a variable. Note that the parsing may fail with a NumberFormatException.

    2) You want an array. Do you known, in advance, how many entities you have in the file? An array must be instantiated with a well known size.

    3) You need a class to "model" the entity composed by product name plus quantity. Do you know how to define a class with some instance variables and appropriate accessor (setter/getter) methods?

    This is an example of a (really basic) Person class:

    public class Person {
        private String firstName;
        private String lastName;
     
        public String getFirstName() {
            retun firstName;
        }
     
        public String getLastName() {
            retun lastName;
        }
     
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
     
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }
    Do a similar thing for you "Product" class.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

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

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

    Default Re: Array & file

    the size of the array is 10 products and 10 numbers

    I love you

Similar Threads

  1. [SOLVED] Accessing && Editing properties of objects in an array. Plus a few more questions.
    By CameronFaust in forum Collections and Generics
    Replies: 31
    Last Post: August 10th, 2011, 07:35 PM