Question

In: Computer Science

Write a java code segment to declare an array of size 10 of type String and...

Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFile {

    public static void main(String[] args) {
        String[] lines = new String[10];

        System.out.print("Enter file name: ");
        Scanner in = new Scanner(System.in);
        int numLines = 0;

        File file = new File(in.nextLine());    // read a file and create a file.
        try {
            Scanner fin = new Scanner(file);    // create a scanner to read from file
            while (fin.hasNextLine() && numLines < lines.length) { // read each line from file
                lines[numLines++] = fin.nextLine();
            }

            // print array
            System.out.println("Lines read from the file are");
            for (int i = 0; i < numLines; i++) {
                System.out.println(lines[i]);
            }

            fin.close();    // close file.
        } catch (FileNotFoundException e) {
            System.out.println("Error. " + file.getAbsolutePath() + " does not exists!");   // if file does not exist, report an error
        }
    }
}

Related Solutions

Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
Write a simple Java program that does the following: 1) Declare a constant of type String...
Write a simple Java program that does the following: 1) Declare a constant of type String to hold the words "Oakland University". 2) Declare variables of the type stated, and Prompt the user to enter in the following information and store in the variables a. Their current GPA on a 4.0 scale, into a variable of type double b. The number of credits they have so far into a variable of type int c. The amount of tuition they paid...
write a java prog that include an array of strings of size 50    String[] strings...
write a java prog that include an array of strings of size 50    String[] strings = new String[50]; then find the max length of the inputs inside a method keep reading from the user until user enters keyword to stop input : may ala jony ram asd fgghff daniel jwana output : max length : 10
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String...
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String variable called myString1 and initialize it to "Programming". C) Declare a String variable called myString2 and initialize it to "Language". D) Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".". E) Print the sum of the lengths of myString1 and myString2 (must call the length() method). F) Print the 2nd, 4th, and 7th character...
1.Write the java code to create a two dimensional String array of sizes 12 by 8;...
1.Write the java code to create a two dimensional String array of sizes 12 by 8; Given the java array:       double[] test = new double[3]; 2.  Write the java code to put the numbers 1.0, 2.0, and 3.0 in to the array so that the 1.0 goes in the first cell, the 2.0 goes in the second cell and the 3.0 goes in the third cell. 3. Can you have different types of data is a three dimensional array? 4....
C++ Write a program to declare an array of double of size 25, ask the user...
C++ Write a program to declare an array of double of size 25, ask the user to input all array elements from the keyboard, then write a function named out_of_order that will test this array for the condition a[0] >= a[1] >= a[2] >= ... > a[24] The function returns a -1 if the elements are not out of order, otherwise it returns the index of the first element that is out of order. Explain what you do to avoid...
Provide the code segment need to DECLARE an array of structures that could store information about a group of US Passports.
Provide the code segment need to DECLARE an array of structures that could store information about a group of US Passports. Do not write a program or worry about initializing the structure, all I am looking for is a set of structure types whose combined members would hold all the information found in a password, AND any supporting structures. Be careful on how you declare the members of a structure. Grading will be based on the flexibility of your design...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
given an array, write code to scan the array for a particular purpose . use java
given an array, write code to scan the array for a particular purpose . use java
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT