Solved

Problem to the codes

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 48
Java string replace 11 63
jsp login check 12 53
CSV file parsing thru Java 13 40
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…
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 …
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:
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

726 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