public static Vehicle[] fillArray(inputString) throws exception {
while(readRecords.ready()) {
Vehicle newVehicle;
checkType = readRecords.readLine();
if(checkType.equals("vehicle")) {
String ownersName = readRecords.readLine();
String address = readRecords.readLine();
String phone = readRecords.readLine();
String email = readRecords.readline();
newVehicle = new Vehicle(ownersName,address,phone,email);
list.add(newVehicle);
}
I am getting an <identifier> expected error. Signals at the inputString within the parenthesis.
any suggestions?
You need to tell what type the parameter is:
public static Vehicle[] fillArray(String inputString) throws Exception
Otherwise the compiler doesn't know if inputString is a string, an int, or some other object. It won't guess by the name.
You should specify a type for your parameter. Try this :
public static Vehicle[] fillArray(String inputString) throws exception
Related
I am shaky with parsing and handling text files. How do I convert my method signatures with enums to strings so I can read from a text file and parse effectively without wasting CPU resources? I currently have method signatures like this
public void createReservation(VehicleType v, String cName, long phoneNumber, String sDate, String eDate) throws Exception //right here
{
//trouble with representing VehicleType v in my Reservation text file
}
Reservation.txt file looks like this matching the signature Above
"COMPACT", "John Wick", 312 900 6001, "2019-02-09", "2019-02-14"
"SUV", "Harvey Dent", 302 600 2001, "2019-02-11", "2019-02-15"
In my main class I have a parseVehicleLine method like this..
private void parseReservationLine(String str){
Scanner sc = new Scanner(str);
sc.useDelimiter(",");
while(sc.hasNext()){
vehicleType = sc.next();//there is an error here because it is still an enum of Vehicle class
String cName = sc.next();
long phoneNumber = sc.nextLong();
String Date = sc.next();
String eDate = sc.next();
}
sc.close();
}
public abstract class Vehicle {
public enum VehicleType
{
ECONOMY(18.00), PREMIUM(22.50), SUV(25.50);
private double vehicleDailyCost;
private VehicleType(double vehicleDailyCost)
{
this.vehicleDailyCost = vehicleDailyCost;
}
public double getVehicleDailyCost()
{
return vehicleDailyCost;
}
}
Can someone explain how I could properly convert the VehicleType enum to a String without issues? Thanks!
You are reading the text file as String, so I think you need to convert String to VehicleType, not vice-versa as you mentioned.
You should be able to get enum from String using valueOf():
vehicleType = VehicleType.valueOf(sc.next());
I'll list my code at the bottom, but here's the deal.
I have a parser class with a process method. This method scrapes data from a web page. I want to take each line of the scrape, split the string into an array and add the values to an object, then add the object to an ArrayList. As the loop iterates, new values from each line of the scrape are added to an ArrayList via the object. The relevent method in this code is public ArrayList process()
I'm getting the following compile errors:
The constructor Flight(String) is undefined
Syntax error on token "[", Expression expected after this token
The method add(String) in the type ArrayList is not applicable for the arguments (Flight)
What am I doing wrong here, and how do if fix it?
public class HtmlParser {
public String url;
public String airline;
public static String lastFlight;
static ArrayList<String> capture = new ArrayList<String>();
public HtmlParser(Properties config) throws IOException{
url = config.getProperty("url");
airline = config.getProperty("airline");
print("Fetching.........%s" , url);
}
public ArrayList<String> process() throws IOException{
Document doc = Jsoup.connect(url).get();
Elements tableRow = doc.getElementsByTag("tr");
for(Element tr : tableRow){
if(tr.text().contains(airline)){
String delims = "[ ]+";
String singleRowArray[] = tr.text().split(delims);
Flight f = new Flight(singleRowArray[]);
capture.add(f);
}
}
return capture;
}
Here's my flight Class.
public class Flight {
public Flight(String singleRowArray[]) {
String origin = singleRowArray[0];
String airline1 = singleRowArray[1];
String airline2 = singleRowArray[2];
String flightNo = singleRowArray[3];
String date = singleRowArray[4];
String ArrTime = singleRowArray[5];
String status = singleRowArray[6];
}
}
"The constructor Flight(String) is undefined Syntax error on token "[", Expression expected after this token"
Try switching the brackets to the type, not the variable: String[] singleRowArray
"The method add(String) in the type ArrayList is not applicable for the arguments (Flight)"
You have a List<String> where you want to input an object of type Flight, so you should change the list type to List<Flight>.
static ArrayList<String> capture = new ArrayList<String>();
You can't add the Flight class object in capture list which can only contain element in String type.
Flight f = new Flight(singleRowArray[]);
capture.add(f); // Issue
-----------------^
I have a fixed length String record, i want to replace the string at a specific position with different string value.
String record ="ABCU0MARK 111111118 CFTT 130913 101100023424";
String extractAccountaccountNumber = record.substring(79, 87);
String newAccountNumber = "some value"
record = record.replaceFirst(extractAccountaccountNumber,newAccountNumber);
This may not work if there are duplicate values. Please advice
you just need to assign it to a new String variable, or to itself:
string = string.replace("to", "xyz");
or
String newString = string.replace("to", "xyz");
public class Run1 {
public static final int its = 4;
public static void main(String[] args) {
String record ="ABCU0MARK 111111118 CFTT 130913 101100023424";
String extractAccountaccountNumber = record.substring(46, 55);
System.out.println("extractAccountaccountNumber:"+extractAccountaccountNumber);
String newAccountNumber = "some value";
String result=record.replaceFirst(extractAccountaccountNumber,newAccountNumber);
System.out.println("result:"+result);
}
}
here is result:
extractAccountaccountNumber:FTT
result:ABCU0MARK 111111118 Csome value 130913 101100023424
I am new in this Java journey, at College they are asking me to
"Define five String variables in the main method called: shipmentNum, supplierName, revDate, revTime, employeeNum." And assign the following text:99, Costco, 12/15/2011, 10:25 AM, 33."
I have this so far, but is giving an error message: "the local variable shipmentNum is never read", I don't see why am I getting this error message.
package c1;
public class ShipmentApp {
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println("99");
System.out.println("Costco");
System.out.println("12/15/2011");
System.out.println("10:25 AM");
System.out.println("33");
}
}
What you are seeing is a compiler warning, not an error. This is basically Java trying to help you find flaws in your code by analyzing what you wrote and detecting common mistakes.
In this case, Java recognized that you assigned values to a bunch of variables, but after that never use those variables again.
You probably want to write out the values of the variables, not the assigned values again.
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum );
System.out.println(supplierName );
System.out.println(revDate );
System.out.println(revTime );
System.out.println(employeeNum );
}
Thats warning not error but try this.
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
}
or just try:
package c1;
public class ShipmentApp{
public static void main(String[] args){
System.out.println("99");
System.out.println("Costco");
System.out.println("12/15/2011");
System.out.println("10:25 AM");
System.out.println("33");
}
}
What you are receiving are warnings because you are not actually ever reading any of the variables you have declared. You could correct this by passing the variables to println instead of typing out the strings twice.
Being that these are only warnings, they should not affect the ability of the program to compile and/or execute. While you could conceivably ignore such warnings, it's usually wise to at least analyze what they are being caused by.
It's a warning that the java compiler tells you that you defined a variable but never used it.
The purpose of a variable, is that it stores information that will be used at some later point in your code. Java gives you a warning, because if you define a variable but never use it you've likely made a mistake. This is because variables that are never used are basically nonsense, and should never have been declared.
Try these print statements:
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
That's is not an Error,It is just a warning shown by the IDE or compiler. There is no issue in this code to compile and run.
You may want to do as follows
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
You are writing the text out, not the value of the variables. It isn't an error, just a warning. You should probably change your code to this:
package c1;
public class ShipmentApp{
public static void main(String[] args){
String shipmentNum = "99";
String supplierName = "Costco";
String revDate = "12/15/2011";
String revTime = "10:25 AM";
String employeeNum = "33";
System.out.println(shipmentNum);
System.out.println(supplierName);
System.out.println(revDate);
System.out.println(revTime);
System.out.println(employeeNum);
}
}
this message is just to notice you, you have some variables haven't been used. It is not a error. if you dont want to see this warning or you cannot stand it, you can add #supresswarnings annotation at the beginning of the class. or you just follow others' suggestion to use the variables you have created.
I try to call the method ss from my main method, but it throws the following exception
Exception in thread "main" java.lang.NullPointerException
at teste1.exp.ss(exp.java:16)
at teste1.Main.main(Main.java:64)
Java Result: 1
public class Main {
public static void main(String[] arguments) {
...................
private static String[] ff;
exp mega = new exp();
mega.ss(ff);
}
class exp {
public void ss (String gvanswer[]){
String answer[] = new String[3];
answer[0] = "pacific ";
answer[1] = "everest";
answer[2] = "amazon ";
if (gvnswer[0].equals("pacific"))
{System.out.println("eeeeeeeeeeeeee ");}
if (gvanswer[1].equals(answer[1])){System.out.println("l ");}
}
you call mega.ss(ff) but ff has never been initialited with somthing like:
ff = new String[1];
ff[0] = "foo";
You haven't populated the the array called ff which you are passing into the ss method.
You pass the ss method an uninitialized String[] array (ff) hence the NullPointerException.