Task 1- Stars.java Write a Java program called Stars.java that, firstly, prompts
ID: 3804675 • Letter: T
Question
Task 1- Stars.java Write a Java program called Stars.java that, firstly, prompts (asks) the user to enter an input file name. This is the name of a text file that can contain any number of records. A record in this file is a single line of text in the following format: Name Proper Name HR number HD number distance where Name is the common name of a star. This is a String (text) and may contain more than one word. This name is followed by a character, there are no spaces before or after the Proper Name is the proper name of a star. This is a String (text and may contain more than one word. This name is followed by a character, there are no spaces before or after the HR number is the HR classification number of the star. It is always the characters HR then a space and a number of digits, followed by a character there are no spaces before or after the HD number is the HD classification number of the star. It is always the characters HD then a space and a number of digits, followed by a character there are no spaces before or after the distance is the distance in light years from Earth. The format of this distance is always two digits after the decimal point, there may be between one and four digits to the left of the decimal point. An example of some of the lines of this file might be Acrux Alpha 1 Crucis HR 4730 HD 108248 320.00 Achird Eta Cassiopeiaen HR 219 HD 4614-19. 41 Ain Al Ramir Na 1 Sagittarii HR 7116- HD 174974 -1850.59 The input file may have 0 to any number of records. The format of the input file is guaranteed to be correct. Your program does not have to check the format Also, your program must work with any file name of the correct format. Do not hard code the file name.) Once this file has been opened, the user is then prompted for a proper star name. The program then reads through the file. If a matching proper star name is found in the file a all the information for that star is displayed to the screen. The order of display is shown in the example runs below. Proper star names are unique in the file, so there will be at most only one match User entered Proper star names must be case insensitive, that is, any combination of uppercase and lowercase letters must give the same result.Explanation / Answer
JAVA PROGRAM :
import java.io.*;
public class Stars {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the file name");
File f = new File(br.readLine().trim());
if(!f.exists()){
System.out.println(f.getName()+" is an empty file");
return;
}
BufferedReader bf = new BufferedReader(new FileReader(f));
System.out.println("Enter the star's proper name ");
String name = br.readLine().trim();
String s ;
while((s=bf.readLine())!=null){
String d[] = s.trim().split("~");
if(d[1].trim().equals(name)){
System.out.println("Star : proper name : "+d[1]+" distance : "+d[4]+" light years HD num : "+d[3]+" HR num : "+d[2]+" common name : "+d[0]);
return;
}
}
System.out.println("No star with the name "+name+" was found");
}
}
OUTPUT 1 :
Enter the file name
P:/qns.txt
Enter the star's proper name
Alpha 1 Crucis
Star : proper name : Alpha 1 Crucis distance : 320.00 light years HD num : HD 108248 HR num : HR 4730 common name : Acrux
OUTPUT 2 :
Enter the file name
P:/star
star is an empty file
OUTPUT 3 :
Enter the file name
P:/qns.txt
Enter the star's proper name
proxima centurai
No star with the name proxima centurai was found
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.