Results 1 to 8 of 8
  1. #1
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Need Help with reversing a string of file type in java

    Here is the code. Im trying to create a method that will reverse the file once user selects option 3. If user selects 0, program will Exit. If user selects 1, user selects file but program doesnt exit. user makes another selection. If user selects 2, program displays file, and if user selects 3, it reversed the file that was displayed... If file not found, then error message...

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    public class Menu {
    private static int output;

    public static void main(String[] args) throws FileNotFoundException
    {
    //int choice;
    File F=new File(" ");

    Scanner input=new Scanner(System.in);
    int choice=menu(output);

    switch(choice){
    case 0:
    System.exit(0);

    case 1:
    // select(String fileName);
    System.out.println("Enter filename: ");
    Scanner input2 = new Scanner(System.in);
    String fileName = input2.nextLine();
    menu(output);

    case 2:
    String filename;
    System.out.print("Enter filename: ");
    Scanner input3 = new Scanner(System.in);
    filename=input3.next();
    //menu(output);
    File f=new File(filename);
    System.out.println(f);


    }
    }
    /*
    public static select(File f, Scanner input2)
    {

    String filename;
    System.out.print("Enter filename: ");
    input2 = new Scanner(System.in);
    filename=input2.next();
    f=new File(filename);
    return filename;
    menu(output);

    }*/

    public static int menu(int output)
    {
    System.out.println("Enter 0-4");
    Scanner input = new Scanner(System.in);
    output =input.nextInt();
    return output;
    }

    }

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Need Help with reversing a string of file type in java

    ^Please put your code inside [code] tags.

  3. #3
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Re: Need Help with reversing a string of file type in java

    Java Code:
     import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    public class Menu {
    private static int output;
    
    public static void main(String[] args) throws FileNotFoundException
    {
    //int choice;
    File F=new File(" ");
    
    Scanner input=new Scanner(System.in);
    int choice=menu(output);
    
    switch(choice){
    case 0:
    System.exit(0);
    
    case 1:
    // select(String fileName);
    System.out.println("Enter filename: ");
    Scanner input2 = new Scanner(System.in);
    String fileName = input2.nextLine();
    menu(output);
    
    case 2:
    String filename;
    System.out.print("Enter filename: ");
    Scanner input3 = new Scanner(System.in);
    filename=input3.next();
    //menu(output);
    File f=new File(filename);
    System.out.println(f);
    
    
    }
    }
    /*
    public static select(File f, Scanner input2)
    {
    
    String filename;
    System.out.print("Enter filename: ");
    input2 = new Scanner(System.in);
    filename=input2.next();
    f=new File(filename);
    return filename;
    menu(output);
    
    }*/
    
    public static int menu(int output)
    {
    System.out.println("Enter 0-4");
    Scanner input = new Scanner(System.in);
    output =input.nextInt();
    return output;
    }
    
    }

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Need Help with reversing a string of file type in java

    Do you want to reverse name of that file or you want to reverse text in that file, or maybe one line of that textual file?

    Why don't you have break; in cases? For example:

    case 1:
    // select(String fileName);
    System.out.println("Enter filename: ");
    Scanner input2 = new Scanner(System.in);
    String fileName = input2.nextLine();
    menu(output);
    break;
    Last edited by cselic; 07-17-2012 at 01:19 AM.

  5. #5
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Re: Need Help with reversing a string of file type in java

    Quote Originally Posted by cselic View Post
    Do you want to reverse name of that file or you want to reverse text in that file, or maybe one line of that textual file?

    Why don't you have break; in cases? For example:

    case 1:
    // select(String fileName);
    System.out.println("Enter filename: ");
    Scanner input2 = new Scanner(System.in);
    String fileName = input2.nextLine();
    menu(output);
    break;
    I need to reverse the contents in the file.....

  6. #6
    anonb is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Re: Need Help with reversing a string of file type in java

    oh and the reason i didn't put a break in it is because I need the program to continue to run until the user selects 0 to Exit....

  7. #7
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Need Help with reversing a string of file type in java

    You should have the break; at the end of each case. To make sure the program keeps running you can make it look like this
    Java Code:
    boolean keepRunning = true;
    while(keepRunning)
    {
    // SWITCH STATEMENT HERE
    //WHEN 0 IS SELECTED 
    keepRunning = false;
    }
    then you can add a default case at the end, just in case someone puts in something other then the given choices.

    As for the reverse file, is it like the file contains

    "the cat"

    so you return it as

    "cat the"

    or

    "tac eht"

  8. #8
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default Re: Need Help with reversing a string of file type in java

    Well, here is the method.

    • Dump contents of a file into a String.
    • Use a function to reserves the text in the String.
    • Overwrite file with the String you just created.


    Make sense?

    You can actually find all the code you need using google. Remember, Google is your friend. You just have to ask he/she the right question.

    3-Ways to Reverse a String in Java « Brilliant Sheep
    That will help with the reversing the strings.

    Reading, Writing, and Creating Files (The Java™ Tutorials > Essential Classes > Basic I/O)
    This will teach you how to use Java Input/Output classes to create a file.

    Put those together and you have exactly what you need.
    My API:
    Java Code:
    cat > a.out || cat > main.class

Similar Threads

  1. reversing words of a string using stacks
    By mamoonrizwan in forum New To Java
    Replies: 1
    Last Post: 11-15-2011, 09:55 PM
  2. Reversing the String
    By Inaam in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 09:35 PM
  3. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 11:43 AM
  4. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 10:42 PM
  5. reversing a string
    By toad in forum New To Java
    Replies: 5
    Last Post: 11-07-2007, 10:13 AM

Posting Permissions

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