• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!