2 Replies - 357 Views - Last Post: 30 January 2013 - 07:48 PM Rate Topic: -----

#1 shnooby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 13

How to read integers in a text file as integers

Posted 30 January 2013 - 05:16 PM

I have numbers on multiple different lines, I need to read them as full numbers. (30, 2, 120) I need to put them in condition statements as numbers, meaning I am comparing them to another number with GREATER THAN or EQUAL TO.

I figured I could do this by putting the number from the text file into a variable, putting the variable in the condition, and then running through the next number in the text file, using a loop and the same variable.

But it will read the number as text right? Help please.

Is This A Good Question/Topic? 0
  • +

Replies To: How to read integers in a text file as integers

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3817
  • View blog
  • Posts: 13,532
  • Joined: 12-December 12

Re: How to read integers in a text file as integers

Posted 30 January 2013 - 05:28 PM

You could use a BufferedReader to read the lines and then use either String.split() or the Scanner to separate the integers.

Yes, the text-file is read as a string initially, so using split or Scanner will help convert them to numbers.

If you need more assistance then you'll need to describe what the file looks like:
Is it just one long string? Or is it split into lines?
Are the numbers separated by commas, spaces or both?
Are (brackets) involved, as suggested in your post?

This post has been edited by andrewsw: 30 January 2013 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 shnooby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 13

Re: How to read integers in a text file as integers

Posted 30 January 2013 - 07:48 PM

View Postandrewsw, on 30 January 2013 - 05:28 PM, said:

You could use a BufferedReader to read the lines and then use either String.split() or the Scanner to separate the integers.

Yes, the text-file is read as a string initially, so using split or Scanner will help convert them to numbers.

If you need more assistance then you'll need to describe what the file looks like:
Is it just one long string? Or is it split into lines?
Are the numbers separated by commas, spaces or both?
Are (brackets) involved, as suggested in your post?


"76, 89, ..." The numbers are represented exactly as they are in the quotes, and sometimes the number is a triple digit.

If I'm blunt, sorry but having one of those bad weeks.

package pkg1.pkg29.pkg13;

import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(new File("number.txt"));
        int[] numbers = new int[8];
        int i = 0;
        while (scan.hasNextInt()) {
            i = scan.nextInt();
        }
        if (i <= 24) {
            numbers[0] = numbers[0] + 1;
        } else if (i <= 49) {
            numbers[1] = numbers[1] + 1;
        } else if (i <= 74) {
            numbers[2] = numbers[2] + 1;
        } else if (i <= 99) {
            numbers[3] = numbers[3] + 1;
        } else if (i <= 124) {
            numbers[4] = numbers[4] + 1;
        } else if (i <= 149) {
            numbers[5] = numbers[5] + 1;
        } else if (i <= 174) {
            numbers[6] = numbers[6] + 1;
        } else if (i <= 175) {
            numbers[7] = numbers[7] + 1;
        }
        System.out.println(numbers[0]);
        System.out.println(numbers[1]);
        System.out.println(numbers[2]);
        System.out.println(numbers[3]);
        System.out.println(numbers[4]);
        System.out.println(numbers[5]);
        System.out.println(numbers[6]);
        System.out.println(numbers[7]);
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1