Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Jr Winston

Greenhorn
+ Follow
since Jul 22, 2011
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jr Winston

thanks! i understand it now.
8 years ago
Hi all. I have to write a java program that allows a user to enter an octal digit and output the binary equivalent for as many numbers as the person wishes using a direct access method.

The error is that even though it compiles, it loops the binary equivalent. The code goes as follows:


import java.util.Scanner; // initialize scanner class for input
public class Arrays
{
public static void main(String[] args)
{
int num = 0;
String OctalNum;
// declare array to hold 7 octal numbers and convert to binary numbers
int i;
int j;
int n;

char c; //to hold each number of input octal

char repeat; //to hold yes or no

//for numbers 1 to 7, make a binary equivalent

String binaryArray[]={"000", "001", "010", "011", "100", "101", "110", "111"};


do
{


Scanner input = new Scanner(System.in);
System.out.println("Octal to Binary Conversion");

System.out.println("Enter the Octal Number: ");
OctalNum = input.nextLine();


// make a loop to read as many numbers as the input octal has
for (i=0; i < OctalNum.length(); i++)
{
//read string by character then convert to an integer

c = OctalNum.charAt(0);

for (j=1; j < c; j++)
{
//convert character to integer (ex: "1" to 1)
n = Character.getNumericValue(c);
if(n <8)
{
binaryArray[0] = "000";
binaryArray[1] = "001";
binaryArray[2] = "010";
binaryArray[3] = "011";
binaryArray[4] = "100";
binaryArray[5] = "101";
binaryArray[7] = "111";

System.out.println(OctalNum+ " converted to binary is "+ binaryArray[n]);

// System.out.println();

}//end if
else if(n>7)
System.out.println( "Not a valid octal number!");

}

}

// prompt for input more OctalNum
System.out.print("Do you want to enter another Octal number? ");

System.out.println("Y for yes or N for No: ");
System.out.println();

String str= input.next(); //Read next char
repeat= str.charAt(0); //Get the first char

}
while(repeat=='Y' || repeat=='y');

}
}
8 years ago
I am not sure what you mean. Can you give me an example? Thanks. Also, I ran the code and I compiled and ran it. When I enter a customer and I press return, it gave me an error: InputMismatchException.

I took the static keywords out because I didn't study that yet (that's the next chapter so I can't use that yet). I believe that the for loop is used to allow the user to enter as many customers as they desire, and I have to code it using the counting method. That is where I am stuck.
9 years ago
import java.text.DecimalFormat; // for decimal formatted output
import java.util.Scanner; // package needed for GUI

public class ElectricBill4
{
public static void main(String[] args)
{
DecimalFormat num = new DecimalFormat("$,###.00");

// variables
String Company; // to hold Company input
int AccountNumber; // to hold AccountNumber
String Customer;

double kwhUsed;
double OldMeterReading; // string for inputting OldMeterReading
double NewMeterReading; // string for inputting NewMeterReading

double AmountDue;
double charge = 0.0;

Scanner inputDevice = new Scanner(System.in); // initialize scanner


for ( Customer = 1; Customer <= 10; Customer++)

System.out.print("Enter your Customer: "); // Customer name

Customer = inputDevice.nextLine();

Customer = 1;
while(Customer < 11)
{
System.out.print(Customer);
++Customer;
}

System.out.println("State your company name: "); // Company name
Company = inputDevice.nextLine();

System.out.println("Enter account number: "); // AccountNumber
AccountNumber = inputDevice.nextInt();

System.out.println("What is your old meter reading? "); // OldMeterReading);
OldMeterReading = inputDevice.nextDouble();

System.out.println("What is your new meter reading? "); // NewMeterReading);
NewMeterReading = inputDevice.nextDouble();

// calculate the current electricity usage

kwhUsed = (NewMeterReading - OldMeterReading);

// computations

if(kwhUsed <= 300)
{
charge = 5.00;
}
else if(kwhUsed <= 1000)
{
charge = 5.00 +(0.03*(kwhUsed - 300));
}
else
{
charge = 35.00+(0.02*(kwhUsed - 1000));
}

AmountDue = charge; // calculate the bill


System.out.println("Company name is: " + Company); // company name

System.out.println("Customer name is: " + Customer); // customer

System.out.println("Account number is: " + AccountNumber); // account number

System.out.println("Old Meter Reading is: " + OldMeterReading); // old meter rating

System.out.println("New Meter Reading is: " + NewMeterReading); // new meter rating

System.out.println("Kilowatts per hour used is: " + kwhUsed); // kilowatts per hour

System.out.println("Amount Due is: " + num.format(AmountDue)); // amount due

System.out.println(); //Prints a blank line.

// input as many customers as you wish.

inputDevice.nextLine();

System.out.print("If you want to exit this program, please enter the word 'stop' in now."+ "\n"

+" Otherwise, enter your name: ");

Customer = inputDevice.nextLine(); // loops back to the Customer.
}

System.out.println("Thank you. Goodbye.");
}

}


I am using jGrasp as an editor.
9 years ago
Hi. I am having a bit of problem with this program. You have to modify a bill program. The bill at the end should print out the bill including customer(s), account number, old and new meter readings, kilowatts per hour, and the amount due. I'm having problems putting a for loop to make the problem enter as many customers as the user wants to have. In addition, the user will first input the number of customers.

The code's errors:

ElectricBill4.java:106: <identifier> expected
System.out.println("Thank you. Goodbye.");
^
ElectricBill4.java:106: illegal start of type
System.out.println("Thank you. Goodbye.");
^
ElectricBill4.java:109: class, interface, or enum expected
}
^
3 errors


The code:



9 years ago