I understand that this code will not run because i have 2 string and 1 float type in my text file and i am storing them as int's but how do i do this?.. please someone help me this is so frustrating

Here is my .txt file

100
200
bogus
300
150
weasel
400
600
250
3.5
800
50
500
450
600
550

// CS 0401 Fall 2010
// Lab 10

import java.util.*;
import java.io.*;
public class lab10
{
	
	public static void main(String [] args)
	{
		Scanner inScan;
		Scanner fScan = null;
		inScan = new Scanner (System.in);
		String nextItem;
		int nextInt = 0;
		int i = 0;
		
		
		int [] A = new int [5];
		
		boolean check = true;
		
		
		String textS = " ";
		
	while (check = true){
	      System.out.println("Please enter the file to read from: ");
		  String fName = inScan.nextLine();
		

		try {
		
			fScan = new Scanner (new File(fName));
			
			while (fScan.hasNextLine()) 
			{

			    nextItem = fScan.nextLine(); 

		            nextInt = Integer.parseInt(nextItem);
			
			    A[i] = nextInt; // array 
	  
			    i++;  // increments the counter 
		  
            }
            System.out.println("Here are your " + i + " items:");
			for (int j = 0; j < i; j++)
			{
				System.out.println(A[j] + " ");
				
			}

			
			
		} catch (FileNotFoundException e) {
			
			System.out.println("Your file is invalid ");
			

		}
	}// end while
		
		
	}
	
}

You can't store a String as an int, unless it can be parsed to an int. You can expressly cast a double to an int, but you'll lose the fractional part.

Furthermore, declaring an array to have five members means you can only store five values in it - there's a lot more than five values in your input file.


So what is it that you're trying to do? Describe the end state that you want to have. You want an array, I'm gathering, and what are the contents of the array supposed to be? Everything in the file? Only the ints? Only the numeric values?

i have to resize the array a few times.. but i am so confused on how

this is the output:

bogus is not an integer -- ignored
weasel is not an integer -- ignored
Resizing array from 5 to 10
3.5 is not an integer -- ignored
Resizing array from 10 to 20
Here are your 13 items:
100
200
300
150
400
600
250
800
50
500
450
600
550

Edited 6 Years Ago by javaking33: n/a

Okay, so you're ignoring non-int values and putting the ints into an array. Fair enough. Now I can look at your code and hope to get something from it.

First thing I'd suggest would be that you sub out adding to the array to a method. It's a perfect candidate for making the code more modular: it's an operation that has some logic to it (do I need to increase the array size? if so, do that. If not, put the value in the next slot. What's the next slot? - all of that is stuff you want to not be thinking about when you're trying to read a file. So that's my first suggestion. It might take a little more implementation than you want to deal with now, fair enough. However you do it, you should know if you don't already that the standard practice is to double the size of an array any time you need to increase it. This, I am told, minimizes the number of resizings you need to do while keeping your memory footprint under control.

Array resizing is actually a misnomer. You can't resize an array, you have to make a new one and copy the contents of the smaller into the larger. Again, this is a good thing to make into a method.


Next, you're using one try block for the whole file read, and only catching one exception, FileNotFound. This will be a problem, since you're going to be running into an exception when you hit the non-integer values, and you're not catching or throwing that. I would wrap the file opening code in one try block and the parseInt in another - you can use the sedond catch block to pass on the "not an integer - ignored" message.

So I would see your main loop as roughly doing the following:

- get the file name and open the file
 - loop through the file and attempt to parse each of its lines as an int, reporting failures and adding successes to the array
 - report out the results, that is, the list of integers you've stored in the array

In addition, you'd have two methods. One would take an integer and add it to the array. (Either The Array, you can use a global in this small program, or the array that you give it as a parameter, which is what you'd do in a more complex situation where you might have multiple arrays) This method would call the second method to resize the array if needed - that is, if this addition would take you over the edge of the array.


I hope that helps you organize your thoughts a little.

notuserfirendly's suggestion of using vector is very good.

Vectors will handle the resizing of your array because vectors are dynamic.

To create a vector,

vectorName = new Vector(5, 3);

then to convert that vector into an array,

arrayName = new String[vectorName.size()];
		
vectorName.copyInto(arrayName);
This article has been dead for over six months. Start a new discussion instead.