Solved

Problem to the codes

Posted on 2015-02-11
3
90 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 11

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

729 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