Solved

Problem to the codes

Posted on 2015-02-11
3
82 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 142

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 84
groupSum6 challenge 6 35
What is JNDI datasource in spring 1 38
base64 decode encode 12 96
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now