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 8679943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:07:13+00:00 2026-06-12T21:07:13+00:00

This is my assignment: Write a program where the user enters a string, and

  • 0

This is my assignment:

Write a program where the user enters a string, and the program echoes it to the monitor with one character per line:

C:\>java LinePerChar
Enter a string:
Octopus

O
c
t
o
p
u
s

I have tried, but I’m getting some compilation errors. Here’s my code:

import java.util.*;

class CharactorEcho{
    public static void main(String args []){

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a string :");

        try {
            String inputString = sc.nextLine();
            for(int i=0; i < sc.length(); i++) {
                char c = inputString.charAt(i);
                System.out.println("" + c);
            }
        } catch(IOException e) {

        }
    }
}
  • 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-12T21:07:16+00:00Added an answer on June 12, 2026 at 9:07 pm

    Two Issues:

    Change for(int i=0; i<sc.length(); i++){ to for(int i=0; i<inputString.length(); i++){

    You care comparing against the scanner and not the input string.

    Also, please try catching

       java.util.NoSuchElementException
       java.lang.IllegalStateException
    

    in place of IOException, as your statement sc.nextLine() with throws NoSuchElementException and IllegalStateException, not IOException.

    Make sure you add the related import statements.

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

Sidebar

Related Questions

I have an assignment to Write a C program that allows a user to
Here is the assignment verbatim: 1.) Write a Java program that is a TCP
for an assignment in my Java class I'm supposed to write a program for
This is a homework assignment, I have to write a program that reads a
My class assignment is to write a program that has the user input a
I have two questions about this homework assignment: Write an assembly language program in
For this assignment I had to create my own string class. I initially wrote
I am making this assignment in my program and I am getting the warning
OK, I have this assignment and I have to prompt the user for data
I was given an assignment to write a program to print only even numbers

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.