Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need an identifier for my for loop when the variables are already declared? [closed]

Can someone tell me what's wrong with my for loop? I have been spending a lot of time trial and error and researching on the internet but I still can't find a solution. It keeps on saying that an identifier expected cannot find symbol. Please help thanks!

package imageviewer;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ImageViewer extends JPanel implements ActionListener {

    JPanel bottomPanel;
    JLabel imageLabel;
    JButton previousBtn;
    JButton nxtBtn;

    ImageViewer()
    {
        imageLabel = new JLabel("Image Display");
        imageLabel.setBackground(Color.WHITE);
        imageLabel.setOpaque(true);
        add(imageLabel);

        previousBtn = new JButton("Previous");
        previousBtn.addActionListener(this);
        add(previousBtn);

        nxtBtn = new JButton("Next");
        nxtBtn.addActionListener(this);
        add(nxtBtn);
    }

    String userInput = JOptionPane.showInputDialog("Please input name of first image file");
    Scanner myScanner = new Scanner(userInput);
    String fileDetail = myScanner.next();
    String[] arr = fileDetail.split("\\.");

    String fileName = arr[0].replaceAll("[0-9.]", "");
    String fileNumber = arr[0].replaceAll("[^0-9]", "");
    String fileFormat = arr[1];

    int num = Integer.parseInt(fileNumber);

    String totalImage = JOptionPane.showInputDialog("Please enter the number of images in total");
    Scanner secondScanner = new Scanner(totalImage);
    int numberInput = secondScanner.nextInt();

    ImageIcon imageGraphic = new ImageIcon(fileName + fileNumber + "." + fileFormat);

    Vector <String> imageDetail = new Vector <String>();
    int total = (num + numberInput);

    for(int i = num; i < total; i++)
    {
    }


public void actionPerformed(ActionEvent e)
    {
    }

}
like image 219
Scorpiorian83 Avatar asked Feb 13 '23 12:02

Scorpiorian83


2 Answers

What's wrong with it is that it is declared out in open space, outside of any method, constructor, static block, and such, and you simply can't have a for loop or any similar control structure in this region. Solution: put it where it belongs, be it a method or constructor, your choice.

In fact, a lot of your lines look like they don't belong out in "open country", including the line that calls the JOptionPane, and the lines that use the user input below it, and I wonder of some of these lines belong inside of this class. The class should concentrate only on one single responsibility -- here viewing an image, and that's it. You should re-think your program design a bit.

For instance, it appears that you want this class to display a collection of images, and so that is what it should do. If it were my class, I'd give it methods that allow other classes to give it a List of ImageIcons, and then all this class to scroll through the images on button push. Any code that asks the user where to start looking for images, should likely be elsewhere and not in this class.

like image 103
Hovercraft Full Of Eels Avatar answered Feb 15 '23 01:02

Hovercraft Full Of Eels


Your loop is currently places where parameters go. It needs to be in an executable block of code ie. a constructor or method of some kind.

like image 25
JustWannaFly Avatar answered Feb 15 '23 02:02

JustWannaFly