I am new to Java. How can i read each integer from a line in a text file. I know the reader class has the read and readline functions for it. But in my case i not only want to read the integers in the file but want to know when the line changes. Because the first element of every line denotes an array index and all the corresponding elements are the linked list values attached to that index.
For example, see the 4 line below. In this case i not only want to read each integer but the first integer of every line would be an array index so i will have an a 4 element array with each array element correspoding to a list where A[1]-> 4, A[2]-> 1,3,4 and soo on.
1 4
2 1 3 4
3 2 5
4 2
After retrieving the integers properly i am planning to populate them via
ArrayList<Integer>[] aList = (ArrayList<Integer>[]) new ArrayList[numLines];
EDITED : I had been asked in one the comments that what i have thinked soo far and where exctly i am stucken so below is what i am thinking (in terms of original and pseoudo code mixed)..
while (lr.readLine() != null) {
while ( // loop through each character)
if ( first charcter)
aList[index] = first character;
else
aList[index]->add(second char.... last char of the line);
}
Thanks
Thanks for the scanner hint, Andrew Thompson.
This is how i have achieved it
Scanner sc =new Scanner(new File("FileName.txt"));
ArrayList<Integer>[] aList = (ArrayList<Integer>[]) new ArrayList[200];
String line;
sc.useDelimiter("\\n");
int vertex = 0;
while (sc.hasNextLine()) {
int edge = 0;
line = sc.nextLine();
Scanner lineSc = new Scanner(line);
lineSc.useDelimiter("\\s");
vertex = lineSc.nextInt() - 1;
aList[vertex] = new ArrayList<Integer>();
int tmp = 0;
System.out.println(vertex);
while (lineSc.hasNextInt()) {
edge = lineSc.nextInt();
aList[vertex].add(edge);
System.out.print(aList[vertex].get(tmp) + " ");
++tmp;
}
System.out.println ();
}
Related
i have a text file and i want to read the integers and doubles. I dont know how many values i have to read. The first value in the line is always the integer and the second is always the double. I want to save the value of the first line seperately.
200
11010 0.004
500 0.02
637 0.018
How to create 2 arrays and save the values, so i can use them later? I am not allowed to create a new class. I tried to use Point but cant store doubles.
Scanner scanner = new Scanner(new File(args[0]));
int cores= scanner.nextInt();
System.out.print(cores);
while (scanner.hasNext()) {
int x = scanner.nextInt();
double y = scanner.nextDouble();
System.out.printf("x");}
I' ve tried the code above but throws out Exception
You can use simple file handling approach to read the file line by line, For the first line you can use a flag to mark the line and sent the file to remote location you want to save the data. Then for all later lines you can split the string on the basis of " " (space). Post which once you have stripped the elements of the resulting array you can typecast and append the element at first index to integer array. And the second element (typecast before append) to the double array. This shall work absolutely fine with any length of file.
A demo code for the same is as following:
public class ReadLineByLine
{
public static void main(String args[])
{
try
{
FileInputStream fis=new FileInputStream("Demo.txt");
Scanner sc=new Scanner(fis);
String tempLineData = "";
int flag = 0;
String[] elements;
List<Integer> ints = new ArrayList<Integer>(
List<Float> floats = new ArrayList<Float>(
while(sc.hasNextLine())
{
if(flag == 0){
// Place the operation with the first line here
flag++;
}
tempLineData=sc.nextLine();
elements = tempLineData.split(" ");
ints.add((int)elements[0].trim());
floats.add((float)elements[1].trim());
}
sc.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
This is a project from school, but i'm only asking for help in the logic on one small part of it. I got most of it figured out.
I'm being given a file with lines of string integers, for example:
1234 123
12 153 23
1234
I am to read each line, compute the sum, and then go to the next one to produce this:
1357
188
1234
I'm stuck on the scanner part.
public static void doTheThing(Scanner input) {
int[] result = new int[MAX_DIGITS];
while(input.hasNextLine()) {
String line = input.nextLine();
Scanner linesc = new Scanner(line);
while(linesc.hasNext()) {
String currentLine = linesc.next();
int[] currentArray = convertArray(stringToArray(currentLine));
result = addInt(result, currentArray);
}
result = new int[MAX_DIGITS];
}
}
In a nutshell, I want to grab each big integer, put it an array of numbers, add them, and then i'll do the rest later.
What this is doing it's basically reading all the lines and adding everything and putting it into a single array.
What i'm stuck on is how do I read each line, add, reset the value to 0, and then read the next line? I've been at this for hours and i'm mind stumped.
Edit 01: I realize now that I should be using another scanner to read each line, but now i'm getting an error that looks like an infinite loop?
Edit 02: Ok, so after more hints and advice, I'm past that error, but now it's doing exactly what the original problem is.
Final Edit: Heh....fixed it. I was forgetting to reset the value to "0" before printing each value. So it makes sense that it was adding all of the values.
Yay....coding is fun....
hasNext method of the Scanner class can be used to check if there is any data available in stream or not. Accordingly, next method used to retrieve next continuous sequence of characters without white space characters. Here use of the hasNext method as condition of if doesn't make any sense as what you want is to check if the there are any numerical data left in the current line. You can use next(String pattern).
In addition, you can try this solution even though it is not optimal solution...
// In a loop
String line = input.nextLine(); //return entire line & descard newline character.
String naw[] = line.split(" "); //split line into sub strings.
/*naw contains numbers of the current line in form of string array.
Now you can perfom your logic after converting string to int.*/
I would also like to mention that it can easily & efficiently be done using java-8 streams.
An easier approach would be to abandon the Scanner altogether, let java.nio.io.Files to the reading for you and then just handle each line:
Files.lines(Paths.get("/path/to/my/file.txt"))
.map(s -> Arrays.stream(s.split("\\s+")).mapToInt(Integer::parseInt).sum())
.forEach(System.out::println);
If i were you i would be using the BufferedReader insted of the Scanner like this:
BufferedReader br = new BufferedReader(new FileReader("path"));
String line = "";
while((line = br.readLine()) != null)
{
int sum = 0;
String[] arr = line.split(" ");
for(String num : arr)
{
sum += Integer.parseInt(num);
}
System.out.println(sum);
}
Considering the level you're on, I think you should consider this solution. By using only the scanner, you can split the lines into an array of tokens, then iterate and sum the tokens by parsing them and validating that they're not empty.
import java.util.*;
class SumLines {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
while(S.hasNext()) {
String[] tokens = S.nextLine().split(" ");
int sum = 0;
for(int i = 0; i < tokens.length; i++) {
if(!tokens[i].equals("")) sum += Integer.parseInt(tokens[i]);
}
System.out.println(sum);
}
}
}
Say you have a text file with "abcdefghijklmnop" and you have to add 3 characters at a time to an array list of type string. So the first cell of the array list would have "abc", the second would have "def" and so on until all the characters are inputted.
public ArrayList<String> returnArray()throws FileNotFoundException
{
int i = 0
private ArrayList<String> list = new ArrayList<String>();
Scanner scanCharacters = new Scanner(file);
while (scanCharacters.hasNext())
{
list.add(scanCharacters.next().substring(i,i+3);
i+= 3;
}
scanCharacters.close();
return characters;
}
Please use the below code,
ArrayList<String> list = new ArrayList<String>();
int i = 0;
int x = 0;
Scanner scanCharacters = new Scanner(file);
scanCharacters.useDelimiter(System.getProperty("line.separator"));
String finalString = "";
while (scanCharacters.hasNext()) {
String[] tokens = scanCharacters.next().split("\t");
for (String str : tokens) {
finalString = StringUtils.deleteWhitespace(str);
for (i = 0; i < finalString.length(); i = i + 3) {
x = i + 3;
if (x < finalString.length()) {
list.add(finalString.substring(i, i + 3));
} else {
list.add(finalString.substring(i, finalString.length()));
}
}
}
}
System.out.println("list" + list);
Here i have used StringUtils.deleteWhitespace(str) of Apache String Utils to delete the blank space from the file tokens.and the if condition inside for loop to check the substring for three char is available in the string if its not then whatever character are left it will go to the list.My text file contains the below strings
asdfcshgfser ajsnsdxs in first line and in second line
sasdsd fghfdgfd
after executing the program result are as,
list[asd, fcs, hgf, ser, ajs, nsd, xs, sas, dsd, fgh, fdg, fd]
public ArrayList<String> returnArray()throws FileNotFoundException
{
private ArrayList<String> list = new ArrayList<String>();
Scanner scanCharacters = new Scanner(file);
String temp = "";
while (scanCharacters.hasNext())
{
temp+=scanCharacters.next();
}
while(temp.length() > 2){
list.add(temp.substring(0,3));
temp = temp.substring(3);
}
if(temp.length()>0){
list.add(temp);
}
scanCharacters.close();
return list;
}
In this example I read in all of the data from the file, and then parse it in groups of three. Scanner can never backtrack so using next will leave out some of the data the way you're using it. You are going to get groups of words (which are separated by spaces, Java's default delimiter) and then sub-stringing the first 3 letters off.
IE:
ALEXCY WOWZAMAN
Would give you:
ALE and WOW
The way my example works is it gets all of the letters in one string and continuously sub strings off letters of three until there are no more, and finally, it adds the remainders. Like the others have said, it would be good to read up on a different data parser such as BufferedReader. In addition, I suggest you research substrings and Scanner if you want to continue to use your current method.
This is my code. It produces the error java.util.NoSuchElementException.
It is meant to search a file, example.txt for a word (eg. and) and find all instances of the the word and print the word either side of it also (eg. cheese and ham, tom and jerry) in ONE JOptionPane. Code:
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class openFileSearchWord {
public static void main(String Args[])
{
int i=0,j=0;
String searchWord = JOptionPane.showInputDialog("What Word Do You Want To Search For?");
File file = new File("example.txt");
try
{
Scanner fileScanner = new Scanner(file);
String[] array = new String[5];
String[] input = new String[1000];
while (fileScanner.hasNextLine())
{
for(i=0;i<1000;i++)
{
input[i] = fileScanner.next();
if(input[i].equalsIgnoreCase(searchWord))
{
array[j] = input[i-1] + input[i] + input[i+1];
j++;
}
}
}
Arrays.toString(array);
JOptionPane.showMessageDialog(null, array);
fileScanner.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
It looks like you're assuming each line will have 1000 words.
while (fileScanner.hasNextLine())
{
for(i=0;i<1000;i++) <-------- Hardcoded limit?
{
....
}
}
You can try putting another catch loop, or check hasNext() during that for loop.
while (fileScanner.hasNextLine())
{
for(i=0;i<1000 && fileScanner.hasNext();i++)
{
....
}
}
There are also many issues with your code, like if input[i-1] hits the -1 index, or if your 'array' array hits the limit.
I took the liberty to have some fun.
Scanner fileScanner = new Scanner(file);
List<String> array = new ArrayList<String>();
String previous, current, next;
while (fileScanner.hasNext())
{
next = fileScanner.next()); // Get the next word
if(current.equalsIgnoreCase(searchWord))
{
array.add( previous + current + next );
}
// Shift stuff
previous = current;
current = next;
next = "";
}
fileScanner.close();
// Edge case check - if the last word was the keyword
if(current.equalsIgnoreCase(searchWord))
{
array.add( previous + current );
}
// Do whatever with array
....
I see a few error here ...
You are creating two arrays one with 5 and one with 1000 elements.
In your code you are referencing elements directly by index ... but this index might not be present.
input[i-1] ... what if i = 0? ...index is -1
array[j] ... what if j > 4 ... index 5 doesn't exist
I suggest using List of elements instead of fixed arrays.
List<String> array = new ArrayList<>();
You are assuming that the input is something but don't do anything to check what it actually is.
Just as Drejc told you, The first iteration would fail because of the negative index and the program will fail as well if it finds more than 5 matches of the desired word.
Also I want to add another one. You should think that when you do this line:
array[j] = input[i-1] + input[i] + input[i+1];
You have not assigned input[i+1] yet. In that iteration you've just assigned input[i], but no the next one.
You should process the concatenation of the three elements (previousWord + match + nextWord) when reaching nextWord.
Another solution, but inefficient, would be copying all the words to an Array at beginning and using your actual code without modifying. This would work, but you would go twice through all the words.
So I'm reading an input text file that is 4 lines of single-space separated numbers. The text file is just:
5
9 6
4 6 8
0 7 1 5
I'd like to input these numbers into a 2D array called rows. So for example, rows[1][0] should be 9, the first number in the second row.
My problem is when I use the Scanner for in.next() or in.nextInt(), it ignores the end of the line and just keeps on going, so rows[0][1] ends up being 9 instead of 0. I'd like for the array to finish populating with 0's if there is no number for that slot. So since the first row is only 1 number, 5, I'd like for the array rows[0][0] to be 5, but rows[0][1] to rows[0][3] to be 0.
I've tried using a try/catch for NoSuchElementException and an if in.hasNextLine() but neither of those seemed to work.
Any ideas or help would be greatly appreciated.
You can use a Scanner to read individual tokens, and you can use a Scanner to read an entire line, but you cannot use a Scanner to do both.
What you want to do is first read the line into a String, then use a Scanner on that String to parse that line, e.g.:
BufferedReader lineReader = new BufferedReader(...);
String line;
while ((line = lineReader.readLine()) != null) {
Scanner scanner = new Scanner(line);
// use scanner here
}
You can also use a Scanner to read lines instead of a BufferedReader, but unless you have a specific requirement (e.g. you're trying to drop this into code that already uses a Scanner), it doesn't really matter how you read the lines.
Hope that helps.
Use more than one Scanner. One Scanner gets each line with nextLine(), and then you feed the line obtained to the next Scanner analyzes each line's tokens. Just be sure to close() of the inner Scanner (and the outer one too) when done using it.
Pseudocode:
create fileScanner with file
while fileScanner has more lines
get next line from fileScanner
create new Scanner with next line, lineScanner
while lineScanner has next,
get next token.
do what you want with token.
close of lineScanner
close fileScanner
You should probably use a BufferedReader and split. The advantage of this is that you know how big to make your arrays in the second dimension as the split method will return an array and you can check its length:
public static void main(String[] args) throws Exception {
final String s = "5\n"
+ "9 6\n"
+ "4 6 8\n"
+ "0 7 1 5";
final InputStream is = new ByteArrayInputStream(s.getBytes());
final int[][] array = new int[4][];
try (final BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
for (int i = 0; (line = br.readLine()) != null; ++i) {
final String[] tokens = line.split("\\s");
final int[] parsed = new int[tokens.length];
for (int j = 0; j < tokens.length; ++j) {
parsed[j] = Integer.parseInt(tokens[j]);
}
array[i] = parsed;
}
}
System.out.println(Arrays.deepToString(array));
}
Output:
[[5], [9, 6], [4, 6, 8], [0, 7, 1, 5]]
As arrays don't expand it's not easy to use them in while loops that you don't know the size of. Using split allows you to simply do final int[] parsed = new int[tokens.length]; which with a Scanner over whitespace you could not do.
The first dimension size is hard coded however as said your file always has 4 lines.
At the end of each line, there is '\n' ,and at the first of each line(except first line), there is '\r'. You can control your file by them.
Scanner scanner = new Scanner(
"5\n9 6\n4 6 8\n0 7 1 5");
while (scanner.hasNextLine())
{
String currentline = scanner.nextLine();
String[] items = currentline.split(" ");
int[] intitems = new int[items.length];
for (int i = 0; i < items.length; i++)
{
intitem[i] = Integer.parseInt(items[i]);
}
}