Asked  6 Months ago    Answers:  5   Viewed   251 times

I got an run time exception in my program while I am reading a file through a Scanner.

java.util.NoSuchElementException: No line found     
   at java.util.Scanner.nextLine(Unknown Source)    
   at Day1.ReadFile.read(ReadFile.java:49)  
   at Day1.ParseTree.main(ParseTree.java:17) 

My code is:

while((str=sc.nextLine())!=null){
    i=0;
    if(str.equals("Locations"))
    {
        size=4;
        t=3;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Professions"))
    {
        size=3;
        t=2;
        str=sc.nextLine();
        str=sc.nextLine();
    }
    if(str.equals("Individuals"))
    {
        size=4;
        t=4;
        str=sc.nextLine();
        str=sc.nextLine();
    }

int j=0;
String loc[]=new String[size];
while(j<size){
    beg=0;
    end=str.indexOf(',');
    if(end!=-1){
        tmp=str.substring(beg, end);
        beg=end+2;
    }
    if(end==-1)
    {
        tmp=str.substring(beg);
    }
    if(beg<str.length())
        str=str.substring(beg);
    loc[i]=tmp;
    i++;

    if(i==size ){
        if(t==3)
        {
            location.add(loc);
        }
        if(t==2)
        {
            profession.add(loc);
        }
        if(t==4)
        {
            individual.add(loc);
        }
        i=0;
    }
    j++;
    System.out.print("n");
}

 Answers

19

with Scanner you need to check if there is a next line with hasNextLine()

so the loop becomes

while(sc.hasNextLine()){
    str=sc.nextLine();
    //...
}

it's readers that return null on EOF

ofcourse in this piece of code this is dependent on whether the input is properly formatted

Tuesday, June 1, 2021
 
MKM
answered 6 Months ago
MKM
83

You are not setting the layout_weight property. Your code reads weight="1" and it should read android:layout_weight="1".

Tuesday, June 1, 2021
 
ManojGeek
answered 6 Months ago
37

After you call hasNext() the first time if you don't read from the file hasNext() will always return true. Because the front of the input doesn't change.

Imagine you have a file with this line in it:

this is input

If you call hasNext() on this file, it will return true because there is a next token in the file, in this case the word this.

If you don't read from the file after this initial call, the "next" input to be processed is STILL the word this. The next input doesn't change until you read from the file.

TL;DR

When you call hasNext() read from the file, otherwise you will always have an infinite loop.

Additionally

If you really want to use hasNext(), or would like to, you could create another Scanner object and read through the file to count the lines, then your loop would work fine. also, you should really use hasNextLine()

public int countLines(File inFile)
{
   int count = 0;
   Scanner fileScanner = new Scanner(inFile);

   while(fileScanner.hasNextLine()) //if you are trying to count lines
   {                                //you should use hasNextLine()
       fileScanner.nextLine() //advance the inputstream
       count++;
   }

   return count;
}

Hope this is helpful.

Sunday, July 4, 2021
 
DMTintner
answered 5 Months ago
15

hasNextLine() calls findWithinHorizon() which in turns calls findPatternInBuffer(), searching a match for a line terminator character pattern defined as .*(rn|[nru2028u2029u0085])|.+$

Strange thing is that with both ways to construct a Scanner (with FileInputStream or via File), findPatternInBuffer returns a positive match if the file contains (independently from file size) for instance the 0x0A line terminator; but in the case the file contains a character out of ascii (ie >= 7f), using FileInputStream returns true while using File returns false.

Very simple test case:

create a file which contains just char "a"

# hexedit file     
00000000   61 0A                                                a.

# java Test.java
using File: true
using FileInputStream: true

now edit the file with hexedit to:

# hexedit file
00000000   61 0A 80                                             a..

# java Test.java
using File: false
using FileInputStream: true

in the test java code there is nothing else than what already in the question:

import java.io.*;
import java.lang.*;
import java.util.*;
public class Test {
    public static void main(String[] args) {
        try {
                File file1 = new File("file");
                Scanner s1 = new Scanner(file1);
                System.out.println("using File: "+s1.hasNextLine());
                File file2 = new File("file");
                Scanner s2 = new Scanner(new FileInputStream(file2));
                System.out.println("using FileInputStream: "+s2.hasNextLine());
        } catch (IOException e) {
                e.printStackTrace();
        }
    }
}

SO, it turns out this is a charset issue. In facts, changing the test to:

 Scanner s1 = new Scanner(file1, "latin1");

we get:

# java Test 
using File: true
using FileInputStream: true
Thursday, July 29, 2021
 
oroshnivskyy
answered 4 Months ago
69

Your primary LinearLayout is set to Horizontal so the first 5 text view and the layout2 are shown on the same line. Adding Layout3 to Layout2 makes the Layout3 to be shown from the right of the last text view from primary Linear Layout. On a 10 inch tablet i see only the first 2 elements of your LinearLayout. Perhaps on a smaller screen you don't see them. Try using

text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT));

instead of

text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT));

and you should see all your text views.

EDIT : In your case this should do the trick; xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/dyn_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dip" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/dyn_layout2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:visibility="gone"
            android:padding="10dip" >
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/dyn_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add TextViews" />

</LinearLayout>

and code :

@Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.dyn_button1:
            LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout);
            LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2);
            LinearLayout layout3 = null;
            for (int i = 0; i < 10; i++) {
                if (i > 4) {
                    if (i == 5) {
                        layout2.setPadding(10, 60, 10, 10);
                        layout3 = new LinearLayout(this);
                        layout3.setLayoutParams(new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                        layout3.setOrientation(LinearLayout.HORIZONTAL);
                        layout3.setPadding(10, 10, 10, 10);
                    }
                    System.out.println("**** Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout3.addView(text);

                    if (i == 9) {
                        System.out
                                .println("Added second linear layout to first");
                        layout2.setVisibility(View.VISIBLE);
                        layout2.addView(layout3);
                    }
                } else {
                    System.out.println("###### Adding text view " + i);
                    TextView text = new TextView(this);
                    text.setText("The Value of i is :" + i);
                    text.setTextSize(12);
                    text.setGravity(Gravity.LEFT);
                    text.setLayoutParams(new LayoutParams(155,
                            LayoutParams.WRAP_CONTENT));
                    layout.addView(text);
                }
            }

        }

    }
Sunday, October 31, 2021
 
Arseny Istlentyev
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share