What is wrong with my code? Using split with a string, and assigning the tokens to the fields in a class [...












-5















The string im passing is this "Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16"



Im passing the string, and assigning the fields in the class to them., Im not sure why im getting this error



  public class UberUser{
String name;
String pickUp;
String destination;
int rideTime;
int miles;
double earning;

public UberUser(String s){
String line = s;
s=s.replaceAll("\s","");

String tokens = s.split(",");
this.name = tokens[0];
this.pickUp = tokens[1];
this.destination = tokens[2];
this.rideTime = Integer.parseInt (tokens[3]);
this.miles = Integer.parseInt (tokens[4]);
this. earning = Double.parseDouble(tokens[5]);
}

public class UberTester{
public static void main (Stringargs)throws Exception{

BufferedReader in = new BufferedReader(new FileReader("uberLog.txt"));
String str;
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}

List<UberUser> users = new ArrayList<UberUser>();
for(String s :list){
users.add(new UberUser(s));
}
}
}


if anybody can help me i would really appreciate it thanks!



this is the error:



java.lang.ArrayIndexOutOfBoundsException: 1
at UberUser.<init>(UberUser.java:17)
at UberTester.main(UberTester.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)









share|improve this question















closed as off-topic by Andy Turner, gnat, shmosel, Pradeep Simha, Nicholas K Nov 16 '18 at 9:06


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Andy Turner, shmosel, Pradeep Simha, Nicholas K

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1





    The first problem is you haven't included the code in the question.

    – Peter Lawrey
    Nov 16 '18 at 8:31











  • Please edit your question to add the code and any errors, as well as a description of what you're trying to do, inputs etc. Images are not a good way to convey the necessary information.

    – Andy Turner
    Nov 16 '18 at 8:31






  • 1





    Im not sure why im getting this error... Which error?

    – shmosel
    Nov 16 '18 at 8:38






  • 1





    @Ricardo Your code seems to run fine. What is the issue?

    – Ankur Chrungoo
    Nov 16 '18 at 8:38








  • 1





    @PeterLawrey it was in the first version of the question.

    – Andy Turner
    Nov 16 '18 at 8:38
















-5















The string im passing is this "Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16"



Im passing the string, and assigning the fields in the class to them., Im not sure why im getting this error



  public class UberUser{
String name;
String pickUp;
String destination;
int rideTime;
int miles;
double earning;

public UberUser(String s){
String line = s;
s=s.replaceAll("\s","");

String tokens = s.split(",");
this.name = tokens[0];
this.pickUp = tokens[1];
this.destination = tokens[2];
this.rideTime = Integer.parseInt (tokens[3]);
this.miles = Integer.parseInt (tokens[4]);
this. earning = Double.parseDouble(tokens[5]);
}

public class UberTester{
public static void main (Stringargs)throws Exception{

BufferedReader in = new BufferedReader(new FileReader("uberLog.txt"));
String str;
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}

List<UberUser> users = new ArrayList<UberUser>();
for(String s :list){
users.add(new UberUser(s));
}
}
}


if anybody can help me i would really appreciate it thanks!



this is the error:



java.lang.ArrayIndexOutOfBoundsException: 1
at UberUser.<init>(UberUser.java:17)
at UberTester.main(UberTester.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)









share|improve this question















closed as off-topic by Andy Turner, gnat, shmosel, Pradeep Simha, Nicholas K Nov 16 '18 at 9:06


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Andy Turner, shmosel, Pradeep Simha, Nicholas K

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1





    The first problem is you haven't included the code in the question.

    – Peter Lawrey
    Nov 16 '18 at 8:31











  • Please edit your question to add the code and any errors, as well as a description of what you're trying to do, inputs etc. Images are not a good way to convey the necessary information.

    – Andy Turner
    Nov 16 '18 at 8:31






  • 1





    Im not sure why im getting this error... Which error?

    – shmosel
    Nov 16 '18 at 8:38






  • 1





    @Ricardo Your code seems to run fine. What is the issue?

    – Ankur Chrungoo
    Nov 16 '18 at 8:38








  • 1





    @PeterLawrey it was in the first version of the question.

    – Andy Turner
    Nov 16 '18 at 8:38














-5












-5








-5








The string im passing is this "Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16"



Im passing the string, and assigning the fields in the class to them., Im not sure why im getting this error



  public class UberUser{
String name;
String pickUp;
String destination;
int rideTime;
int miles;
double earning;

public UberUser(String s){
String line = s;
s=s.replaceAll("\s","");

String tokens = s.split(",");
this.name = tokens[0];
this.pickUp = tokens[1];
this.destination = tokens[2];
this.rideTime = Integer.parseInt (tokens[3]);
this.miles = Integer.parseInt (tokens[4]);
this. earning = Double.parseDouble(tokens[5]);
}

public class UberTester{
public static void main (Stringargs)throws Exception{

BufferedReader in = new BufferedReader(new FileReader("uberLog.txt"));
String str;
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}

List<UberUser> users = new ArrayList<UberUser>();
for(String s :list){
users.add(new UberUser(s));
}
}
}


if anybody can help me i would really appreciate it thanks!



this is the error:



java.lang.ArrayIndexOutOfBoundsException: 1
at UberUser.<init>(UberUser.java:17)
at UberTester.main(UberTester.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)









share|improve this question
















The string im passing is this "Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16"



Im passing the string, and assigning the fields in the class to them., Im not sure why im getting this error



  public class UberUser{
String name;
String pickUp;
String destination;
int rideTime;
int miles;
double earning;

public UberUser(String s){
String line = s;
s=s.replaceAll("\s","");

String tokens = s.split(",");
this.name = tokens[0];
this.pickUp = tokens[1];
this.destination = tokens[2];
this.rideTime = Integer.parseInt (tokens[3]);
this.miles = Integer.parseInt (tokens[4]);
this. earning = Double.parseDouble(tokens[5]);
}

public class UberTester{
public static void main (Stringargs)throws Exception{

BufferedReader in = new BufferedReader(new FileReader("uberLog.txt"));
String str;
List<String> list = new ArrayList<String>();
while((str = in.readLine()) != null){
list.add(str);
}

List<UberUser> users = new ArrayList<UberUser>();
for(String s :list){
users.add(new UberUser(s));
}
}
}


if anybody can help me i would really appreciate it thanks!



this is the error:



java.lang.ArrayIndexOutOfBoundsException: 1
at UberUser.<init>(UberUser.java:17)
at UberTester.main(UberTester.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)






java string split






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 8:47







Ricardo Ruvalcaba

















asked Nov 16 '18 at 8:29









Ricardo RuvalcabaRicardo Ruvalcaba

134




134




closed as off-topic by Andy Turner, gnat, shmosel, Pradeep Simha, Nicholas K Nov 16 '18 at 9:06


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Andy Turner, shmosel, Pradeep Simha, Nicholas K

If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Andy Turner, gnat, shmosel, Pradeep Simha, Nicholas K Nov 16 '18 at 9:06


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Andy Turner, shmosel, Pradeep Simha, Nicholas K

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1





    The first problem is you haven't included the code in the question.

    – Peter Lawrey
    Nov 16 '18 at 8:31











  • Please edit your question to add the code and any errors, as well as a description of what you're trying to do, inputs etc. Images are not a good way to convey the necessary information.

    – Andy Turner
    Nov 16 '18 at 8:31






  • 1





    Im not sure why im getting this error... Which error?

    – shmosel
    Nov 16 '18 at 8:38






  • 1





    @Ricardo Your code seems to run fine. What is the issue?

    – Ankur Chrungoo
    Nov 16 '18 at 8:38








  • 1





    @PeterLawrey it was in the first version of the question.

    – Andy Turner
    Nov 16 '18 at 8:38














  • 1





    The first problem is you haven't included the code in the question.

    – Peter Lawrey
    Nov 16 '18 at 8:31











  • Please edit your question to add the code and any errors, as well as a description of what you're trying to do, inputs etc. Images are not a good way to convey the necessary information.

    – Andy Turner
    Nov 16 '18 at 8:31






  • 1





    Im not sure why im getting this error... Which error?

    – shmosel
    Nov 16 '18 at 8:38






  • 1





    @Ricardo Your code seems to run fine. What is the issue?

    – Ankur Chrungoo
    Nov 16 '18 at 8:38








  • 1





    @PeterLawrey it was in the first version of the question.

    – Andy Turner
    Nov 16 '18 at 8:38








1




1





The first problem is you haven't included the code in the question.

– Peter Lawrey
Nov 16 '18 at 8:31





The first problem is you haven't included the code in the question.

– Peter Lawrey
Nov 16 '18 at 8:31













Please edit your question to add the code and any errors, as well as a description of what you're trying to do, inputs etc. Images are not a good way to convey the necessary information.

– Andy Turner
Nov 16 '18 at 8:31





Please edit your question to add the code and any errors, as well as a description of what you're trying to do, inputs etc. Images are not a good way to convey the necessary information.

– Andy Turner
Nov 16 '18 at 8:31




1




1





Im not sure why im getting this error... Which error?

– shmosel
Nov 16 '18 at 8:38





Im not sure why im getting this error... Which error?

– shmosel
Nov 16 '18 at 8:38




1




1





@Ricardo Your code seems to run fine. What is the issue?

– Ankur Chrungoo
Nov 16 '18 at 8:38







@Ricardo Your code seems to run fine. What is the issue?

– Ankur Chrungoo
Nov 16 '18 at 8:38






1




1





@PeterLawrey it was in the first version of the question.

– Andy Turner
Nov 16 '18 at 8:38





@PeterLawrey it was in the first version of the question.

– Andy Turner
Nov 16 '18 at 8:38












3 Answers
3






active

oldest

votes


















0














Looks like you're not passing the string in.



class Main {
public static void main(String args) {
UberUser user = new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
System.out.println(user);
}
}

class UberUser{
String name;
String pickUp;
String destination;
int rideTime;
int miles;
double earning;

public UberUser(String s){
String line = s;
s=s.replaceAll("\s","");

String tokens = s.split(",");
this.name = tokens[0];
this.pickUp = tokens[1];
this.destination = tokens[2];
this.rideTime = Integer.parseInt (tokens[3]);
this.miles = Integer.parseInt (tokens[4]);
this. earning = Double.parseDouble(tokens[5]);
}

public String toString() {
return String.format("Name: %snPick-Up: %snDestination: %snRide Time: %dnMiles: %dnEarning: £%f", name, pickUp, destination, rideTime, miles, earning);
}
}


Gives



java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

Name: Tom
Pick-Up: WingStop
Destination: FranklinMountain
Ride Time: 28
Miles: 5
Earning: £39.160000





share|improve this answer

































    0














    This is where using a debugger is useful as you are not using the input you think you are.



    This runs without error.



    class A {
    static class UberUser {
    String name;
    String pickUp;
    String destination;
    int rideTime;
    int miles;
    double earning;

    public UberUser(String s) {
    String line = s;
    s = s.replaceAll("\s", "");

    String tokens = s.split(",");
    this.name = tokens[0];
    this.pickUp = tokens[1];
    this.destination = tokens[2];
    this.rideTime = Integer.parseInt(tokens[3]);
    this.miles = Integer.parseInt(tokens[4]);
    this.earning = Double.parseDouble(tokens[5]);
    }
    }

    public static void main(String args) {
    new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
    }
    }


    As @AndyTurner points out you should be parsing the input before calling the constructor.



    class A {
    static class UberUser {
    String name;
    String pickUp;
    String destination;
    int rideTime;
    int miles;
    double earning;

    public UberUser(String name, String pickUp, String destination, int rideTime, int miles, double earning) {
    this.name = name;
    this.pickUp = pickUp;
    this.destination = destination;
    this.rideTime = rideTime;
    this.miles = miles;
    this.earning = earning;
    }

    @Override
    public String toString() {
    return "UberUser{" +
    "name='" + name + ''' +
    ", pickUp='" + pickUp + ''' +
    ", destination='" + destination + ''' +
    ", rideTime=" + rideTime +
    ", miles=" + miles +
    ", earning=" + earning +
    '}';
    }
    }

    public static void main(String args) {
    String line = "Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16";

    String tokens = line.trim().split(" *, *");
    String name = tokens[0];
    String pickUp = tokens[1];
    String destination = tokens[2];
    int rideTime = Integer.parseInt(tokens[3]);
    int miles = Integer.parseInt(tokens[4]);
    double earning = Double.parseDouble(tokens[5]);
    UberUser user = new UberUser(name, pickUp, destination, rideTime, miles, earning);
    System.out.println(user);
    }
    }


    prints



    UberUser{name='Tom', pickUp='Wing Stop', destination='Franklin Mountain', rideTime=28, miles=5, earning=39.16}





    share|improve this answer































      0














      You are trying to split a String using the delimiter , Since there is no , in your String the Split only returns a single index. which is tokens[0], The problem with your code is you are trying to call tokens[1] What I would do is check if the String contains , before splitting it. Or check if tokens.length is greater then 1.



      You can do a simple check by using this code



          if(!s.contains(",")){
      throw new IllegalArgumentException("Invalid String Pattern");
      }





      share|improve this answer


























      • Reason for downvote? The OP asked what is wrong with their code.

        – SamzSakerz
        Nov 16 '18 at 8:33











      • Whilst I haven't downvoted, your answer doesn't actually say anything other than "yep, it's wrong".

        – Andy Turner
        Nov 16 '18 at 8:33











      • @AndyTurner edited or does this still not explain whats wrong?

        – SamzSakerz
        Nov 16 '18 at 8:36











      • Maybe the OP isn't passing in , in the actual code they are testing, because the code the OP provided compiles for me!

        – SamzSakerz
        Nov 16 '18 at 8:38











      • There you go :P

        – SamzSakerz
        Nov 16 '18 at 8:41


















      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Looks like you're not passing the string in.



      class Main {
      public static void main(String args) {
      UberUser user = new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
      System.out.println(user);
      }
      }

      class UberUser{
      String name;
      String pickUp;
      String destination;
      int rideTime;
      int miles;
      double earning;

      public UberUser(String s){
      String line = s;
      s=s.replaceAll("\s","");

      String tokens = s.split(",");
      this.name = tokens[0];
      this.pickUp = tokens[1];
      this.destination = tokens[2];
      this.rideTime = Integer.parseInt (tokens[3]);
      this.miles = Integer.parseInt (tokens[4]);
      this. earning = Double.parseDouble(tokens[5]);
      }

      public String toString() {
      return String.format("Name: %snPick-Up: %snDestination: %snRide Time: %dnMiles: %dnEarning: £%f", name, pickUp, destination, rideTime, miles, earning);
      }
      }


      Gives



      java version "1.8.0_31"
      Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

      Name: Tom
      Pick-Up: WingStop
      Destination: FranklinMountain
      Ride Time: 28
      Miles: 5
      Earning: £39.160000





      share|improve this answer






























        0














        Looks like you're not passing the string in.



        class Main {
        public static void main(String args) {
        UberUser user = new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
        System.out.println(user);
        }
        }

        class UberUser{
        String name;
        String pickUp;
        String destination;
        int rideTime;
        int miles;
        double earning;

        public UberUser(String s){
        String line = s;
        s=s.replaceAll("\s","");

        String tokens = s.split(",");
        this.name = tokens[0];
        this.pickUp = tokens[1];
        this.destination = tokens[2];
        this.rideTime = Integer.parseInt (tokens[3]);
        this.miles = Integer.parseInt (tokens[4]);
        this. earning = Double.parseDouble(tokens[5]);
        }

        public String toString() {
        return String.format("Name: %snPick-Up: %snDestination: %snRide Time: %dnMiles: %dnEarning: £%f", name, pickUp, destination, rideTime, miles, earning);
        }
        }


        Gives



        java version "1.8.0_31"
        Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
        Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

        Name: Tom
        Pick-Up: WingStop
        Destination: FranklinMountain
        Ride Time: 28
        Miles: 5
        Earning: £39.160000





        share|improve this answer




























          0












          0








          0







          Looks like you're not passing the string in.



          class Main {
          public static void main(String args) {
          UberUser user = new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
          System.out.println(user);
          }
          }

          class UberUser{
          String name;
          String pickUp;
          String destination;
          int rideTime;
          int miles;
          double earning;

          public UberUser(String s){
          String line = s;
          s=s.replaceAll("\s","");

          String tokens = s.split(",");
          this.name = tokens[0];
          this.pickUp = tokens[1];
          this.destination = tokens[2];
          this.rideTime = Integer.parseInt (tokens[3]);
          this.miles = Integer.parseInt (tokens[4]);
          this. earning = Double.parseDouble(tokens[5]);
          }

          public String toString() {
          return String.format("Name: %snPick-Up: %snDestination: %snRide Time: %dnMiles: %dnEarning: £%f", name, pickUp, destination, rideTime, miles, earning);
          }
          }


          Gives



          java version "1.8.0_31"
          Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
          Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

          Name: Tom
          Pick-Up: WingStop
          Destination: FranklinMountain
          Ride Time: 28
          Miles: 5
          Earning: £39.160000





          share|improve this answer















          Looks like you're not passing the string in.



          class Main {
          public static void main(String args) {
          UberUser user = new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
          System.out.println(user);
          }
          }

          class UberUser{
          String name;
          String pickUp;
          String destination;
          int rideTime;
          int miles;
          double earning;

          public UberUser(String s){
          String line = s;
          s=s.replaceAll("\s","");

          String tokens = s.split(",");
          this.name = tokens[0];
          this.pickUp = tokens[1];
          this.destination = tokens[2];
          this.rideTime = Integer.parseInt (tokens[3]);
          this.miles = Integer.parseInt (tokens[4]);
          this. earning = Double.parseDouble(tokens[5]);
          }

          public String toString() {
          return String.format("Name: %snPick-Up: %snDestination: %snRide Time: %dnMiles: %dnEarning: £%f", name, pickUp, destination, rideTime, miles, earning);
          }
          }


          Gives



          java version "1.8.0_31"
          Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
          Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

          Name: Tom
          Pick-Up: WingStop
          Destination: FranklinMountain
          Ride Time: 28
          Miles: 5
          Earning: £39.160000






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 8:43

























          answered Nov 16 '18 at 8:40









          elkenelken

          138110




          138110

























              0














              This is where using a debugger is useful as you are not using the input you think you are.



              This runs without error.



              class A {
              static class UberUser {
              String name;
              String pickUp;
              String destination;
              int rideTime;
              int miles;
              double earning;

              public UberUser(String s) {
              String line = s;
              s = s.replaceAll("\s", "");

              String tokens = s.split(",");
              this.name = tokens[0];
              this.pickUp = tokens[1];
              this.destination = tokens[2];
              this.rideTime = Integer.parseInt(tokens[3]);
              this.miles = Integer.parseInt(tokens[4]);
              this.earning = Double.parseDouble(tokens[5]);
              }
              }

              public static void main(String args) {
              new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
              }
              }


              As @AndyTurner points out you should be parsing the input before calling the constructor.



              class A {
              static class UberUser {
              String name;
              String pickUp;
              String destination;
              int rideTime;
              int miles;
              double earning;

              public UberUser(String name, String pickUp, String destination, int rideTime, int miles, double earning) {
              this.name = name;
              this.pickUp = pickUp;
              this.destination = destination;
              this.rideTime = rideTime;
              this.miles = miles;
              this.earning = earning;
              }

              @Override
              public String toString() {
              return "UberUser{" +
              "name='" + name + ''' +
              ", pickUp='" + pickUp + ''' +
              ", destination='" + destination + ''' +
              ", rideTime=" + rideTime +
              ", miles=" + miles +
              ", earning=" + earning +
              '}';
              }
              }

              public static void main(String args) {
              String line = "Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16";

              String tokens = line.trim().split(" *, *");
              String name = tokens[0];
              String pickUp = tokens[1];
              String destination = tokens[2];
              int rideTime = Integer.parseInt(tokens[3]);
              int miles = Integer.parseInt(tokens[4]);
              double earning = Double.parseDouble(tokens[5]);
              UberUser user = new UberUser(name, pickUp, destination, rideTime, miles, earning);
              System.out.println(user);
              }
              }


              prints



              UberUser{name='Tom', pickUp='Wing Stop', destination='Franklin Mountain', rideTime=28, miles=5, earning=39.16}





              share|improve this answer




























                0














                This is where using a debugger is useful as you are not using the input you think you are.



                This runs without error.



                class A {
                static class UberUser {
                String name;
                String pickUp;
                String destination;
                int rideTime;
                int miles;
                double earning;

                public UberUser(String s) {
                String line = s;
                s = s.replaceAll("\s", "");

                String tokens = s.split(",");
                this.name = tokens[0];
                this.pickUp = tokens[1];
                this.destination = tokens[2];
                this.rideTime = Integer.parseInt(tokens[3]);
                this.miles = Integer.parseInt(tokens[4]);
                this.earning = Double.parseDouble(tokens[5]);
                }
                }

                public static void main(String args) {
                new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
                }
                }


                As @AndyTurner points out you should be parsing the input before calling the constructor.



                class A {
                static class UberUser {
                String name;
                String pickUp;
                String destination;
                int rideTime;
                int miles;
                double earning;

                public UberUser(String name, String pickUp, String destination, int rideTime, int miles, double earning) {
                this.name = name;
                this.pickUp = pickUp;
                this.destination = destination;
                this.rideTime = rideTime;
                this.miles = miles;
                this.earning = earning;
                }

                @Override
                public String toString() {
                return "UberUser{" +
                "name='" + name + ''' +
                ", pickUp='" + pickUp + ''' +
                ", destination='" + destination + ''' +
                ", rideTime=" + rideTime +
                ", miles=" + miles +
                ", earning=" + earning +
                '}';
                }
                }

                public static void main(String args) {
                String line = "Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16";

                String tokens = line.trim().split(" *, *");
                String name = tokens[0];
                String pickUp = tokens[1];
                String destination = tokens[2];
                int rideTime = Integer.parseInt(tokens[3]);
                int miles = Integer.parseInt(tokens[4]);
                double earning = Double.parseDouble(tokens[5]);
                UberUser user = new UberUser(name, pickUp, destination, rideTime, miles, earning);
                System.out.println(user);
                }
                }


                prints



                UberUser{name='Tom', pickUp='Wing Stop', destination='Franklin Mountain', rideTime=28, miles=5, earning=39.16}





                share|improve this answer


























                  0












                  0








                  0







                  This is where using a debugger is useful as you are not using the input you think you are.



                  This runs without error.



                  class A {
                  static class UberUser {
                  String name;
                  String pickUp;
                  String destination;
                  int rideTime;
                  int miles;
                  double earning;

                  public UberUser(String s) {
                  String line = s;
                  s = s.replaceAll("\s", "");

                  String tokens = s.split(",");
                  this.name = tokens[0];
                  this.pickUp = tokens[1];
                  this.destination = tokens[2];
                  this.rideTime = Integer.parseInt(tokens[3]);
                  this.miles = Integer.parseInt(tokens[4]);
                  this.earning = Double.parseDouble(tokens[5]);
                  }
                  }

                  public static void main(String args) {
                  new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
                  }
                  }


                  As @AndyTurner points out you should be parsing the input before calling the constructor.



                  class A {
                  static class UberUser {
                  String name;
                  String pickUp;
                  String destination;
                  int rideTime;
                  int miles;
                  double earning;

                  public UberUser(String name, String pickUp, String destination, int rideTime, int miles, double earning) {
                  this.name = name;
                  this.pickUp = pickUp;
                  this.destination = destination;
                  this.rideTime = rideTime;
                  this.miles = miles;
                  this.earning = earning;
                  }

                  @Override
                  public String toString() {
                  return "UberUser{" +
                  "name='" + name + ''' +
                  ", pickUp='" + pickUp + ''' +
                  ", destination='" + destination + ''' +
                  ", rideTime=" + rideTime +
                  ", miles=" + miles +
                  ", earning=" + earning +
                  '}';
                  }
                  }

                  public static void main(String args) {
                  String line = "Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16";

                  String tokens = line.trim().split(" *, *");
                  String name = tokens[0];
                  String pickUp = tokens[1];
                  String destination = tokens[2];
                  int rideTime = Integer.parseInt(tokens[3]);
                  int miles = Integer.parseInt(tokens[4]);
                  double earning = Double.parseDouble(tokens[5]);
                  UberUser user = new UberUser(name, pickUp, destination, rideTime, miles, earning);
                  System.out.println(user);
                  }
                  }


                  prints



                  UberUser{name='Tom', pickUp='Wing Stop', destination='Franklin Mountain', rideTime=28, miles=5, earning=39.16}





                  share|improve this answer













                  This is where using a debugger is useful as you are not using the input you think you are.



                  This runs without error.



                  class A {
                  static class UberUser {
                  String name;
                  String pickUp;
                  String destination;
                  int rideTime;
                  int miles;
                  double earning;

                  public UberUser(String s) {
                  String line = s;
                  s = s.replaceAll("\s", "");

                  String tokens = s.split(",");
                  this.name = tokens[0];
                  this.pickUp = tokens[1];
                  this.destination = tokens[2];
                  this.rideTime = Integer.parseInt(tokens[3]);
                  this.miles = Integer.parseInt(tokens[4]);
                  this.earning = Double.parseDouble(tokens[5]);
                  }
                  }

                  public static void main(String args) {
                  new UberUser("Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16");
                  }
                  }


                  As @AndyTurner points out you should be parsing the input before calling the constructor.



                  class A {
                  static class UberUser {
                  String name;
                  String pickUp;
                  String destination;
                  int rideTime;
                  int miles;
                  double earning;

                  public UberUser(String name, String pickUp, String destination, int rideTime, int miles, double earning) {
                  this.name = name;
                  this.pickUp = pickUp;
                  this.destination = destination;
                  this.rideTime = rideTime;
                  this.miles = miles;
                  this.earning = earning;
                  }

                  @Override
                  public String toString() {
                  return "UberUser{" +
                  "name='" + name + ''' +
                  ", pickUp='" + pickUp + ''' +
                  ", destination='" + destination + ''' +
                  ", rideTime=" + rideTime +
                  ", miles=" + miles +
                  ", earning=" + earning +
                  '}';
                  }
                  }

                  public static void main(String args) {
                  String line = "Tom, Wing Stop, Franklin Mountain, 28, 5, 39.16";

                  String tokens = line.trim().split(" *, *");
                  String name = tokens[0];
                  String pickUp = tokens[1];
                  String destination = tokens[2];
                  int rideTime = Integer.parseInt(tokens[3]);
                  int miles = Integer.parseInt(tokens[4]);
                  double earning = Double.parseDouble(tokens[5]);
                  UberUser user = new UberUser(name, pickUp, destination, rideTime, miles, earning);
                  System.out.println(user);
                  }
                  }


                  prints



                  UberUser{name='Tom', pickUp='Wing Stop', destination='Franklin Mountain', rideTime=28, miles=5, earning=39.16}






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 8:40









                  Peter LawreyPeter Lawrey

                  448k56575979




                  448k56575979























                      0














                      You are trying to split a String using the delimiter , Since there is no , in your String the Split only returns a single index. which is tokens[0], The problem with your code is you are trying to call tokens[1] What I would do is check if the String contains , before splitting it. Or check if tokens.length is greater then 1.



                      You can do a simple check by using this code



                          if(!s.contains(",")){
                      throw new IllegalArgumentException("Invalid String Pattern");
                      }





                      share|improve this answer


























                      • Reason for downvote? The OP asked what is wrong with their code.

                        – SamzSakerz
                        Nov 16 '18 at 8:33











                      • Whilst I haven't downvoted, your answer doesn't actually say anything other than "yep, it's wrong".

                        – Andy Turner
                        Nov 16 '18 at 8:33











                      • @AndyTurner edited or does this still not explain whats wrong?

                        – SamzSakerz
                        Nov 16 '18 at 8:36











                      • Maybe the OP isn't passing in , in the actual code they are testing, because the code the OP provided compiles for me!

                        – SamzSakerz
                        Nov 16 '18 at 8:38











                      • There you go :P

                        – SamzSakerz
                        Nov 16 '18 at 8:41
















                      0














                      You are trying to split a String using the delimiter , Since there is no , in your String the Split only returns a single index. which is tokens[0], The problem with your code is you are trying to call tokens[1] What I would do is check if the String contains , before splitting it. Or check if tokens.length is greater then 1.



                      You can do a simple check by using this code



                          if(!s.contains(",")){
                      throw new IllegalArgumentException("Invalid String Pattern");
                      }





                      share|improve this answer


























                      • Reason for downvote? The OP asked what is wrong with their code.

                        – SamzSakerz
                        Nov 16 '18 at 8:33











                      • Whilst I haven't downvoted, your answer doesn't actually say anything other than "yep, it's wrong".

                        – Andy Turner
                        Nov 16 '18 at 8:33











                      • @AndyTurner edited or does this still not explain whats wrong?

                        – SamzSakerz
                        Nov 16 '18 at 8:36











                      • Maybe the OP isn't passing in , in the actual code they are testing, because the code the OP provided compiles for me!

                        – SamzSakerz
                        Nov 16 '18 at 8:38











                      • There you go :P

                        – SamzSakerz
                        Nov 16 '18 at 8:41














                      0












                      0








                      0







                      You are trying to split a String using the delimiter , Since there is no , in your String the Split only returns a single index. which is tokens[0], The problem with your code is you are trying to call tokens[1] What I would do is check if the String contains , before splitting it. Or check if tokens.length is greater then 1.



                      You can do a simple check by using this code



                          if(!s.contains(",")){
                      throw new IllegalArgumentException("Invalid String Pattern");
                      }





                      share|improve this answer















                      You are trying to split a String using the delimiter , Since there is no , in your String the Split only returns a single index. which is tokens[0], The problem with your code is you are trying to call tokens[1] What I would do is check if the String contains , before splitting it. Or check if tokens.length is greater then 1.



                      You can do a simple check by using this code



                          if(!s.contains(",")){
                      throw new IllegalArgumentException("Invalid String Pattern");
                      }






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 16 '18 at 8:41

























                      answered Nov 16 '18 at 8:32









                      SamzSakerzSamzSakerz

                      1,6691627




                      1,6691627













                      • Reason for downvote? The OP asked what is wrong with their code.

                        – SamzSakerz
                        Nov 16 '18 at 8:33











                      • Whilst I haven't downvoted, your answer doesn't actually say anything other than "yep, it's wrong".

                        – Andy Turner
                        Nov 16 '18 at 8:33











                      • @AndyTurner edited or does this still not explain whats wrong?

                        – SamzSakerz
                        Nov 16 '18 at 8:36











                      • Maybe the OP isn't passing in , in the actual code they are testing, because the code the OP provided compiles for me!

                        – SamzSakerz
                        Nov 16 '18 at 8:38











                      • There you go :P

                        – SamzSakerz
                        Nov 16 '18 at 8:41



















                      • Reason for downvote? The OP asked what is wrong with their code.

                        – SamzSakerz
                        Nov 16 '18 at 8:33











                      • Whilst I haven't downvoted, your answer doesn't actually say anything other than "yep, it's wrong".

                        – Andy Turner
                        Nov 16 '18 at 8:33











                      • @AndyTurner edited or does this still not explain whats wrong?

                        – SamzSakerz
                        Nov 16 '18 at 8:36











                      • Maybe the OP isn't passing in , in the actual code they are testing, because the code the OP provided compiles for me!

                        – SamzSakerz
                        Nov 16 '18 at 8:38











                      • There you go :P

                        – SamzSakerz
                        Nov 16 '18 at 8:41

















                      Reason for downvote? The OP asked what is wrong with their code.

                      – SamzSakerz
                      Nov 16 '18 at 8:33





                      Reason for downvote? The OP asked what is wrong with their code.

                      – SamzSakerz
                      Nov 16 '18 at 8:33













                      Whilst I haven't downvoted, your answer doesn't actually say anything other than "yep, it's wrong".

                      – Andy Turner
                      Nov 16 '18 at 8:33





                      Whilst I haven't downvoted, your answer doesn't actually say anything other than "yep, it's wrong".

                      – Andy Turner
                      Nov 16 '18 at 8:33













                      @AndyTurner edited or does this still not explain whats wrong?

                      – SamzSakerz
                      Nov 16 '18 at 8:36





                      @AndyTurner edited or does this still not explain whats wrong?

                      – SamzSakerz
                      Nov 16 '18 at 8:36













                      Maybe the OP isn't passing in , in the actual code they are testing, because the code the OP provided compiles for me!

                      – SamzSakerz
                      Nov 16 '18 at 8:38





                      Maybe the OP isn't passing in , in the actual code they are testing, because the code the OP provided compiles for me!

                      – SamzSakerz
                      Nov 16 '18 at 8:38













                      There you go :P

                      – SamzSakerz
                      Nov 16 '18 at 8:41





                      There you go :P

                      – SamzSakerz
                      Nov 16 '18 at 8:41



                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine