• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 23928
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 ]
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic