Connecting Tech Pros Worldwide Forums | Help | Site Map

substring

Newbie
 
Join Date: Oct 2009
Posts: 5
#1: Oct 13 '09
Hello,

I am reading in a file and breaking it up using substring. This is working well except for the last part I need to read in needs to be an integer, not a string. I was curious if it is possible to use substring to break up both strings and ints.

Here is the code I have:

Expand|Select|Wrap|Line Numbers
  1.  
  2. String input = "";
  3.  
  4. while(readFile.hasNextLine())
  5.             {
  6.                 input = readFile.nextLine();
  7.                 sName = input.substring(0, 14);
  8.                 String sCap = input.substring(15,29);
  9.                 String sAbbr = input.substring(30,32);
  10.                 String sPop = input.substring(32,40);
  11.                 String sReg = input.substring(40,56);
  12.                 int sRegNum = input.substring (56); // this breaks
  13.  
  14.  
  15.                State lilState = new State(sName.trim(), sCap, sAbbr,
  16.                         sPop, sReg, sRegNum);
  17.                stateObject1[lilState.getRegionNum()-1].queueInsert(lilState);
  18.              }
  19.  
Thanks for any help that can be offered.

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,948
#2: Oct 14 '09

re: substring


See the parseInt() method of the Integer class.

Mark.
Newbie
 
Join Date: Oct 2009
Posts: 5
#3: Oct 14 '09

re: substring


Thanks for the quick reply.

Will I be able to use the parse int method in the same while statement using substring, or will I have to read it seperate?
Newbie
 
Join Date: Oct 2009
Posts: 5
#4: Oct 14 '09

re: substring


Think I figured out how to parse it

Expand|Select|Wrap|Line Numbers
  1. int sRegNum = Integer.parseInt(input.substring(56));
Thanks for the help!
Reply


Similar Java bytes