import java.io.File; import java.io.FileNotFoundException; import java.util.Arra
ID: 3840019 • Letter: I
Question
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
// Programmer: Sammy Student
public class DataApplication
{
public static void searchData(ArrayList<String> vals)
{
System.out.print("enter a name: ");
Scanner sc = new Scanner(System.in);
String strName = sc.nextLine().trim();
boolean found = false;
for (int i = 0; i < vals.size(); i++)
{
if(vals.get(i).equals(strName.trim()))
{
found = true;
break;
}
}
if(found == true)
System.out.println(" data found ");
else
System.out.println(" data not found ");
sc.close();
}
public static void main(String[] args)
{
try
{
File fin = new File("data.txt");
Scanner scan = new Scanner(fin);
ArrayList<String> theData = new ArrayList<String>();
// read the column headings from the flat text file
String line = scan.nextLine();
while(scan.hasNextLine())
{
line = scan.nextLine();
String[] list = line.split("");
int key = Integer.parseInt(list[0]);
String name = list[1];
int fee = Integer.parseInt(list[2]);
String specialty = list[3];
theData.add(String.valueOf(key));
theData.add(name);
theData.add(String.valueOf(fee));
theData.add(specialty);
}
int count = 1;
for (int i = 0; i < theData.size(); i++)
{
System.out.print(theData.get(i) + " ");
if(count % 4 == 0 )
System.out.println(" ");
count++;
}
scan.close();
System.out.println(theData);
searchData(theData);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
//
error
run:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at DataApplication.main(DataApplication.java:75)
C:Usersris 2AppDataLocalNetBeansCache8.2executor-snippets un.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Explanation / Answer
Hi
Here the issue is unable to loop the file data.txt. Please place data.txt file and this java file in same folder and try to run the program then it will work.
I have ran this program at my side with sample data. it is working fine
DataApplication.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
// Programmer: Sammy Student
public class DataApplication
{
public static void searchData(ArrayList<String> vals)
{
System.out.print("enter a name: ");
Scanner sc = new Scanner(System.in);
String strName = sc.nextLine().trim();
boolean found = false;
for (int i = 0; i < vals.size(); i++)
{
if(vals.get(i).equals(strName.trim()))
{
found = true;
break;
}
}
if(found == true)
System.out.println(" data found ");
else
System.out.println(" data not found ");
sc.close();
}
public static void main(String[] args)
{
try
{
File fin = new File("D:\data.txt");
Scanner scan = new Scanner(fin);
ArrayList<String> theData = new ArrayList<String>();
// read the column headings from the flat text file
String line = scan.nextLine();
while(scan.hasNextLine())
{
line = scan.nextLine();
String[] list = line.split(" ");
int key = Integer.parseInt(list[0]);
String name = list[1];
int fee = Integer.parseInt(list[2]);
String specialty = list[3];
theData.add(String.valueOf(key));
theData.add(name);
theData.add(String.valueOf(fee));
theData.add(specialty);
}
int count = 1;
for (int i = 0; i < theData.size(); i++)
{
System.out.print(theData.get(i) + " ");
if(count % 4 == 0 )
System.out.println(" ");
count++;
}
scan.close();
System.out.println(theData);
searchData(theData);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
Output:
22 Suresh 44 55
33 Sekhar 33 444
44 anshu 11 8
[22, Suresh, 44, 55, 33, Sekhar, 33, 444, 44, anshu, 11, 8]
enter a name: Sekhar
data found
data.txt
header
22 Suresh 44 55
33 Sekhar 33 444
44 anshu 11 8
In this code, i have placed data.txt file in D drive.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.