Results 1 to 4 of 4

Thread: Array Error

  1. #1
    crueoj is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Question Array Error

    Array Error-capture.jpg
    I am trying to get the data from the input file into 6 different categories which I will then sort and do other things with. Pretty much what is supposed to happen is the program reads the first line of the squalmart.in file which will be the size of the array the categories will go into. Next it reads the next line from the programs and sets it equal to a string. This string is then read by another scanner which breaks it up into different pieces and then is placed in its proper array category.


    Array Error-capture2.jpg

    Array Error-capture1.jpg
    I keep getting this error and am unsure why. I've tried messing around with things a lot and still can't figure it out. Any suggestions would be a great help, thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Array Error

    To have the best chance of getting good help, consider posting your actual code text here surrounded by [code] [/code] tags, not hard to read images of your code.

  3. #3
    crueoj is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: Array Error

    Quote Originally Posted by Fubarable View Post
    To have the best chance of getting good help, consider posting your actual code text here surrounded by [code] [/code] tags, not hard to read images of your code.
    sorry hope this makes it easier

    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;	
    import java.io.*;
    
    public class Application
    {
    	//Reads the inventory of items from a departmant store and stores 
    	//them in an organized fashion.  Then prints a series of detailed 
    	//reports to an output file.  
    
    	public static void main (String[] args) throws IOException
    	{
    		int num_scans, instock;
    		String productInfo, skipline, prodCat;
    		Scanner strScan, brkScan;
    
    		strScan = new Scanner (new File("squalmart.in"));
    		
    		num_scans = strScan.nextInt();
    		//skip to next line
    		skipline = strScan.nextLine();
    		
    		String Product_Category[] = new String[num_scans];
    		String Product_Name[] = new String[num_scans];
    		int Quantity_On_Hand[] = new int[num_scans];
    		int Number_Sold[] = new int[num_scans]; 			
    		float Product_Price[] = new float[num_scans];
    		float Product_Cost[] = new float[num_scans];
    
    		for (int count = 0; count < num_scans; count++)
    		{
    
    			productInfo = strScan.nextLine();
    
    			brkScan = new Scanner (productInfo).useDelimiter("\t");
    
    			
    			prodCat = brkScan.next();
    				if (prodCat != "Automotive" || "Shoes" || "Women's Wear" || "Household" || "Home and Garden" || "Men's Wear")
    					System.err.println("Error: product category DNE. This line was discarded")	
    				else 
    					Product_Category[num_scans] = prodCat;
    			Product_Name[num_scans] = brkScan.next();
    			Quantity_On_Hand[num_scans] = brkScan.nextInt();
    			Number_Sold[num_scans] = brkScan.nextInt();
    			Product_Price[num_scans] = brkScan.nextFloat();
    			Product_Cost[num_scans] = brkScan.nextFloat();
    
    		}
    		 
    	}
    }

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Array Error

    If an array has 'num_scans' elements it has index values 0, 1, 2, 3 ... num_scans-1 (they are zero based); trying to access an element with an index value 'num_scans' causes an out of bounds error; you're doing that all the time in your code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. HELP. Getting an error returning a 2d array
    By Orangepillow in forum New To Java
    Replies: 22
    Last Post: 02-12-2012, 07:11 PM
  2. an error in call array
    By HearT.Hunt3r in forum New To Java
    Replies: 17
    Last Post: 09-14-2011, 05:11 PM
  3. Array Size Error
    By pondwire in forum New To Java
    Replies: 5
    Last Post: 07-21-2011, 08:07 AM
  4. Saving Array Error
    By aianta in forum New To Java
    Replies: 1
    Last Post: 04-17-2011, 04:50 PM
  5. array error
    By blu_fox88 in forum New To Java
    Replies: 4
    Last Post: 04-25-2010, 12:18 PM

Tags for this Thread

Posting Permissions

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