Home > other >  How do you print out the last position or occurrence of a number within a file without an array?
How do you print out the last position or occurrence of a number within a file without an array?

Time:11-29

I am trying to print out only the last position # of a number from a file called numbers.text. numbers.text:

10
23
43
5
12
23
9
8
10
1
16
9

Basically, I am attempting to print out a statement along the lines of (using 10 as an example) "10 appeared last at position 9."

import java.io.File;
import java.util.*;

public class SortOf3 {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        Scanner datafile = new Scanner(new File("numbers.text"));

        boolean duplicate = true;
        int count = 0;
        int duplicate = 0
        while (datafile.hasNextInt()) {
            if (duplicate) {
                System.out.print("Enter a number: ");
                lastNumber = scanner.nextInt();
                count  ;
                duplicate = false;
            } else {
                System.out.print("Enter a number: ");
                lastNumber = scanner.nextInt();
                count  ;
            }
  

                if (lastNumber == datafile.nextInt())
                    System.out.println(lastNumber   " last appears in the file at position "   count);
                else
                    System.out.println(lastNumber   " does not appear in the file");
                }
            }
        }

I tried using boolean duplicate in hopes of ignoring the counter the first time lastNumber appears, however it did not seem to do anything. If there's no way to make boolean work here, what's another way to print out the last position of a number in a file?

CodePudding user response:

You need to exhaustively check for the number before declaring it wasn't found. Also, I would start by reading the lines into a List, then you can search backward through the List to find the last occurence of the value (since we're scanning backward, that would be the first from the right hand side). Something like,

Scanner scanner = new Scanner(System.in);
try {
    List<String> lines = Files.readAllLines(new File("numbers.text").toPath());
    System.out.print("Enter a number (-1 to quit): ");
    int lastNumber;
    while ((lastNumber = scanner.nextInt()) != -1) {
        boolean found = false;
        for (int j = lines.size() - 1; j >= 0 && !found; j--) {
            if (Integer.parseInt(lines.get(j)) == lastNumber) {
                System.out.println(
                        lastNumber
                          " last appears in the file at position "
                          j);
                found = true;
            }
        }
        if (!found) {
            System.out.println(lastNumber   " does not appear in the file");
        }
        System.out.print("Enter a number (-1 to quit): ");
    }
} catch (IOException e) {
    e.printStackTrace();
}

CodePudding user response:

You can try using a HashMap

Map<String, Integer> map = new HashMap<>();

In your while if you found the value you are searching you can put that in your HashMap

  map.put(value, position);

You can get the position with the counter and then when the while ends you just check if the map contains the value

if(map.containsKey(value)){
   map.get(value) //getting position of value
}
else{
   //value not found
}
  •  Tags:  
  • java
  • Related