can't find method java - class

So I run the following program and my cmd prompt gives me an error saying that the getDescriptions() method is not found in the DataElements class. I'm sure there's a simple solution but I'm just stuck. Here's the DataElements class:
import java.io.*;
public class DataElements
{
File file;
private int columns;
private int row;
private int length;
private String name;
private String type;
private int position;
private String[] descriptions;
public File getFile(){
return file;
}
public void setFile(File f){
file = f;
}
public int getColumns(){
return columns;
}
public void setColumns(int c){
columns = c;
}
public int getRow(){
return row;
}
public void setRow(int r){
row = r;
}
public int getLength(){
return length;
}
public void setLength(int l){
length = l;
}
public String getName(){
return name;
}
public void setName(String n){
name = n;
}
public String getType(){
return type;
}
public void setType(String t){
type = t;
}
public int getPosition(){
return position;
}
public void setPosition(int p){
position = p;
}
public String[] getDescriptions(){
return description;
}
public void setDescriptions(String[] d){
description = d;
}
}
And here's the main method. If you need the CMSReader class let me know, but the problem seems to be stuck in these two classes
import java.util.Scanner;
import java.io.*;
public class Project2{
public static void main(String[] args) throws FileNotFoundException{
Scanner keyboard = new Scanner(System.in);
boolean fileParsed = false;
String inFile;
String outFile;
if(args.length != 1){
System.out.println("Error. Enter one argument: the file that needs to be parsed.");
System.exit(0);
}
Scanner scan = new Scanner(new File(args[0]));
DataElements storage = new DataElements();
CMSReader reader = new CMSReader(scan,storage);
reader.scanTopData();
System.out.println("Input File - " + storage.getName());
System.out.println("Output File - ");//*************Look at this*********************
System.out.println("Number of Variables - " + storage.getColumns());
System.out.println("Number of Records - " + storage.getRow());
System.out.println("Record Length - " + storage.getLength());
System.out.println("Variable information:");
reader.scanVariableData();
String[] variableData = storage.getDescriptions();
for(int i = 0; i < variableData.length ; i++){
System.out.println(variableData[i]);
}
}
}
I appreciate any help. Like I said, I'm sure it's something dumb but I've been looking at this for too long.

The variable description is not declared in your DataElements class, which is why DataElements file cannot compile, and my guess is that you have an older compiled version (.class file) of DataElements which does not contain that method.
Recommendation:
Start working with a good IDE (IntelliJ is my personal favorite, but Eclipse and Netbeans are also good options). A good IDE, on top of all other goodies it provides, will highlight such issues in a way you won't miss.

Related

Why does my ArrayList of objects print out one variable stored inside the object, but not the other

HandDrawn class extends the super-class Car, as you can see. The problem is it doesn't print out the the String name when i try to print the ArrayList that stores the objects. Btw, the ArrayList stores objects of the class Car.
Calm down, if i did something that offends your little feelings with this question, dont down vote... tell me what's up so i know in the future.
public class HandDrawn extends Card {
private boolean niceDrawing;
public HandDrawn(String name, boolean niceDrawing) {
super(name);
this.niceDrawing = niceDrawing;
}
#Override
public String toString() {
return "HandDrawn{" +
"niceDrawing=" + niceDrawing +
'}';
}
public void setNiceDrawing() {
this.niceDrawing = niceDrawing;
}
public boolean getNiceDrawing(boolean niceDrawing) {
return this.niceDrawing;
}
}
public class Main {
static ArrayList<Card> cards = new ArrayList<>();
public static void main(String[] args) {
cards.add(new HandDrawn("Anna", true));
cards.add(new HandDrawn("Kalle", false));
Main myApp = new Main();
myApp.cardList(cards);
}
public void cardList(ArrayList<Card> e) {
for (int i = 0; i <e.size(); i++) {
System.out.println(e.get(i));
}
}
}
This is the Main class and the HandDrawn class
Again, I don't know what is in your super class "Card" but maybe this will help:
In your HandDrawn.java file:
public class HandDrawn extends Card {
private boolean niceDrawing;
public HandDrawn(String name, boolean niceDrawing) {
super(name);
this.niceDrawing = niceDrawing;
}
#Override
public String toString() {
return "HandDrawn{" + "niceDrawing=" + this.niceDrawing + "}";
}
// Notice I have modified the parameters of your getters
// and setters because it looked as if you had swapped them:
public void setNiceDrawing(boolean niceDrawing) {
this.niceDrawing = niceDrawing;
}
public boolean getNiceDrawing() {
return this.niceDrawing;
}
}
In your Main.java file:
public class Main {
static ArrayList<Card> cards = new ArrayList();
public static void main(String[] args) {
// This is the entry point of your program... execution begins here.
cards.add(new HandDrawn("Anna", true));
cards.add(new HandDrawn("Kalle", false));
cardList(cards);
}
// The following two lines are out of place and should not be in class definition:
// Main myApp = new Main();
// myApp.cardList(cards);
// I added static keyword so that it could be used in call above
public static void cardList(ArrayList<Card> e) {
for (int i = 0; i < e.size(); i++) {
System.out.println(e.get(i));
}
}
If you are wanting to use the "Main" class as a piece in another part of your code then probably that means that execution starts somewhere else and never hits the "main" method. If that is the case you will need to call it explicitly (example: Main.main(null);) or reorganize your code to do it in a constructor for the "Main" class. Then your Main myApp = new Main(); and myApp.cardList(cards); lines would make sense in that context and the cardList method would need to be a non-static member method as you had originally written.
EDIT:
I think I misunderstood your formatting... Your code should work as follows:
In your Main.java file:
public class Main {
static ArrayList<Card> cards = new ArrayList();
public static void main(String[] args) {
// This is the entry point of your program... execution begins here.
cards.add(new HandDrawn("Anna", true));
cards.add(new HandDrawn("Kalle", false));
Main myApp = new Main();
myApp.cardList(cards);
}
public void cardList(ArrayList<Card> e) {
for (int i = 0; i < e.size(); i++) {
System.out.println(e.get(i));
}
}
What were you expecting to see? Again I cannot test your code because I don't have "Card" class but when I use the "HandDrawn" class in place of "Card" (without extending "Card") it works as I would expect and prints:
HandDrawn{niceDrawing=true}
HandDrawn{niceDrawing=false}

Trying to read values returned on jsp form submission in springboot project by setters and use the combination to call another java class

So, I have values in getter setter variables when I click on form submit but now want to have those values in variables and check combination of them to run code from another java class
I have tried using parametrized constructor or may be having a common setter but that did not help.
package com.grt.dto;
import java.util.Set;
public class WDPayrollRecon {
public Set<String> dataType;
public String planCountry;
public String payPeriod;
public String currentPeriod;
public String lastPayPeriod;
Set<String> test;
public Set<String> getdataType() {
return dataType;
}
public void setdataType(Set<String> dataType) {
this.dataType = dataType;
System.out.println("this is dataType" +dataType);
test = dataType;
}
public String getPlanCountry() {
return planCountry;
}
public void setPlanCountry(String planCountry) {
this.planCountry = planCountry;
}
public String getPayPeriod() {
return payPeriod;
}
public void setPayPeriod(String payPeriod) {
this.payPeriod = payPeriod;
}
public String getCurrentPeriod() {
return currentPeriod;
}
public void setCurrentPeriod(String currentPeriod) {
this.currentPeriod = currentPeriod;
}
public String getlastPayPeriod() {
return lastPayPeriod;
}
public void setlastPayPeriod(String lastPayPeriod) {
this.lastPayPeriod = lastPayPeriod;
}
public WDPayrollRecon()
{
}
public WDPayrollRecon(Set<String> dataType,String planCountry,String payPeriod,String currentPeriod,String lastPayPeriod)
{
this.dataType = dataType;
this.planCountry = planCountry;
this.payPeriod = payPeriod;
this.currentPeriod = currentPeriod;
this.lastPayPeriod = lastPayPeriod;
if(dataType.contains("GTLI")& planCountry.equals("USA")){
System.out.println("This is test");
}
else{
System.out.println("This is not test");
}
}
}

parsing issue in apache beam (beamSql)

I have a below code in which I'm reading a file in string format, then converting it into class format then converting it to BeamRecord and at the end converting back it to string format and writing the output in google storage.
DataflowPipelineOptions options = PipelineOptionsFactory.as(DataflowPipelineOptions.class);
options.setProject("beta-194409");
options.setStagingLocation("gs://clrtegbucket/staging");
options.setRunner(DataflowRunner.class);
DataflowRunner.fromOptions(options);
Pipeline p = Pipeline.create(options);
PCollection<String> weekly = p.apply(TextIO.read().from("gs://gcp/input/WeeklyDueto.csv"));
PCollection<ClassWeeklyDueto> pojos = weekly.apply(ParDo.of(new DoFn<String, ClassWeeklyDueto>() { // converting String into class
// typ
private static final long serialVersionUID = 1L;
#ProcessElement
public void processElement(ProcessContext c) {
String[] strArr = c.element().split(",");
ClassWeeklyDueto clr = new ClassWeeklyDueto();
clr.setCatLib(strArr[1]);
clr.setCausalValue(strArr[7]);
clr.setDuetoValue(strArr[5]);
clr.setModelIteration(strArr[8]);
clr.setOutlet(strArr[0]);
clr.setPrimaryCausalKey(strArr[6]);
clr.setProdKey(strArr[2]);
clr.setPublished(strArr[9]);
clr.setSalesComponent(strArr[4]);
clr.setWeek(strArr[3]);
global_Weekly.add(clr);
c.output(clr);
}
}));
BeamRecordSqlType appType = BeamRecordSqlType.create(
Arrays.asList("Outlet", "CatLib", "ProdKey", "Week", "SalesComponent", "DuetoValue","PrimaryCausalKey", "CausalValue", "ModelIteration", "Published"),
Arrays.asList(Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.FLOAT, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR));
PCollection<BeamRecord> apps = pojos.apply(ParDo.of(new DoFn<ClassWeeklyDueto, BeamRecord>() {
private static final long serialVersionUID = 1L;
#ProcessElement
public void processElement(ProcessContext c) {
BeamRecord br = new BeamRecord(appType, {
BeamRecord br = new BeamRecord(appType, c.element().Outlet, c.element().CatLib, c.element().ProdKey,
c.element().Week, c.element().SalesComponent, c.element().DuetoValue,
c.element().PrimaryCausalKey, c.element().CausalValue, c.element().ModelIteration,
c.element().Published);
c.output(br); }
})).setCoder(appType.getRecordCoder());
PCollection<String> gs_output_final = apps.apply(ParDo.of(new DoFn<BeamRecord, String>() {
private static final long serialVersionUID = 1L;
#ProcessElement
public void processElement(ProcessContext c) {
c.output(c.element().toString());
System.out.println(c.element().toString());
}
}));
gs_output_final.apply(TextIO.write().to("gs://gcp/output/Q"));
I have created class ClassWeeklyDueto below :
package com.pojo;
import java.io.Serializable;
public class ClassWeeklyDueto implements Serializable {
private static final long serialVersionUID = 1L;
public String Outlet;
public String CatLib;
public String ProdKey;
public String Week;
public String SalesComponent;
public float DuetoValue;
public String PrimaryCausalKey;
public String CausalValue;
public String ModelIteration;
public String Published;
public String getOutlet() {
return Outlet;
}
public void setOutlet(String outlet) {
Outlet = outlet;
}
public String getCatLib() {
return CatLib;
}
public void setCatLib(String catLib) {
CatLib = catLib;
}
public String getProdKey() {
return ProdKey;
}
public void setProdKey(String prodKey) {
ProdKey = prodKey;
}
public String getWeek() {
return Week;
}
public void setWeek(String week) {
Week = week;
}
public String getSalesComponent() {
return SalesComponent;
}
public void setSalesComponent(String salesComponent) {
SalesComponent = salesComponent;
}
public float getDuetoValue() {
return DuetoValue;
}
public void setDuetoValue(float duetoValue) {
DuetoValue = duetoValue;
}
public String getPrimaryCausalKey() {
return PrimaryCausalKey;
}
public void setPrimaryCausalKey(String primaryCausalKey) {
PrimaryCausalKey = primaryCausalKey;
}
public String getCausalValue() {
return CausalValue;
}
public void setCausalValue(String causalValue) {
CausalValue = causalValue;
}
public String getModelIteration() {
return ModelIteration;
}
public void setModelIteration(String modelIteration) {
ModelIteration = modelIteration;
}
public String getPublished() {
return Published;
}
public void setPublished(String published) {
Published = published;
}
public float setDuetoValue(String string) {
// TODO Auto-generated method stub
float f = Float.valueOf(string.trim()).floatValue();
return f;
}
}
The DueToValue field is declared float type, Only the field declared as varchar is getting parsed rest none of the datatypes are getting parsed.
So how shall I parse field declared as Int or float or even Date ?
When you manually split a string line from CSV, you get an array of strings. Then you have to manually parse the values from strings. Java doesn't handle it automatically.
In your case to handle floats you need to change clr.setDueToValue(strArr[5]) to clr.setDueToValue(Float.parseFloat(strArr[5])), see the doc.
Similarly you can use Integer.parseInt() to parse integers.
For parsing dates you will likely need to use a SimpleDateFormat.

java.util.UnknownFormatConversionException: Conversion = 'N'

This is my code:
package com.example.java;
public class PackageDataTest {
public static void main(String[] args) {
PackageData data1 = new PackageData("John Davids");
System.out.printf("%Number of object created until now is : %d", PackageData.count);
}
}
class PackageData {
static int count = 0;
String name;
public PackageData(){
this.name = "";
++count;
}
public PackageData(String name) {
this.name = name;
++count;
}
public int getCount() { return count; }
}
I keep receiving the following error:
"Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'N'"
%N is not a valid conversion Format and %n is for the line break.
so if you replace the code as below:
System.out.printf("%nNumber of object created until now is : %d", PackageData.count);
output will be on new line:
Number of object created until now is : 1
check this discussion: What's up with Java's "%n" in printf?

Is there something wrong with my eclipse calculator?

public class TaxReturn {
private double rate1= 0.10;
private double rate2=0.25;
private double single_limit = 32000;
private double married_limit = 64000;
private double income;
private int status;
public static int married=2;
public static int single=1;
public TaxReturn(double inc, int stat){
double income = inc;
int status=stat;
}
public double getTaxi(){
double tax1=0;
double tax2=0;
if(status==single){
if(income<=single_limit)
tax1=rate1*income;
else{
tax1=rate1*single_limit;
tax2=rate2*(income-single_limit);
}
}
else{
if(income<=married_limit)
tax1=rate1*income;
else
tax1=rate1*married_limit;
tax2=rate2*(income-married_limit);
}
return tax1+tax2;
}
}
import java.util.Scanner;
public class TaxCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("eneter income; avoid commas");
double income = sc.nextDouble();
System.out.println("are you married; type Y or N");
String status=sc.next();
int statuss;
if (status.equalsIgnoreCase("y"))
statuss=TaxReturn.married;
else
statuss=TaxReturn.single;
TaxReturn tr = new TaxReturn(income, statuss);
System.out.println("your tax is: " + tr.getTaxi());
}
}
I keep getting-16000 for the answer.I dont know if my code is wrong or something wrong with the software. This code was copied from the book. Iva had this problem with other codes too. Any help would be appreciated. Thanks
Your constructor is not correct.
Rewrite it as follow
public TaxReturn(double inc, int stat){
income = inc;
status = stat;
}
By declaring type on income and status variables, you made them local to the constructor.