Solved

Problem to the codes

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 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

21 Experts available now in Live!

Get 1:1 Help Now