Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8938025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:31:46+00:00 2026-06-15T10:31:46+00:00

Possible Duplicate: Java: Scanner stopping at new line I’m new to programming and I

  • 0

Possible Duplicate:
Java: Scanner stopping at new line

I’m new to programming and I was wondering if there is a way to identify when there is a new line in a Scanner. My method is supposed to take a Scanner of a text file and break the line if it is over 60 characters long without breaking it in the middle of a word.

The problem I’m having is since I’m going by each token my code doesn’t account for lines that are less than 60 characters and appends them onto the previous line up to 60 characters.

This is the method I have created:

public static void wordWrap3(Scanner s) {
    String k = "";
    int length = 0;
    while(s.hasNext()) {
        k = s.next();
        length = length + k.length() + 1;
        if (length>60) {
            System.out.print("\n");
            length = 0;
        }
        System.out.print(" " + k);
    }
}

This is the text I am using:

We're no strangers to love, You know the rules and so do I, 
A full commitment's what Im thinking of, You wouldn't get this from any other guy.
I just wanna tell you how I'm feeling, Gotta make you understand.
Never gonna give you up, Never gonna let you down, Never gonna run around and desert you.
Never gonna make you cry, Never gonna say goodbye, Never gonna tell a lie and hurt you.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:31:47+00:00Added an answer on June 15, 2026 at 10:31 am

    You seem to be saying that you want to word wrap each line separately. If so, the simple solution is to split the input into lines, and then create a scanner for each line and pass that to your existing wordWrap3 method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java REPL shell Hey, Is there any way to execute Java code
Possible Duplicate: Java Regex Replace with Capturing Group Is there any way to replace
Possible Duplicate: Java/C# method representation in memory I was wondering if i have an
Possible Duplicate: Java : Is there a good natural language processing library Can anybody
Possible Duplicate: Why would one declare a Java interface method as abstract? The following
Possible Duplicate: Simple way to repeat a String in java How can I convert
Possible Duplicate: Java split() method strips empty strings at the end? In Java, I'm
Possible Duplicate: Scanner vs. BufferedReader Is there any situation in which it's apropriate to
Possible Duplicate: Java: Detecting client disconnects from server side What is the best way
Possible Duplicate: Java split() method strips empty strings at the end? The split method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.