1

This program is going to output distance among other things from specific points that a user might enter. However, I'm having trouble setting up my scanner because its reading input from another text file. Here is my code:

public class junior 
{


public static void main(String[] args)throws IOException
{
    double avgMPG;
    double pricePer;
    double speed;
    String letter1;
    String letter2;
    int start = 0;
    int end=0;
    int distance;


    Scanner inData = new Scanner(new File("input.txt"));

    boolean repeat=false;
while (inData.hasNext())
{
    letter1.inData.next();
    letter2.inData.next();


    while (!repeat)
        {


            System.out.print("1.");
            letter1=inData.next();
            if(letter1.equals("A"))
            {
                start=0;
                repeat=true;

            }
            else if (letter1.equals("B"))
            {
                start=450;
                repeat=true;


            }
            else if (letter1.equals("C"))
            {
                start=590;
                repeat=true;

            }
            else if (letter1.equals("D"))
            {
                start=710;
                repeat=true;

            }
            else if(letter1.equals("E"))
            {
                start=1030;
                repeat=true;

            }
            else if (letter1.equals("F"))
            {
                start=1280;
                repeat=true;

            }
            else if (letter1.equals("G"))
            {
                start=1360;
                repeat=true;
            }
            else
            {
                System.out.println("ERROR: You didn't enter a valid character. Please try again");
                repeat=false;
            }
        }

repeat=false;
while (!repeat)
{


    if(letter2.equals("A"))
    {
        end=0;
        repeat=true;

    }
    else if (letter2.equals("B"))
    {
        end=450;
        repeat=true;


    }
    else if (letter2.equals("C"))
    {
        end=590;
        repeat=true;

    }
    else if (letter2.equals("D"))
    {
        end=710;
        repeat=true;

    }
    else if(letter2.equals("E"))
    {
        end=1030;
        repeat=true;

    }
    else if (letter2.equals("F"))
    {
        end=1280;
        repeat=true;

    }
    else if (letter2.equals("G"))
    {
        end=1360;
        repeat=true;
    }
    else
    {
        System.out.println("ERROR: You didn't enter a valid character. Please try again");
        repeat=false;
    }
    distance=end-start;
    System.out.print("Total distance: "+distance);
}
}
}


}

The part thats says "inData.next" keeps sending an error saying that the field does not exist or cannot be resolved. How do I fix this to correctly read from my test file?

OoOoOoOoOoO
  • 131
  • 3
  • 10
  • `letter1.inData.next();` should be `letter1 = inData.next();`? – takendarkk Dec 04 '14 at 16:41
  • Pretty sure @Takendarkk is right. For what you're trying to do, this might be a better route for reading in from a file: http://stackoverflow.com/questions/811851/how-do-i-read-input-character-by-character-in-java –  Dec 04 '14 at 16:47

1 Answers1

1

It looks as though here

letter1.inData.next();
letter2.inData.next();

you're trying to read from the scanner and put the result into your String variables. But to do that, you want

letter1 = inData.next();
letter2 = inData.next();

The compiler is complaining because you're trying to get to a field of your String variables called inData (that's what letter1.inData means), but they don't have such a field.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67