• 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

How do I make Scan.findInLine(word).charAt(number); Scan a known string and not an input?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I'm having an issue with my program that needs 2 lines of input when I only want one. First I call Scan.nextLine(); to fetch a word. Then I want Scan.findInLine(word).charAt(number); to scan the string word for a letter instead of having to put another line in the console. How do I make Scan.findInLine(word).charAt(number); scan a single String instead of scanning input typed by the user? Just FYI, I have tried to read and read to find solutions, I'm currently reading Learning How To Program Java For Dummies. But I've also read online posts on similar issues, but I just don't get it, hopefully you can help!

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

Joseph Alrawi wrote:
I'm having an issue with my program that needs 2 lines of input when I only want one. First I call Scan.nextLine(); to fetch a word. Then I want Scan.findInLine(word).charAt(number); to scan the string word for a letter instead of having to put another line in the console. How do I make Scan.findInLine(word).charAt(number); scan a single String instead of scanning input typed by the user? Just FYI, I have tried to read and read to find solutions, I'm currently reading Learning How To Program Java For Dummies. But I've also read online posts on similar issues, but I just don't get it, hopefully you can help!



The scanner class allows you to read from different types of inputs ... String, or various forms of streams, etc. However, a single Scanner instance is only configured for one source. So, if you are using a scanner instance to get a string, and then, you want to scan that string for a word, then you need to create another scanner instance using that string as the input source.

Henry
 
Joseph Alrawi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Joseph Alrawi wrote:
I'm having an issue with my program that needs 2 lines of input when I only want one. First I call Scan.nextLine(); to fetch a word. Then I want Scan.findInLine(word).charAt(number); to scan the string word for a letter instead of having to put another line in the console. How do I make Scan.findInLine(word).charAt(number); scan a single String instead of scanning input typed by the user? Just FYI, I have tried to read and read to find solutions, I'm currently reading Learning How To Program Java For Dummies. But I've also read online posts on similar issues, but I just don't get it, hopefully you can help!



The scanner class allows you to read from different types of inputs ... String, or various forms of streams, etc. However, a single Scanner instance is only configured for one source. So, if you are using a scanner instance to get a string, and then, you want to scan that string for a word, then you need to create another scanner instance using that string as the input source.

Henry



Hello Henry.

I have before tired out making another scanner like:

Scanner Scan2 = new Scanner(word);ยจ

Using it in Scan2.findInLine(word).charAt(number);

The problem is that I get an error when running the program afterwards saying:

Exception in thread "main" java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Default.Horizon.main(Horizon.java:14)


Here's my code if you need further info:

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joseph Alrawi wrote:The problem is that I get an error when running the program afterwards saying:
Exception in thread "main" java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Default.Horizon.main(Horizon.java:14)


So: what is the value of 'word' at line 14?

I hope you know by now that Java passes everything (including references) by value; and if not, you should read the link (and possibly also it's "part 1" page).

HIH

Winston
 
Joseph Alrawi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Joseph Alrawi wrote:The problem is that I get an error when running the program afterwards saying:
Exception in thread "main" java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Default.Horizon.main(Horizon.java:14)


So: what is the value of 'word' at line 14?

I hope you know by now that Java passes everything (including references) by value; and if not, you should read the link (and possibly also it's "part 1" page).

HIH

Winston



Hey, I've known that yes, I just didn't think of it at all, how embarrassing. Thanks for your help, the problem is finally fixed!
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joseph Alrawi wrote:Hey, I've known that yes, I just didn't think of it at all, how embarrassing.


Don't worry. We've all been there.

Thanks for your help, the problem is finally fixed!


Great! Glad we could help.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic