Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Norm

Thread: Reading from text file

  1. #1
    Zaadyn is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Reading from text file

    Hello everyone,

    I'm in a beginner level Java course, and I'm having a little bit of a problem.

    I need to write a program that uses a while loop to read data from a text file called 'test.txt'. Each line in the text file is a string containing two integers in the format "number|number", and there are one hundred lines of these strings. The program then needs to compare the numbers in a certain way, but I know how to do that. I already wrote a method (determineRisk()) that does the job on a single string, but what I need is a new method that calls on determineRisk() to process each string in the file. My instructor says to run the program by using "java
    programName < test.txt" in the command prompt.

    Here's the method that processes one string:
    Java Code:
    public String determineRisk(String record)
    	{
    		int bar = record.indexOf('|');
    		String paymentString = record.substring(0,bar);
    		String incomeString = record.substring(bar + 1, record.length());
    		double payment = Double.valueOf(paymentString);
    		double income = Double.valueOf(incomeString);
    		String risk = "";
    		
    		if (payment > (income * 0.3))
    		{
    			risk = "risky";
    		}
    		else
    		{
    			risk = "safe";
    		}
    		return risk;
    	}
    And here's how I call on determineRisk() in the main method:
    Java Code:
    Scanner scan = new Scanner(System.in);
    String record = scan.nextLine();
    String risk = fc.determineRisk(record);
    Any help will be greatly appreciated!!

  2. #2
    Zaadyn is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Reading from text file

    It might also be important to mention that we haven't started using arrays yet in the course.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,267
    Rep Power
    25

    Default Re: Reading from text file

    I need is a new method that calls on determineRisk()
    How is this new method called?
    What does it do besides calling the determineRisk method?
    You need to define what the method is supposed to do before you can call it.
    What parameters will be passed to it
    what task will it do
    and what will it return
    DarrylBurke likes this.

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. Help reading a text file.
    By Kristoba in forum New To Java
    Replies: 5
    Last Post: 03-11-2010, 08:42 AM
  3. Reading in a text file
    By TheRealHoff in forum AWT / Swing
    Replies: 10
    Last Post: 02-07-2010, 11:47 PM
  4. reading text file
    By trofyscarz in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 02:24 AM
  5. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •