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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:50:28+00:00 2026-06-12T04:50:28+00:00

My small program is supposed to read from a .txt file named map1.txt and

  • 0

My small program is supposed to read from a .txt file named map1.txt and store the chars in it in an Char[][] array. Depending on the chars in my map1.txt my Tile class draw red if char is ‘x’ or else draws green. (Instead the program shows what´s behind the panel instead of boxes in red and green. EDIT).

Note that this is my program...

public class MainClass extends JPanel {
    static Tile[][] tile = new Tile[SomeInts.amount][SomeInts.amount];
    static Map map = new Map();
    public MainClass () {
        this.setBackground(Color.white);
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for (int x = 0; x < SomeInts.amount; x++){
            for (int y = 0; y < SomeInts.amount; y++){
                tile[x][y].colorBody(g, x, y);
            }
        }
    }
    //Most important
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(500, 500);
        f.setLocation(100,100);
        f.setTitle("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainClass p = new MainClass ();
        f.add(p);
        f.setVisible(true);
            //load map
        map.loadMap();
        for (int x = 0; x < SomeInts.amount; x++){
            for (int y = 0; y < SomeInts.amount; y++){
                tile[x][y] = new Tile();
            }
        }
        for (int x = 0; x < SomeInts.amount; x++){
            for (int y = 0; y < SomeInts.amount; y++){
                tile[x][y].setType(map.charmap[x][y]);
            }
        }
    }
}

public class Tile {
    char type = 'a';
    public void setType(char c){
        type = c;
    }
    public void colorBody(Graphics g, int x, int y){
        if (type == 'x'){
            g.setColor(new Color(255, 0, 0));
            g.fillRect(x * 10, y * 10, 10, 10);
        }
        else{
            g.setColor(new Color(0, 255, 0));
            g.fillRect(x * 10, y * 10, 10, 10);
        }
    }   
}

public class Map {
    public char[][] charmap = new char[SomeInts.amount][SomeInts.amount];
    public void loadMap(){
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader("map1.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        String line = null;
        try {
            line = in.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (line != null){
            int y = 0;
            for (int x = 0; x < line.length(); x++){
                charmap[x][y] = line.charAt(x);
            }
            y++;
        }
    }

    //Not used but does work
    public void createMap() {
          for (int x = 0; x < SomeInts.amount; x++){
              for (int y = 0; y < SomeInts.amount; y++){
                  charmap[x][y] = 'x';
              }
          }
    }
}
  • 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-12T04:50:30+00:00Added an answer on June 12, 2026 at 4:50 am

    The code you are showing has a infinite loop, your thinking here is a bit off.
    The logic you have reads a line, and then it loops as long as that line is not null. Either the line was null form the beginning or it wasn’t, it will not change by itself. You need to read a new line once you processed the current one.

    You also need to move the reset of y to 0 out of the loop. Now you set y to 0, increase it by one and then set it back to 0 again.

    String line = null;
    int y = 0;
    try {
        while ((line = in.readLine())!= null) {
            for (int x = 0; x < line.length(); x++){
                charmap[x][y] = line.charAt(x);
            }
            y++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small program that's supposed to sample some value from a device
I have a C# program that is supposed to pull data from a small,
I'm writing a small program that's supposed to execute a command on a remote
I have a quick question. I am supposed to create a small multithreaded program
I have a small program that I am supposed to write that makes a
I am writing a small program that sends and receive multicast packets.I need to
Consider this small program: #include <stdio.h> #include <stdlib.h> // Change 60000 to 70000 and
I have written a small program just like hello the world , everything was
Below is a small program that I have written to count the number of
I have a small program <350ish lines of code on 5 forms>. It works

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.