• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reg expression

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
In the below pro iam getting output as 1,2 but i except 1,2,12,25 since mextInt will move the pointer to next int value.please help me in this regard
thanks in advance
sankar

import java.util.*;
import java.io.*;
class reg4
{
static long aLong;
public static void main(String ava[])
{
try{
Scanner sc = new Scanner("1 2asa 12,25");
//assert(sc.hasNextInt()):"nega value"+sc.hasNextInt();
do{
//aLong += sc.nextInt();
System.out.println(sc.nextInt());
}while(sc.hasNextInt());
}catch(Exception e){e.printStackTrace();}//System.out.println(aLong);
}}
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sankar,

If you put your code inside the "" tag, it would look
more better and readable. It may attract greedy ranch fellows to answer your
post.


Thanks,
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the below pro iam getting output as 1,2 but i except 1,2,12,25 since mextInt will move the pointer to next int value.please help me in this regard



The nextInt() method does not "move the pointer to the next int value". It tries to interpreted the next token as an int value. The hasNextInit() method doesn't try to find if there are more ints in the string, it returns whether the next token can be interpreted as an int.

Anyway, if you only want ints, one way to fix this is to make everything that is not a digit, a delimiter. Like so...



Henry
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from that the output will be 1 as henry already said "The hasNextInit() method doesn't try to find if there are more ints in the string, it returns whether the next token can be interpreted as an int."
[ June 26, 2007: Message edited by: nitin pokhriyal ]
[ June 26, 2007: Message edited by: nitin pokhriyal ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic