Solved

Problem to the codes

Posted on 2015-02-11
3
88 Views
Last Modified: 2015-02-11
Hi,
I put
0750-105633-462

to 2nd parameter below
	public static int ValAcctNo (String ItemCode,
			String ItemAcctNo)
	{
		if (!ItemCode.equals("049"))
		{
			JOptionPane.showMessageDialog(null,
				    "Item Code should be '049'.",
				    "Inane warning",
				    JOptionPane.WARNING_MESSAGE);
			return 2;
		}
		String Acct,str1,str2,str3;
		Acct=ItemAcctNo.trim();
		str1=Acct.substring(0, 4);
		str2=Acct.substring(5, 6);
		str3=Acct.substring(12, 3);
		...

Open in new window


but I get this
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -10
	at java.lang.String.substring(Unknown Source)
	at com.hkicl.ecp.hkanvm.b049.ItemAcctNoValidator_049.ValAcctNo(ItemAcctNoValidator_049.java:66)
	at com.hkicl.ecp.hkanvm.b049.ItemAcctNoValidator_049.main(ItemAcctNoValidator_049.java:45)

Open in new window

due to last line in above. why?
0
Comment
Question by:HuaMinChen
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40602592
it means that the string Acct is not long enough.
you need to check the string length before, it must be 15 or longer
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40602595
But the input string is with length 15.

BTW, is there any way to go to one specific place within the same event?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 40602634
Try something like

	String ACC_NO_PAT = "\\d{4}-\\d{6}-\\d{3}";
	if (!itemAcctNo.matches(ACC_NO_PAT)) {
	    throw new RuntimeException(String.format("Current item account number (%s) does not match pattern %s",
		    itemAcctNo, ACC_NO_PAT));
	}
	String[] atoms = itemAcctNo.split("-");
	System.out.println(Arrays.toString(atoms));

Open in new window


btw, Java variable names start lower case
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
spring jars download 1 35
How to convert from xls to xlsx using java 7 55
mysql jsp example issue 32 48
Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question