• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Scanner in java.util

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone,


import java.util.*;
class Brain
{
public static void main(String[] args)
{
Scanner sc = new Scanner("123 A 3b c,45,x5X,76 82 L");
while(sc.hasNext())
{
if(sc.hasNextInt())
System.out.print(sc.nextInt()+":");
else
System.out.println(sc.next());
}
}
}
OUTPUT: 123:82

what I am not able to understand is,how come output is not 123:3:45:76:82
Please explain.

Regards,
-Radhika
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Radhika,
In this example if loop is looking for a integer value(sc.hasNextInt). And "white space" is a default delimiter of scanner class. That's why you are getting "123 82". They both are only the right entries for what code is looking for.
I think you will clear now.
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because by Default ,WhiteSPace is the delimiter ,unless we change it with useDelimiter.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to get 123 3 45 76 82, you have to have a space delimiter in between them. for 123 that is first entry having a white space forward, for 3 is not a valid integer(as it is 3b, so you will have it if you have \d\w expression), for 45, it has no white space(it can be read like c,45,), for 76, it has no white space(it can be read like x5x,76), for 82, it has a white space.
 
radhika ayirala
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies.I just want to clear one more thing.When the while loop is executed for the 3rd time,the token would be "3b",as "sc.hasNext()" returns "3b".And then the control goes to if condition,where it checks for "sc.hasNextInt()". My assumption is that the condition gives the boolean value 'true' as '3' is an integer.Please tell me if iam correct or wrong.
-Radhika.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Condition doesn't gives you "true", because it is not an integer it is a "3b". That is the reason for what 3b is not in the output.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this thread is old but just wanted to comment on the output of this code because when i went through the code and looked at the output posted i thought i was wrong but it seems like no one compiled and ran this code. When i compiled and ran the code i got:

123:A
3b
c,45,x5X,76
82:L
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe sridhar is correct. Guess everyone just ignored the "System.out.println" in the else part of the program.

If we replace this by simple "sc.next()"....the output would match the one given.

But in the end I think the real question was understood and explained well.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting this.
Where we need to replace sc.next().
Can some one please help me...
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dinesh -

Can you show us what you've tried when you've compiled and run this code? Have you added any System.out.println statements?

We're not trying to be hard on you Dinesh, but we've all experienced the fact that really the ONLY way you're going to be able to learn this stuff is by writing code, running it, testing it, and trying lots of stuff out.

I'm not ashamed to admit to you that when we wrote the book we wrote HUNDREDS of programs and for many of them we tested many variations so that we could learn how this stuff works.

Thanks,

Bert
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class Brain
{
public static void main(String[] args)
{
Scanner sc = new Scanner("123 A 3 b c,45,x5X,76 82 L");
while(sc.hasNext())
{
if(sc.hasNextInt())
System.out.println(sc.nextInt()+":");
else
System.out.println(sc.next());
}
}
}

o/p--

123:
A
3:
b
c,45,x5X,76
82:
L

This is wht i got when i complied the prog..
 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic