简体   繁体   中英

Reading Integers from Text File and Storing Into Separate Variables? (Scanner)

I have a two-part question here. So I'm writing code for an assignment that tries to read in integers from a text file as coordinates. For example, I have a text file titled test1 that reads something like: 50 00 510 53 310 0. Now, those integers are supposed to representative of coordinates meaning that 50 actually translates to (5,0), (0,0), (5,10) and so forth.

I'm trying to use a scanner to go into that text file and pick the first number in that double digit integer and store it as an "x" value and then select the second number and store it as a "y" value and then rinse repeat for the remainder.

 int nSheep = 0;
 while (sc.hasNextLine()) {
     nSheep++;
     sc.nextLine();
 }

This is my current code to determine how many sheep there are in the text file. It basically just reads how many lines there are, counts them, and stores them in the variable nSheep. So in my test1 file example, it would return the number 6.

for (int i = 0; i < nSheep; i++) {
    while (sc.hasNextLine()) {
        int x = sc.nextInt();
        int y = sc.nextInt();
    }
    System.out.println(x);
} 

This is my attempt at reading in the integers and storing them in variable x and y. I can't tell if this is even close to working as the println doesn't print anything out.

Finally...

xMin = xMax = sc.nextInt();
yMin = yMax = sc.nextInt();

//read the remaining coordinates
for (int i = 1; i <= nSheep - 1; i++) {
     while (sc.hasNextInt) {
        int x = sc.nextInt();
        int y = sc.nextInt();

        if (x < xMin)
            xMin = x;
        if (x > xMax)
            xMax = x;
        if (y < yMin)
            yMin = y;
        if (y > yMax)
            yMax = y;

        if (x < xMin)
           xMin = x;
        if (x > xMax)
           xMax = x;
        if (y < yMin)
           yMin = y;
        if (y > yMax)
           yMax = y;
    }
}
System.out.print("Fence Coordinates: {(" + xMin + "," + yMin + "), ");
System.out.print("(" + xMax + "," + yMin + "), ");
System.out.print("(" + xMax + "," + yMax + "), ");
System.out.println("(" + xMin + "," + yMax + ") ");

This was the successful code that works if I asked a user to input the number of sheep and coordinates themselves. The only difference from this is that I don't want user input, I just want a scanner to read the text file, determine the coordinates, and then print them out. If this long-winded question makes sense, can anyone help me?

creating File instance to reference text file in Java

File text = new File("C:/temp/test1.txt");

Creating Scanner instance to read File in Java

Scanner sc = new Scanner(text);

so

File text = new File("C:/temp/test1.txt");
Scanner sc = new Scanner(text);

int xMin, xMax;
xMin = xMax = sc.nextInt();

int yMin, yMax
yMin = yMax = sc.nextInt();

while (sc.hasNextLine()) {
    int x = sc.nextInt();
    int y = sc.nextInt();

    if (x < xMin) { xMin = x; }
    if (y < yMin) { yMin = y; }
    if (x > xMax) { xMax = x; }
    if (y > yMax) { yMax = y; }
}

System.out.println("Fence Coordinates:"
System.out.print("{ (" + xMin + "," + yMin + "), ");
System.out.print("(" + xMax + "," + yMin + "), ");
System.out.print("(" + xMax + "," + yMax + "), ");
System.out.print("(" + xMin + "," + yMax + ") } ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM