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

  • SEARCH
  • Home
  • 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 6844697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:24:36+00:00 2026-05-27T00:24:36+00:00

I have two classes. And I want the code in the for loop in

  • 0

I have two classes. And I want the code in the for loop in the class below to run twice, however when I sent int i = 0 and the condition to i < 2, it only seemed to run once. As you can see I’ve had to set it to i < 3 to get it to run just twice.

import java.util.Scanner;

public class LPrint {       
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("sender: ");

        String a = input.next();

        System.out.println("recepiant: ");

        String b = input.next();

        Letter one = new Letter(a, b);

        System.out.println("letter: ");

        for (int i = 0; i < 3; i++) {
            one.body = one.body.concat(input.nextLine() + "\n");
        }

        System.out.print(one.getLetter());
    }
}

In the other class, the String body = "", and it is only used in String result = ("to " + recepiant + ":\n").concat(body + "\n").concat("regards,\n").concat(sender);

  • 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-05-27T00:24:36+00:00Added an answer on May 27, 2026 at 12:24 am

    I think that your problem comes from reading an unwanted string containing only a new line on the first iteration through the loop. I guess your input looks something like this (with either a space or a newline between foo and bar):

    foo bar
    baz
    qux
    

    After the first two reads the scanner will be positioned just after the token bar but before the new line immediately following it. The call to nextLine will read the rest of the first line (i.e. the new line character). It does not read the second line as you intended. When you read three lines you get these strings:

    "\n"
    "baz\n"
    "qux\n"
    

    To solve the problem you can either use readLine instead of read to read the first two tokens, or else you can add an extra call to readLine just before your loop and discard any remaining characters from the current line, including the new line character.

    See this code on ideone that demonstrates the problem more clearly.

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

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
So I have two classes like this: class foo { /* code here */
Say you have these two classes. public class Author { public int ID {get;
First I want to describe my situation briefly. I have two classes, one MainClass
Given two model classes, Foo and Bar , I want Foo to have 3
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have two classes: Action and MyAction . The latter is declared as: class
I have a super-class named ClassA and two sub-classes Class1 and Class2. I have
I have two classes and I want to establish a many-to-many assications, here is

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.