Trying to instantiate objects within for loop and failing

后端 未结 4 549
暗喜
暗喜 2021-01-29 08:40

Basically I am trying to create a new class as long as the continue variable equals \"Y\". The problem I am having is

DigitalMain.java:18: not a statement
    D         


        
相关标签:
4条回答
  • 2021-01-29 09:01

    The issue is most likely the name of your array. The word class is a keyword in the Java language and hence cannot be used to name variables. You can also use ArrayLists like so:

    List<DigitalPhoto> photoes = new ArrayList<DigitalPhoto>(); 
    do
        {
        DigitalPhoto photo = new DigitalPhoto();
        heightString = JOptionPane.showInputDialog('Please enter height');
        photo .setHeight = double.parseDouble(heightString);
        heightString = JOptionPane.showInputDialog('Please enter width');
        photo .setWidth = double.parseDouble(widthtString);
        photos.add(photo)
        continueQuestion = JOptionPane.showInputDialog('Height: ' + class[i].getHeight + '\n\lWidth: ' + class[i].getWidth + '\n\l Resolution: ' + class[i].getResolution + '\n\lCompression Ratio: ' + class[i].getCompression + '\n\lRequired Storage: ' + class[i].calcStorage() + '\n\lPrice of Scanned Photo: ' + class[i].getCost() + 'Please enter "Y" to try again or anything but "Y" to accept values.')
        } while {cont.equals("Y")};
    
    0 讨论(0)
  • 2021-01-29 09:04

    class is a keyword - you can't use it as a variable name.

    Additionally, you have an odd construct here:

    for(int i=0; cont.equals("Y") ; i++)
    {
        ...
    } while {continue.equalsIgnoreCase(Y)};
    

    There's no such thing as a "for/while" loop - there's a normal for loop, a while loop, and a do/while loop.

    So you've actually got a for loop followed by an invalid while loop here. It has no condition.

    You need to work out which you want. (Possibly a for loop containing a do/while loop, although I'd extract the inner loop into a separate method. In general your code would greatly benefit from being broken out into multiple methods.

    You do something similar later, although this time with do/while:

    do
    {
        ...
    } while {bitps > 0 && bitps < 99999999};
    

    The condition of a while loop goes in round brackets, not braces:

    do
    {
        ...
    } while (bitps > 0 && bitps < 99999999);
    

    Basically, you should read up on the syntax options available for loops.

    0 讨论(0)
  • 2021-01-29 09:06

    there is some rules for naming of variables Variables

     You cannot use any of the following(in list of keywords) as identifiers in your
     programs.The keywords const and goto are reserved, even though they are not 
     currently used. true, false, and null might seem like keywords, but they 
     are actually literals;      you cannot use them as identifiers in your programs.
    

    List of keywords

    for(int i=0; cont.equals("Y") ; i++)
     {
       class[i] = new DigitalPhoto();
       heightString = JOptionPane.showInputDialog('Please enter height');
       class[i].setHeight = double.parseDouble(heightString);
       heightString = JOptionPane.showInputDialog('Please enter width');
       class[i].setWidth = double.parseDouble(widthtString);
       continueQuestion = JOptionPane.showInputDialog('Height: ' + 
       class[i].getHeight + '\n\lWidth: ' + class[i].getWidth + '\n\l Resolution: ' +
       class[i].getResolution + '\n\lCompression Ratio: ' +  class[i].getCompression +
      '\n\lRequired Storage: ' + class[i].calcStorage() + '\n\lPrice of Scanned Photo: ' + 
        class[i].getCost() + 'Please enter "Y" to 
       try again or anything but "Y" to accept values.')
    } while {continue.equalsIgnoreCase(Y)};
    

    here i dont know any for-while loop..just check some basics..
    Loops in java

    0 讨论(0)
  • 2021-01-29 09:08

    What kind of loop you are using.

    for(...)
    {
    ...
    }while();
    

    There is no for-while loop.

    And also your for loop condition never going to become false. Set a proper condition to your for loop.
    Also there is syntax error in your for-while loop. ie. last statement

    continueQuestion = JOptionPane.showInputDialog('Height: ' + class[i].getHeight + '\n\lWidth: ' + class[i].getWidth + '\n\l Resolution: ' + class[i].getResolution + '\n\lCompression Ratio: ' + class[i].getCompression + '\n\lRequired Storage: ' + class[i].calcStorage() + '\n\lPrice of Scanned Photo: ' + class[i].getCost() + 'Please enter "Y" to try again or anything but "Y" to accept values.') // you miss the ;     
    
    0 讨论(0)
提交回复
热议问题