Ok I have another homework problem. Essentially I am to make a text file of randomized characters ( leters bothe upper and lower case, numbers, spaces and specialized characters) and import it as a string into my program. From there I am to make all lowercase letters uppercase. I think I can handle the latter but the part I'm having issues with is bring in the file. I have looked through the forum and found some stuff about bufferereader and io exceptions thrown and null etc but that didn't make any sense plus that solution seemed very complicated for something that seems like it should be maybe 2 lines. I have some of the initial code:

package lab7a;

/**
 *
 * @author 24x24
 */

import java.util.Scanner;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         Scanner keyboard = new Scanner(System.in);
         
         String filePath = getFilePath();//gets the file path from user
         String random = getString(filePath);//finds string from the filepath
         String convert = convertString();//converts the lower case in string to uppercase
         String write = writeString();//rewrites file
    }
public static String getFilePath (){
    System.out.print("Enter the file name: ");
    Scanner keyboard = new Scanner(System.in);//user input for the file path
    String filePath = keyboard.next();
    return filePath;//returns filePath
    
}//end getFilePath

public static String getString (String filePath){
    
    //this is where I am currently stuck
    
}//end getString
}

Once I get the string from the file I am assuming I can just use toUpper and it will only change the letters that are lower case. If not I could put the string into an array or something and search the array for letters that are equivalent to ascii values from a-z convert then copy it all to a new array, output as string and then write to file. I'm pretty certain I can figure that one out either way. Its the read from file issue that I'm in need of some assistance.

Thanks in advance,
24x24

Ok I might be on to something but 2 things in what I'm doing: 1 readLine is crossed out? How is this possible and what does it mean? I'm using netbeans. And 2, I need to store the string but I'm struggling with that. I threw file in there but I know I need something in String format, I just don't know how to go from InputStream to String. This is what I have. Let me know if I'm close or completely off path:

public static String getString (String filePath){

   FileInputStream file;
        try {
            file = new FileInputStream(filePath);
            System.out.println( new DataInputStream (file).readLine() );
            file.close();
            //this is where I am currently stuck
        } catch (IOException e) {
            System.err.println ("Unable to read from file");
        System.exit(-1);}
        return file;


}//end getString

Ok I might be on to something but 2 things in what I'm doing: 1 readLine is crossed out? How is this possible and what does it mean? I'm using netbeans. And 2, I need to store the string but I'm struggling with that. I threw file in there but I know I need something in String format, I just don't know how to go from InputStream to String. This is what I have. Let me know if I'm close or completely off path:

public static String getString (String filePath){

   FileInputStream file;
        try {
            file = new FileInputStream(filePath);
            System.out.println( new DataInputStream (file).readLine() );
            file.close();
            //this is where I am assuming I would put the String variable
        } catch (IOException e) {
            System.err.println ("Unable to read from file");
        System.exit(-1);}
        return file;


}//end getString

Apologies for the double post. Will try to clean it up later

hey 24x24. you can loop the text file with while loop, but first you have to make file scanner, then import it in a while loop as while((nameofthescanner).hasNext()){ String lines = (nameofthescanner).nextLine();}

This is what I did according to what you suggested:

public static String getString(String filePath) {

        Scanner fileScanner = new Scanner (System.load(String filePath));
        while ((fileScanner).hasNext()) {
            String text = (fileScanner).nextLine();
            return text;
        }
    }//end getString

The code in red is giving an error. Its just saying
******
')' expected

';' expected

not a statement

cannot find symbol
symbol: variable String
location: class lab7a.Main
******

Im not too positive why considering everything is there that it is asking for. I think.

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


public class tesst {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		String text = "";
	    File file = new File("src/random.txt");
		Scanner fileScanner = new Scanner (file);
		        while (fileScanner.hasNext()) {
		            text = fileScanner.nextLine();
			        System.out.println(text);}
	}

}

heres how i would do it

File ofile = fc.getSelectedFile();
        			file = new File(yourfilepath);
        			FileInputStream fis = null;
        		    BufferedInputStream bis = null;
        		    DataInputStream dis = null;
        		    textArea.setText(null);
        		    try {
        		      fis = new FileInputStream(file);
        		      bis = new BufferedInputStream(fis);
        		      dis = new DataInputStream(bis);

        		      while (dis.available() != 0) {
        		    	  textArea.setText(textArea.getText() + dis.readLine());
        		      }
        		      fis.close();
        		      bis.close();
        		      dis.close();

        		    } catch (Exception ex){}

I figured it out. Thanks for the help!
The finished product (for learning purposes only, please write your own code for homework):

/*
 * This program read a txt file in, converts all lowercase letters to uppercase
 * and then saves them to a new file
 */
package lab7a;

/**
 *
 * @author 24x24
 */
import java.io.File;

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

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
        String filePath = getFilePath();//gets the file path from user
        String text = getString(filePath);//finds string from the filepath
        String converted = convertString(text);//converts the lower case in string to uppercase
        writeString(converted);//rewrites file
        
    }//end main method
/**
 * This method asks the user for the file path
 * Precondition
 * Postcondition filePath can't be null, file must exist in filePath
 * @return filePath
 */
    public static String getFilePath() {
        System.out.print("Enter the file path: ");
        Scanner keyboard = new Scanner(System.in);//user input for the file path
        String filePath = keyboard.nextLine();
        return filePath;//returns filePath

    }//end getFilePath
/**
 * This method takes a string from the selected file
 * Precondition filePath must be valid, file must contain string of characters
 * Postcondition text must be a string
 * @param filePath
 * @return text
 * @throws FileNotFoundException
 */
    public static String getString(String filePath) throws FileNotFoundException {
        String text = "";
        File file = new File(filePath);
        Scanner fileScanner = new Scanner(file);
        while (fileScanner.hasNext()) {
            text = fileScanner.nextLine();
            //Print out confirmation of file contents
            System.out.println("The contents of the input file string is...\n" + text);
        }
        return text;

    }//end getString

    /**
     * This method converts text lowercase characters to uppercase
     * Precondition text must be a string
     * Postcondition Converted will contain all uppercase letters
     * @param text
     * @return converted
     */
    public static String convertString(String text) {

        String converted = text.toUpperCase();
        return converted;
    }//end convertString

/**
 * This method writes converted text to a new file
 * Precondition converted must contain a string
 * Postcondition file must be written to a valid location
 * @param converted
 */
    public static void writeString(String converted) {
        try {
            PrintWriter out = new PrintWriter("newFile.txt");
            out.print(converted);
            out.close();
        } catch (FileNotFoundException ex) {
        }
        //Print out confirmation of conversion
        System.out.print("The contents of the converted string is...\n" + converted);

    }//end writeString 
}//end main class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.