• 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 to use Scanner class to read multiple lines

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program that:
- asks for a number - amount of strangers to meet,
- then reads stranger names line by line
- and, finally, prints line by line "Hello, _stranger name_" for each stranger.

It is guaranteed that the input is not null.
It is guaranteed that the input of strangers count is int number.

Consider special cases:
- If strangers count is zero, then program must print "Oh, it looks like there is no one here".
- If strangers count is negative, then program must print "Seriously? Why so negative?".



Input:
3
Athos
Porthos
Aramis
Output:
Hello, Athos
Hello, Porthos
Hello, Aramis
---
Input:
0
Output:
Oh, it looks like there is no one here
---
Input:
-3
Output:
Seriously? Why so negative?



I tried to do like this. It works for the first two conditions, but for the last its not working properly.

Do you know where am I wrong?
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ItDoesntWorkIsUseless. In the future, please tell us what output you were expecting and what the program did instead.

I'm assuming that by the first two conditions, you mean the first two if-statements in your code, and not the first two cases described in your assignment.

By "it's not working properly", I'm guessing that it continuously keeps asking for new input, and that input of each name also immediately follows the output of the previous name. To fix that, you need to do the following:

  • Instead of using scanner.hasNext() in a while-loop, you need to repeat the loop numberStrangers times.
  • You first need to read in every stranger's name and save them somewhere. You can use an array or a List for this. Don't print them yet.
  • After you've read in numberStrangers names, only then print all the saved names.

  • Some other comments:

  • There is no need to make your class public, so don't.
  • Make your classes final, unless you have a good reason not to.
  • Why did you declare that the method throws IOException? It doesn't.
  • Please write out variable names. Use numberOfStrangers instead of numberStrangers.
  • Why is your variable named "words" when its purpose it to hold a "name"?
  • You can simplify the printing of a name to just: System.out.printf("Hello %s%n", name);
  •  
    Cris Marinescu
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    The classes where public from the description of the statement.
    I didn't declare the method throws IOException. It is in the stament of the problem.

    I tried to use BufferedReader, but i have the same wrong results.
    My problem is with first condition. I still don't know how to solve it. Any advices?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15485
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Cris Marinescu wrote:I tried to use BufferedReader


    Why?

    My problem is with first condition. I still don't know how to solve it. Any advices?


    Yes. Try the advice I already gave you.
     
    Cris Marinescu
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator



    I'm stuck at this part:
    " You first need to read in every stranger's name and save them somewhere. You can use an array or a List for this. Don't print them yet.
    After you've read in numberStrangers names, only then print all the saved names."

    Can you give more details about how to use an array to save the input? I don't know how to do that.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15485
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Have you worked with arrays before?

    Declare and initialize an array like this:

    Assign a value at an index in the array like this:
     
    Cris Marinescu
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    I did like this, but I'm stuck!

    I don't know what is this name in my code
    names[i] = name;

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15485
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Assigning scanner.next() to names[i] is fine. The problem is that you're still trying to print something before you have collected every name.

    First finish the loop that reads each name into the array. Then use a second loop to get the names out of the array and print them. You can use the enhanced for-loop syntax like this:

    When you've updated your code, please post the entire program.
     
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are starting the loop at '1' which is a problem for names[i] where the first name needs to be stored at index '0'. In Java all array indexes start with zero.
     
    Cris Marinescu
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    I did how you told me, it's working for first two conditions. But for the last one when I put an input less than 0 it shows me this error "Exception in thread "main" java.lang.NegativeArraySizeException: -1 ".

    What you think its the problem? I checked a lot of times, but still I can't figure out.
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Line 7 should be after line 8. Only allocate the array when you know it's positive.
     
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are trying to create the array too early. You cannot create arrays with sizes < 0. The way your program works, you can (should?) make the array a variable with restricted scope inside the if.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15485
    363
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This goes hand in hand with a general programming rule: Don't declare variables in a broader scope than you need.

    You don't need the names array outside of the if-statement. You only need it when the number of strangers is greater than zero. So declare the array inside your first condition, not outside of it.
     
    Cris Marinescu
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Its working good, but when I'm testing I have some test failures.
    I noticed that I have one more hint in statement description. Perhaps it has something to do with it.

    Hint: In case you use the Scanner class, it might be helpful sure to check strings it reads be non-empty.

    What does not mean that? How can I check strings it reads be non-empty?
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Read string into temporary variable then check that to see if it's empty, if not, then assign it to the array.
     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I forgot ... if it IS EMPTY then don't advance the 'i' index.
     
    Cris Marinescu
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Either way, I still have some test failures
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15485
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "Test failures" doesn't really tell us much. Usually when a test fails, the failure message says what is wrong. Please tell us what the tests say, and how you are running the tests.
     
    Cris Marinescu
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It is only one message: "There are test failures". I don't have other description, unfortunately.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15485
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Again, how are you running the tests?
     
    Cris Marinescu
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm working on a platform and they show me in a specific way. In Maven -> Lifecycle    
    -> test, these are the steps I'm doing.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The if‑else in lines 12‑16 is unnecessary because a Scanner with the default delimiter cannot return an empty String from next().
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The if in line 37 is incorrect and will fail to populate your array.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15485
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Cris Marinescu wrote:In Maven -> Lifecycle -> test, these are the steps I'm doing.


    Then there must be a file named pom.xml in the root folder of your project. Please show that file to us.

    Also, if there are any files under src/test/java, then please show those to us as well.
     
    Cris Marinescu
    Ranch Hand
    Posts: 77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    pom.xml



    and this is HelloStrangersTest



     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Two issues:
    1) On lines 11 & 15 you read next which means in some cases you'll call next twice for one input resulting in two different strings, and running out of strings too quickly.
    2) This requirement
    //Then reads stranger names line by line and prints line by line
    means you should not be using next to read input but nextLine() instead.
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:. . . you should not be using next to read input but nextLine() instead.

    But remember that nextLine() after nextInt() will usually return an empty String. More information about that phenomenonin this old post.
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you guys, finally works as expected 🙂😊



     
    Carey Brown
    Saloon Keeper
    Posts: 10687
    85
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You shouldn't mix calls to next() with calls to  nextLine() inside the loop. I think if you test with first and last names and then put in an empty string you will not get the expected results. In the "else" block all you need is "i--" to put the counter back one.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic