Results 1 to 7 of 7

Thread: Array

  1. #1
    mikerousse is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Array

    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 :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.
    Any ideas?

  2. #2
    mikerousse is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: Array

    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

  3. #3
    mikerousse is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: Array

    OK I find the problem but now i see that the
    Java Code:
    [B]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);
    		}
    	}
    }
    [/B]
    String ProdName & quantity is useless. How xan I make them to work?
    Last edited by mikerousse; 12-08-2013 at 06:31 PM.

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

    Default Re: Array

    How xan I make them to work?
    Can you explain what you want the program to do and what values you want to put in those variables?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mikerousse is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: Array

    what I want is to find the way to read from the file string and int too

    File txt look like this:
    asfasf 32
    fsevvv 54
    fgggf 65
    ggfgff 577

    I have to use tokenizer and split them in 2 column Left (String) & right (int).
    The integers I need them for other use.

    The other thing is to habe the possibility to read only the first 10 horizontal lines, after this to stop.

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

    Default Re: Array

    The posted code has the statements that define and assign values to those variables commented out? What happens when the //s are removed so the statements are compiled?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Array

    Also posted at: Array & file
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 09:39 PM
  2. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  3. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

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