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

Thread: logic error somewhere.. working with try & catch, simple array.. please help

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

    Default logic error somewhere.. working with try & catch, simple array.. please help

    // CS 0401 Fall 2010
    // Lab 10
    // You must modify this file so that it works as described in the lab handout.
    import java.util.*;
    import java.io.*;
    import java.lang.*;
     
    public class lab10
    {
    	public static void main(String [] args) throws IOException
    	{
    		Scanner inScan, fScan = null;
    		int [] A = new int[5];
    		inScan = new Scanner(System.in);
    		String fName;
     
     
    		do
    		{
    			System.out.println("Please enter the file to read from: ");
    			fName = inScan.nextLine();
    		} while (!fName.toString().equals("lab10out.txt"));
     
     
    		fScan = new Scanner(new File(fName));
    		String nextItem = " ";
    		int nextInt = 0;
    		int i = 0;
     
    		while (fScan.hasNextLine())
    		{
    			try
    			{
    				nextItem = fScan.nextLine(); // reads all lines as strings
    				nextInt = Integer.parseInt(nextItem); // then reads all lines as ints
    				A[i] = nextInt;     // all of the numbers are stored in 
    				i++;                 // processes through array until final line is reached
     
    			if (i>A.length)    // if incremented more than array size 
    			{
    				int [] temp = new int[A.length*2];  // create new array with the size of array A * 2 ( doubled) == A = 10
    				int j;
    				for(j=0; j<A.length; j++)
    				{
    					temp[j] = A[j];  // put all of the values of old array into new array Temp 
    				}
    				temp[j] = nextInt;  // all the new numbers are stored in nextInt soo nextInt = 10
    				i++;
    				A = new int[temp.length]; // new Array size now array size A is 10
     
     
    			}
    			}catch (NumberFormatException e1)
    			{
    				System.out.println(nextItem + "  is not an integer -- Ignored");
    			}catch (ArrayIndexOutOfBoundsException e2)
    			{
    				System.out.println("Resizing array from " + A.length + "to" +
    								(A.length*2) + ".");
     
    			}
    		}
     
    		System.out.println("Here are your " + i + " items:");
    		for (int j = 0; j < i; j++)
    		{
    			System.out.println(A[j] + " ");
    		}
    	}
    }

    Heres my output:
    bogus is not an integer -- Ignored
    weasel is not an integer -- Ignored
    Resizing array from 5to10.
    Resizing array from 5to10.
    3.5 is not an integer -- Ignored
    Resizing array from 5to10.
    Resizing array from 5to10.
    Resizing array from 5to10.
    Resizing array from 5to10.
    Resizing array from 5to10.
    Resizing array from 5to10.
    Here are your 5 items:
    100
    200
    300
    150
    400




    Needed 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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: logic error somewhere.. working with try & catch, simple array.. please help

    I suggest you step through your code with a debugger.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: logic error somewhere.. working with try & catch, simple array.. please help

    Have you tried putting your if(i>a.length) block inside of your catch block ? It looks to me like you are going to keep throwing the exception until you change your code to check A[i] is within bounds before attempting to assign it.

    good luck!

Similar Threads

  1. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 03:25 PM
  2. Simple error can't figure out.
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 30th, 2010, 12:19 PM
  3. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  4. Simple recursion logic
    By chronoz13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 24th, 2009, 10:53 PM
  5. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM