Line by line reading

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 451 Views - Last Post: 31 October 2017 - 07:09 AM Rate Topic: -----

#1 showdawg04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-October 17

Line by line reading

Posted 29 October 2017 - 10:09 PM

 
public static void processFile (boolean obscure, Scanner input, PrintStream output){
    while(input.hasNextLine()) {
        String line = input.nextLine();



I am trying to read each line of a file and in this method I want to do something to the file depending on the boolean value then print that to an output file. I know I need this so far, but am confused on what else to write. I know this isn't a code writing service. I am just asking for some guidance and maybe a basic structure for how to do this. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Line by line reading

#2 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,476
  • Joined: 13-June 14

Re: Line by line reading

Posted 29 October 2017 - 10:13 PM

You haven't told us what the something is you want to do when you read each line. You'll need to give more details in order for us to help you. It would also help to know how you're thinking of approaching the problem you're trying to solve. You at least need to put some effort in there.
Was This Post Helpful? 0
  • +
  • -

#3 showdawg04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-October 17

Re: Line by line reading

Posted 29 October 2017 - 10:25 PM

If obscure is true, obscures map in input and outputs obscured map.
If obscure is false, uncovers map in input and outputs uncovered map.

public static void processFile (boolean obscure, Scanner input, PrintStream output){
for every line of input
either obscure or uncover it
print it to the output file
}

This is the structure I am intending to use, just not sure how to do that.

This post has been edited by ndc85430: 29 October 2017 - 10:27 PM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,476
  • Joined: 13-June 14

Re: Line by line reading

Posted 29 October 2017 - 10:31 PM

View Postshowdawg04, on 30 October 2017 - 05:25 AM, said:

If obscure is true, obscures map in input and outputs obscured map.
If obscure is false, uncovers map in input and outputs uncovered map.


I have no idea what you mean by this. Please explain what "obscure" means in this context.

Also, there's no need to quote the previous post in its entirety - just use the "Reply" button in future please.
Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Line by line reading

Posted 29 October 2017 - 11:04 PM

you would need to do something like this

if(obscure)
{

// obscure map
}
else
{

// show map
}


@ndc85430 'obscure' is a boolean ie boolean obscure....... obscure in English means to hide or cover something
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,476
  • Joined: 13-June 14

Re: Line by line reading

Posted 29 October 2017 - 11:36 PM

Of course I know that, but what I was getting at was what does it mean in this context? Cover up the text in what way?
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Line by line reading

Posted 30 October 2017 - 12:59 AM

Well presumably the OP knows how he is going to show the 'map' and how he is going to cover up the 'map'

whatever the 'map' read from the file is...


And I was only trying to help ndc... I thought you had missed the 'boolean obscure' in the function definition.. wasn't having a go, I think you're great... :)
Was This Post Helpful? 0
  • +
  • -

#8 showdawg04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-October 17

Re: Line by line reading

Posted 30 October 2017 - 01:39 PM

import java.util.*;
import java.io.*;
public class MapChanger {
    public static final String VALID_CHARACTERS = "+/\\~X";
    public static final String OBSCURE_CHARACTERS = "!@#$%^&*[]=";
    public static final Random RAND = new Random();
    public static void main(String args []) throws FileNotFoundException {
        Scanner console = new Scanner(System.in); 
        String option = "";
        boolean obscure = false;
        while(!(option.equals("O") || option.equals("o") || option.equals("U") || option.equals("u") || option.equals("Q") || option.equals("q"))) {
            System.out.println("Enter O-bscure, U-ncover, or Q-uit: ");
            option = console.next();
            if(option.equals("O") || option.equals("o")) {
                obscure = true;
                Scanner input = getInputScanner(console); 
                PrintStream output = getOutputPrintStream(console);
            }
            if(option.equals("U") || option.equals("u")) {
                obscure = false;
                Scanner input = getInputScanner(console); 
                PrintStream output = getOutputPrintStream(console);
            }
            if(option.equals("Q") || option.equals("q")) {
                System.exit(0);
            }
            else {
                System.out.println("Invalid Action");
            }

        }

        
    }
    //Repeatedly prompts the user for the name of an input file until the user enters
    //the name of an existing file; then creates and returns a Scanner for the input file.
    //See the lecture notes for your section and/or the textbook for examples.
    //Use a try/catch block to catch and handle any FileNotFoundException's that occur
    public static Scanner getInputScanner(Scanner console){
        Scanner result = null;     
        while (result == null) {
            System.out.print("Enter input file: ");
            String name = console.next();
            try {
                result = new Scanner(new File(name));
            } 
            catch (FileNotFoundException e) {
                System.out.println(name + "(No such file or directory)");
                
            }
         }
         return result;
     }

    //Prompts the user for the name of an output file. 
    //If the file does not exist, creates and returns a PrintStream for the output file.
    //If the file does exist, prints an error message and exits the program OR
    //does one of the Extra Credit options described below.
    //Use a try/catch block to catch and handle any FileNotFoundException's that occur
    public static PrintStream getOutputPrintStream(Scanner console){
        System.out.print("Enter output file: ");
        String filename = console.next();
        File file = new File(filename);
        PrintStream output = null;
        if(!file.exists()) {
            try {
                output = new PrintStream(file);
            }
            catch (FileNotFoundException e) {
                System.out.println("File exists");
                System.exit(0);
            }
            
        }
        return output;
      
    }
    //  
    //If obscure is false, uncovers map in input and outputs uncovered map.
    public static void processFile (boolean obscure, Scanner input, PrintStream output){
        while(input.hasNextLine()) {
            String line = input.nextLine();
            if (obscure = true) {
                obscureLine(line);
            }
            if (obscure = false) {
                uncoverLine(line);
            }
        }
              
    }
    //Returns string containing obscured line.
    public static String obscureLine(String line){
        String s = "";
            for(int i = 0; i < line.length(); i++) {
                char ch = line.charAt(i);
                char randChar = getRandomCharacter();
                if (!isValidCharacter(ch)) {
                    s += randChar; 
                }
                else {
                    s += ch;
                }
            }
        return s;

    }
    //Returns string containing uncovered line.
    public static String uncoverLine(String line){
        String s = "";
            for(int i = 0; i < line.length(); i++) {
                char ch = line.charAt(i);
                if(isValidCharacter(ch)) {
                    s += ch;
                }
                else {
                    s += "";
                }
                
            }
        return s;

    }
    //Returns true if ch is one of the following five characters: 
    // + / \ ~ X
    //Returns false otherwise
    public static boolean isValidCharacter(char ch){
        if (ch == '+' || ch == '/' || ch == '\\' || ch == '~' || ch == 'X' ) {
            return true;
            
        }
        return false;
        

    }
    //Randomly returns one of the following eleven characters:
    // ! @ # $ % ^ & * [ ] = 
    public static char getRandomCharacter(){
        char randomCharacter = OBSCURE_CHARACTERS.charAt(RAND.nextInt(OBSCURE_CHARACTERS.length()));
        return randomCharacter;

    }

}





This code is supposed to uncover or obscure an input file, but when I run the program it will create the output file, but not have the correct contents. Also after entering in the name of the output file I get the message "Invalid Action" which is in my top while loop. Any help would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#9 Programmer2004  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 25-October 17

Re: Line by line reading

Posted 30 October 2017 - 02:09 PM

The reason of getting "Invalid Action" message is that every "if" runs separately, so your code does something like this:

Check if user selected O, if yes, then load from user input
Check if user selected U, if yes, then load from user input
Check if user selected Q, if yes, then exit program, otherwise print "Invalid Action"

To correct this, you should do this:

            if(option.equals("O") || option.equals("o")) {
                obscure = true;
                Scanner input = getInputScanner(console); 
                PrintStream output = getOutputPrintStream(console);
            } else {
                if(option.equals("U") || option.equals("u")) {
                    obscure = false;
                    Scanner input = getInputScanner(console); 
                    PrintStream output = getOutputPrintStream(console);
                } else {
                    if(option.equals("Q") || option.equals("q")) {
                        System.exit(0);
                    } else {
                        System.out.println("Invalid Action");
                    }
                }
            }



If talking about invalid result, I guess that the reason is bug in processFile function, in "if" statements:

if (obscure = true) {

    obscureLine(line);

}

if (obscure = false) {

    uncoverLine(line);

}



It has to be == instead of =.
Was This Post Helpful? 0
  • +
  • -

#10 showdawg04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-October 17

Re: Line by line reading

Posted 30 October 2017 - 02:57 PM

Thanks for the help. That still didn't fix the nothing in the output file though.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,157
  • Joined: 27-December 08

Re: Line by line reading

Posted 30 October 2017 - 03:34 PM

Merged duplicate threads. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,309
  • Joined: 20-March 10

Re: Line by line reading

Posted 30 October 2017 - 07:11 PM

Sweet Mother Of God, that is some fugly looking code for Java....

The main trap you are falling into is you are applying functional programming techniques to a true OOP language like Java, why not take advantage of Java's true OOP nature and pass some objects around instead of trying to beat Java to death to suit your own functional programming style.. Java is not a functional programming language and it doesn't take kindly to you beating it into one.

I will go slowly...though with your idiom.. although I would rather not...

and answer your questions one by one...

Question 1 why does it show invalid action for all actions apart from 'Q' or 'q'

import java.util.*;
import java.io.*;
public class MapChanger {
    public static final String VALID_CHARACTERS = "+/\\~X";
    public static final String OBSCURE_CHARACTERS = "!@#$%^&*[]=";
    public static final Random RAND = new Random();
    public static void main(String args []) throws FileNotFoundException {
        Scanner console = new Scanner(System.in); 
        String option = "";
        boolean obscure = false;
        while(!(option.equals("O") || option.equals("o") || option.equals("U") || option.equals("u") || option.equals("Q") || option.equals("q"))) {
            System.out.println("Enter O-bscure, U-ncover, or Q-uit: ");
            option = console.next();
            if(option.equals("O") || option.equals("o")) {
                obscure = true;
                Scanner input = getInputScanner(console); 
                PrintStream output = getOutputPrintStream(console);
            }
            if(option.equals("U") || option.equals("u")) {
                obscure = false;
                Scanner input = getInputScanner(console); 
                PrintStream output = getOutputPrintStream(console);
            }
            if(option.equals("Q") || option.equals("q")) {
                System.exit(0);
            }
            else {
                System.out.println("Invalid Action");
            }

        }

        
    }
    




answer: because that is what you told it to do...

a series of if() else if() else if() else() statements
instead of if() if() if() else() where only the last if is considered for the else() will sort it out.

In processFile you are setting the value of obscure to false with if(obscure = true).......then you say... if(obscure=false)

remember == for comparison not as you are doing = for assignment

in getRandomCharacter you have this line

OBSCURE_CHARACTERS.charAt(RAND.nextInt(OBSCURE_CHARACTERS.length()));



when clearly it should be this

OBSCURE_CHARACTERS.charAt(RAND.nextInt(OBSCURE_CHARACTERS.length()-1));

Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3443
  • View blog
  • Posts: 15,601
  • Joined: 20-September 08

Re: Line by line reading

Posted 31 October 2017 - 01:50 AM

Quote

when clearly it should be this

Actually - no. Check the API docs for Random.nextInt and what it includes

Please post your current code with other corrections
Was This Post Helpful? 0
  • +
  • -

#14 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,476
  • Joined: 13-June 14

Re: Line by line reading

Posted 31 October 2017 - 03:53 AM

Quote

The main trap you are falling into is you are applying functional programming techniques to a true OOP language like Java ...


I don't see any evidence of a functional style used in the code there. Having said that, I think it is possible to write Java in a functional way - Java 8 has introduced features that facilitate that (streams, lambdas and method references and Optional, for example).

This post has been edited by g00se: 31 October 2017 - 05:25 AM
Reason for edit:: added quotation

Was This Post Helpful? 0
  • +
  • -

#15 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: Line by line reading

Posted 31 October 2017 - 05:50 AM

A few random observations:

* checking those letters in while is useless.. you could use a control variable defined outside the loop like boolean repeat = true; if any option is chosen, you can modify it to false (if you want to exit the loop)

* if you want to check for both uppercase and lowercase strings use str.equalsIgnoreCase(str2)

* variables declared within blocks are local variables that won't exist outside the blocks, it happens here with input and output:

if (option.equals("O") || option.equals("o")) {
    obscure = true;
    Scanner input = getInputScanner(console);
    PrintStream output = getOutputPrintStream(console);
}


You'd have to declare those variables outside not just the block, but also outside(before) the loop... however, what you're trying to do doesn't seem to make sense...

* obscureLine(line); and uncoverLine(line); won't do anything as their return values are not assigned to any variable... besides, line is immutable(as are all strings) so you can't modify

* if you want to check whether a character exists in a string you could use str.indexOf(char); the way you created your isValidCharacter function, rendered VALID_CHARACTERS useless... you could at least do:
public static boolean isValidCharacter(char ch) {
    for (char c: VALID_CHARACTERS.toCharArray())
        if (c == ch) return true;
    return false;
}

or:
public static boolean isValidCharacter(char ch) {
    return VALID_CHARACTERS.indexOf(ch) < 0 ? false : true;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2