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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:29:15+00:00 2026-06-01T20:29:15+00:00

int n = sc.nextInt(); char[][] original = new char[n][n]; char [][] result = new

  • 0
int n = sc.nextInt();
char[][] original = new char[n][n];
char [][] result = new char[n][n];
String line;

for (int i = 0; i < n; i++) {
  line = sc.nextLine();
  System.out.println(line);
  for (int u = 0; u < n; u++) {
    original[i][u] = line.charAt(u);
  }
}

for (int i = 0; i < n; i++) {
  line = sc.nextLine();
  for (int u = 0; u < n; u++) {
    result[i][u] = line.charAt(u);
  }
}

I have a file that looks like this:

2
ha
ah
lo
ol

I have an integer N on firs line, and then two N*N matrixes of characters.

I am trying to read them to two arrays of arrays of chars, but I get this error:

Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:695)
at transform.main(transform.java:25)

Line 25 is this:

original[i][u] = line.charAt(u);

I just don’t get it, I think I’m doing everything just fine. Any ideas? Thank 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-01T20:29:16+00:00Added an answer on June 1, 2026 at 8:29 pm

    I suppose your sc is a scanner instance? The method nextInt() does read the next number from your file but does not process the following newline. Thus, your first call to nextLine() will return just an empty string (i.e. all characters until the newline which was not processed yet). You will see that you println(...) call does print an empty line. You may insert a call to newLine() before your loop to fix this behavior.

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

Sidebar

Related Questions

int main(void) { char tmp, arr[100]; int i, k; printf(Enter a string: ); scanf_s(%s,
public static void main (String args[]) { Scanner myinput=new Scanner(System.in) ; //Arrary length comes
here is my code Scanner in = new Scanner(new File(words.txt)); int choice = 0;
int main(void) { std::string foo(foo); } My understanding is that the above code uses
I'm creating a function where returns multiple of 10? public static int NextInt(this Random
I'm quite new to Scala programming language, and was trying something out stucked in
So I have a method that reads a file and assigns classes to elements
static void LasVegas(int []tablero, int f, int ultimaReina){ HashSet<Integer> enterosUsados = new HashSet<Integer>(); if
i'm new to c++ and have been trying to implement the bellman ford algorithm,
Is there a Ruby equivalent of the Java Scanner? If I have a string

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.