hi.
I read a .txt file and i want to put the values from it , to an array. I have dificulties.
here is the code i am using:

public void read(File afile) {

    FileInputStream fInpStr = null;
    BufferedReader bRead = null;
    StringTokenizer strTok = null;
    String line = null;
    String next = null;

    //temporary variables
    String variableName;
    double variableValue;

    //open the variable text file and catch any error.
    try {
      fInpStr = new FileInputStream(afile);
    }
    catch (FileNotFoundException e) {
      JOptionPane.showMessageDialog(null, "File not found " + afile,
                                    "Error", JOptionPane.ERROR_MESSAGE);
      return;
    }
    variableName="";
    variableValue=0.0;

    try {
      // Read in a line at a time from the text file
      bRead = new BufferedReader(new InputStreamReader(fInpStr));

       // first line
      variableName = bRead.readLine();
     
      // second line
      line = bRead.readLine();

      while (line != null) {

        strTok = new StringTokenizer(line,"/n",true);
       
        try {
          double []jim = new double[5];

          next = strTok.nextToken();
          for (int i = 0; i < 3; i++) {
            variableValue = Double.parseDouble(next);

            jim [i]= variableValue;

          }

          System.out.println(jim[1]);

        }
        catch (NumberFormatException e) {
          System.out.println(e.toString());
          JOptionPane.showMessageDialog(null, "Error reading file "
                                        + afile, "Error",
                                        JOptionPane.ERROR_MESSAGE);
        }

        line = bRead.readLine();
      }
      fInpStr.close();
    }
    catch (IOException e) {
      System.out.println(e.toString());
      JOptionPane.showMessageDialog(null, "Error reading file " + afile,
                                    "Error", JOptionPane.ERROR_MESSAGE);
    }

  } //end of read
}//end of class

The problem is that by this code insted of printing only one value (jim[1]), it prints all the values i have passed in from the .txt file.

Do i miss anything??
I cannot understand.
Thanks.

Recommended Answers

All 4 Replies

Thought I don't quite understand why you're doing this:

for (int i = 0; i < 3; i++) {
			variableValue = Double.parseDouble(next);

		    jim [i]= variableValue;

		  }

but that code is inside the while loop which goes through each token, or in this case number. So for every token found in the file, the println() method will be called.

I am trying to "take" the numbers from the text file and store them in a double array (that's why i am trying the "for loop").
Is there any other tway to "store" the numbers in the array?

I am reading "THE COMPLETE REFERENCE OF JAVA" by Herbert Schildt (Mc Graw Hill) and i am trying to solve this out, but i am cofused...

What i was saying is that you're writing the same number to the array 3 times.

next = strTok.nextToken();

That should be in the FOR loop, otherwise next never changes inside of it. Plus, that loop assumes there will be 3 tokens, which is not safe to do without some sort of pre-check. You basically have everything you need, just the order of the commands is a little off.

// read variable's name
	variableName = bRead.readLine();
	while (line != null)
	{
		//read line in while loop so all lines from file are read
		line = bRead.readLine();
		strTok = new StringTokenizer(line,"/n",true);
		
		//for the array below
		int count = 0;
		
		try
		{
			//ok to do ONLY if you know there will be 5 elements or less
			double []jim = new double[5];
			//while strTok has tokens left in its line
			while (strTok.hasMoreTokens())
			{
				//get next token
				next = strTok.nextToken();
				//convert to double
				variableValue = Double.parseDouble(next);
				//add double to array
				jim [count]= variableValue;
				count++;
			}
			for(int i=0; i<5; i++)
				System.out.prinln(jim[i]);
				
			//leave blank line between blocks of output	
			System.out.println("");
		}
	}

The main difference here is the extra WHILE loop instead of a FOR loop and uses StringTokenizer.hasMoreTokens();

Thank you very much for your help.
Now I understoud the mistake I did.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.