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.
How to read integers in a text file as integers
Page 1 of 12 Replies - 100 Views - Last Post: 30 January 2013 - 07:48 PM
Replies To: How to read integers in a text file as integers
#2
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?
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
#3
Re: How to read integers in a text file as integers
Posted 30 January 2013 - 07:48 PM
andrewsw, 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?
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]);
}
}
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|