Java - class expected error - string

I have the following code:
import javax.sqing.JOptionPane;
public class DebugTwo4
{
public static void main(String[] args)
{
string costString;
double cost;
final double TAX = 0.06;
costString = JOptionPane.showInputDialog("Enter price of item you are buying", "Purchases", JOptionPane.INFORMATION_MESSAGE);
cost = double.parsecost (coststring);
JOptionPane.showMessageDialog(null,"With " + TAX * 100 + "% tax, purchase is $" + (cost - cost * TAX));
}
}
I am getting the following errors:
DebugTwo4.java:11: error: class expected
cost = double.parsecost (coststring);
DebugTwo4.java:11: error: ';' expected
cost = double.parsecost (coststring);
Please help!

Not sure what parsecost is supposed to do since I don't see a definition for it anywhere, but from what I understand could you not change this line:
cost = double.parsecost (coststring);
To something like this:
cost = Double.parseDouble(coststring);

Related

How do I get the output of the function in Groovy

static def StartMonth() {
def date = new Date().format('MM')
def month = date.toInteger()
if (month > 6) {
month = month - 6
} else if (month <= 6) {
month = (month + 12) - 6
}
}
static void main(String[] args) {
StartMonth();
}
}
I have this and the output will be 4 (right now in October) and I want to retrieve that value so that I can have a new variable newDate = *startDate output* + '25/2019' but I do not know how to get the value back. (I am very new to this, I usually program in Javascript and even then I am still a beginner)
Thank you
In groovy you can return a value using the return value (as you can do in javascript). If there is no return a method returns the last value assigned (month in your case).
So you can either add return month in your StartMonth method. Or you can keep it as it is and just print it:
static void main(String[] args) {
println StartMonth()
}

Generate histogram from JavaRDD

I am trying to write code for converting data in Java RDD to a histogram so that I can bin the data in a certain way. For example, for the data I want to create a histogram of sizes such that I can find out which bin contains how many entries of a certain size range. I am able to get the value in different RDD's but I am not sure what I am missing here.
Is there an easier way to do this?
0 - 1 GB - 2 entries
1 - 5GB - 4 entries
and so on
EntryWithSize {
long size;
String entryId;
String groupId;
}
JavaRDD<EntryWithSize> entries = getEntries();
JavaRDD<HistoSize> histoSizeJavaRDD = entryJavaRDD.keyBy(EntryWithSize::getGroupId)
.combineByKey(
HistoSize::new,
(HistoSize h, EntryWithSize y) -> h.mergeWith(new HistoSize(y)),
HistoSize::mergeWith
).values();
#Data
#AllArgsConstructor
static class HistoSize implements Serializable {
int oneGB;
int fiveGB;
public HistoSize(EntryWithSize entry) {
addSize(entry);
}
private void addSize(EntryWithSize entry) {
long size = entry.getSize();
if (size <= ONE_GB) {
oneGB++;
} else {
fiveGB++;
}
}
public HistoSize mergeWith(HistoSize other) {
oneGB += other.oneGB;
fiveGB += other.fiveGB;
return this;
}
}
I was able to get it working by using a reduce on final pair rdd. My test data was wrong which was causing red herring in the output.
Function2<HistoSize, HistoSize, HistoSize> reduceSumFunc = (a, b) -> (new HistoSize(
a.oneGB + b.oneGB,
a.fiveGB + b.fiveGB,
));
HistoSize finalSize = histoSizeJavaRDD.reduce(reduceSumFunc);

groovy.lang.MissingMethodException String vs java.lang.String

I was doing a coding challenge that prints a given text in a zig-zag:
thisisazigzag:
t a g
h s z a
i i i z
s g
So I have my code (not sure if it's right or not yet, but that's not part of the question)
class Main {
public void zigzag(String text, int lines) {
String zigLines = [];
while(lines > 0){
String line = "";
increment = lines+(lines-2);
lines = lines + (" " * (lines-1));
for(int i=(lines-1); i<text.length(); i+=increment) {
line = line + text[i] + (" " * increment);
}
zigLines.add(0, line);
lines--;
}
for(line in zigLines){
println(line);
}
}
static void main(String[] args) {
zigzag("thisisazigzag", 4);
}
}
But when I run the script, I keep getting this error:
groovy.lang.MissingMethodException: No signature of method: static Main.zigzag()
is applicable for argument types: (String, Integer) values: [thisisazigzag, 4]
Possible solutions: zigzag(java.lang.String, int)
And I'm very confused as to the difference between java.lang.String and String and then Integer and int?
Any help or explanation of this would be great!
Thanks.
You should make your zigzag method static.
Your code wasn't working because without the static modifier zigzag was an instance method, meaning you would need an instance of your class Main to be able to call it. Here's an introductory tutorial explaining some of these concepts: docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

rounding up to the nearest penny in have

alright heres my assignment question
Create a Java program called DrivingCost that prompts the user to enter the distance to drive, the fuel efficiency of the car in miles per gallon, and the price per gallon, and then displays the cost of the trip:
I am supposed to get the result $81.40 but I keep getting $81.39
How do I round up that penny?
Here's my code
import java.util.Scanner;
public class DrivingCost
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the driving distance: ");
double distance = input.nextDouble();
System.out.print("Enter miles per gallon: ");
double mpg = input.nextDouble();
System.out.print("Enter price per gallon: ");
double ppg = input.nextDouble();
double cost = distance / mpg * ppg;
System.out.println("The cost of driving is $" + (int)(cost * 100) / 100.0);
}
}
Try...
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class DrivingCost
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the driving distance: ");
double distance = input.nextDouble();
System.out.print("Enter miles per gallon: ");
double mpg = input.nextDouble();
System.out.print("Enter price per gallon: ");
double ppg = input.nextDouble();
double cost = (distance / mpg) * ppg;
input.close();
NumberFormat df = DecimalFormat.getCurrencyInstance(Locale.US);
System.out.println("The cost of driving is " + df.format(cost));
}
}

How can i make the value the the user inputs appear next to thew print statement?

import java.util.Scanner;
public class nameP{
public static void main(String[] args){
Scanner user = new Scanner(System.in);
int value;
System.out.println("Type a number: ");
value = user.nextInt();
if (value % 2 == 0)
System.out.println("even");
else
System.out.println("odd");
}
}
Need help making the value given by the user appear right next to the print statement....and not in the bottom as it commonly happens. Help would be greatly appreciated.
Use System.out.print
instead of System.out.println
println prints a newline after the String you send it.
System.out.println("The number " + value + " is even");

Resources