user3177001
user3177001

Reputation: 45

Converting KG to Pounds program

import javax.swing.JOptionPane;

public class NewClass {


int i = 1;

public static void main(String[] args)
{


    String output = "";                       
    System.out.println("kilograms | pounds");
    System.out.println("--------+-----------");
    for ( i = 1; i <= 199; i ++)
    {            
       // Every table line in between
    System.out.printLn(“| “+i+” | “+i*2.2+” |”);
        }
    System.out.println(output);
    } //end method
} // end class

what's wrong here, why can't I run it??trying to display a list of KG to pound from 1 to 199

Upvotes: 0

Views: 9186

Answers (3)

ChopChop
ChopChop

Reputation: 140

You did some typos. This should compile:

//Removed import because you aren't using it

public class NewClass {

  public static void main(String[] args)
  {                 
    System.out.println("kilograms | pounds");
    System.out.println("--------+-----------");
    for (int i = 1; i <= 199; i ++) 
    // define i at this point because it's only used in this scope
    {            
      System.out.println("| " + i + " | " + (2.2 * i) + " |");
      // Use normal "
      // you misspelled println
      // put parenthesis around calculations
    }
    System.out.println("");
  } //end method
} // end class

You should consider using an IDE like Eclipse. It highlights errors like the ones above.

Upvotes: 1

splrs
splrs

Reputation: 2432

Change your for-loop so you're not using the class member i (which you should delete):

for ( int i = 1; i <= 199; i ++) //could be i < 200 though, if I was being picky...

Change this line so you have println and not printLn, and use normal double quotes:

System.out.println("| " + i + " | " + i * 2.2 + " |");

Upvotes: 0

evanchooly
evanchooly

Reputation: 6233

You're trying to do math in the middle of concatenating strings. Wrap i*2.2 in parentheses and you should be fine. Right now, you're appending the value of i to the String and then trying to multiply that String by 2.2.

Upvotes: 0

Related Questions