Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I am a student in my first Java class. so far all has gone well, this new code is just not working

  1. #1
    Junior Member
    Join Date
    Feb 2023
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I am a student in my first Java class. so far all has gone well, this new code is just not working

    it seems simple and I was confident. I input a tweet string and then parse it out. the first two lines went well and I thought it would just be a repeat.

    "#typ fire; #det firefighter sees a glow; #loc sw from west coach rd toward sunshine canyon;
    #lat 40.0515; #lng -105.332;"
    Type: FIRE
    Detail: firefighter sees a glow
    Location: sw from west coach rd toward sunshine canyon
    Latitude: p fire
    Longitude: p fire

    should be
    Type: FIRE
    Detail: firefighter sees a glow
    Location: sw from west coach rd toward sunshine canyon
    Latitude: 40.0515
    Longitude: -105.332


    again it seems my first three fields work, and I have used the same logic for the last two fields.
    I am guessing it has something to do with the wraping of the text, i.e. it is on two lines?

    any help is appreciated

    import java.util.Scanner;

    public class ParseTheTweet {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Enter a tweet below");

    Scanner tweetInput = new Scanner(System.in);

    String tweet = tweetInput.nextLine();
    tweet = tweet.replace(',','-');

    String type, detail, location, latitude, longitude;

    int startType = tweet.indexOf("#typ");
    int startDetail = tweet.indexOf("#det");
    int startLoc = tweet.indexOf("#loc");
    int startLat = tweet.indexOf("#lat");
    int startLng = tweet.indexOf("#lng");
    int finishType = tweet.indexOf(";", startType);
    int finishDetail = tweet.indexOf(";", startDetail);
    int finishLoc = tweet.indexOf(";", startLoc);
    int finishLat = tweet.indexOf(";", startLat);
    int finishLng = tweet.indexOf(";", startLng);

    //these ten variables mark the start and finish of the index positions needed for the substrings

    type = tweet.substring(startType + 5 , finishType);
    type = type.toUpperCase();
    String typeTrimmed = type.trim();
    System.out.print("\nType: \t\t\t");
    System.out.println(typeTrimmed);

    detail = tweet.substring(startDetail +5, finishDetail);
    String detTrimmed = detail.trim();
    System.out.print("Detail: \t\t");
    System.out.println(detTrimmed);

    location = tweet.substring(startLoc + 5, finishLoc);
    String locTrimmed = location.trim();
    System.out.print("Location: \t\t");
    System.out.println(locTrimmed);

    latitude = tweet.substring(startLat + 5, finishLat);
    String latTrimmed = latitude.trim();
    System.out.print("Latitude: \t\t");
    System.out.println(latTrimmed);

    longitude = tweet.substring(startLng + 5, finishLng);
    String lngTrimmed = longitude.trim();
    System.out.print("Longitude: \t\t");
    System.out.println(lngTrimmed);

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I am a student in my first Java class. so far all has gone well, this new code is just not working

    Latitude: p fire
    Is that line the problem?
    Is this the input?
    "#typ fire; #det firefighter sees a glow; #loc sw from west coach rd toward sunshine canyon;
    #lat 40.0515; #lng -105.332;"
    Where does the value: "p fire" come from? It looks like it came from the first line of the input, skipping over the #ty

    The code should check for #lat to be sure that the data is as expected.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java beginner code not working in Ireport :(
    By jakehngo in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 28th, 2017, 05:19 PM
  2. Complete beginner (college student) - code not working!!
    By javanewbie085 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2013, 09:19 AM
  3. Replies: 4
    Last Post: December 4th, 2012, 06:11 PM
  4. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  5. New java student need help with code
    By Darkcore123 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 10:50 AM