Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Java run file as batch and write problem to TXT file

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java run file as batch and write problem to TXT file

    Hello,

    I have got a problem with running the java program with batch file.
    The program works well when I run with NetBeans or Eclipse.
    When I want to add a property to List class with useDelimiter"\n" the batch file can't process it well and I got breaking line in the TXT file when I want to write it.

    This is my method for addig car to the list:

    private static void autokHozzaadasa() throws IOException, InterruptedException {
            sikeres = false;
            Scanner beMarkaMegnev = new Scanner(System.in).useDelimiter("\n");
            Scanner be = new Scanner(System.in);
            String marka = null;
            String rendszam = null;
            String kobcenti = null;
            String evszam = null;
            String ertek = null;
            System.out.println("\nAutó hozzáadása!");
            while (!sikeres) {
                System.out.print("Autó megnevezése: ");
                marka = beMarkaMegnev.next().toUpperCase();
                System.out.print("Rendszám: ");
                rendszam = be.next().toUpperCase();
                System.out.print("Köbcenti: ");
                kobcenti = be.next();
                System.out.print("Évszám: ");
                evszam = be.next();
                System.out.print("Ára: ");
                ertek = be.next();
                if (vizsgal(kobcenti) && vizsgal(evszam) && vizsgal(ertek)) {
                    sikeres = true;
                } else {
                    sikeres = false;
                }
                for (Autok n : autok) {
                    if (n.getRendszam().equals(rendszam)) {
                        sikeres = false;
                    }
                }
                if (!sikeres) {
                    System.out.println("\nMár van ilyen rendszámmal rendelkező autó!\n");
                }
            }
            if (sikeres) {
                autok.add(new Autok(marka, rendszam, Integer.parseInt(kobcenti), Integer.parseInt(evszam), Integer.parseInt(ertek)));
                System.out.println("\nSikeresen hozzáadtad ezt az autót:");
                for (Autok n : autok) {
                    if (n.getRendszam().equals(rendszam)) {
                        System.out.println("\n\t" + n);
                    }
                }
                autokKiir();
            }
            System.out.println("\nVisszalépés a főmenübe: 4 másodperc múlva!\n");
            Thread.sleep(4000);
            fomenu();
        }

    When the program is finishing with add to list and writing to the file I've got this txt datas:
    CAR AND TYPE
    ABC-123;1842;2000;20000000

    Then CAR AND TYPE must be seperator like this ";" without breaking lines.

    Someone can solve my problem please?

    Thanks
    Last edited by froland20; April 18th, 2021 at 06:43 AM. Reason: Extented information with text

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java run file as batch and write problem to TXT file

    Please copy and paste any information about the problem here as text, not as images.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java run file as batch and write problem to TXT file

    Right, done.

    --- Update ---

    I hope It's batch file problem, not java.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java run file as batch and write problem to TXT file

    Thank you, that is better.
    Sorry, I do not understand what the problem is. Is the program's output not formatted correctly? Can you post the current output with some comments saying what is wrong with it?

    must be seperator like this ";" without breaking lines.
    Is the posted output what the desired output should look like?


    problem with running the java program with batch file.
    Can you post the contents of the batch file?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java run file as batch and write problem to TXT file

    Here is the full output

    \------------------------/
            MEGACAR BT
    \------------------------/
     
    1. Regisztáció
    2. Bejelentkezés
     
    Regisztráció: [1], Bejelentkezés: [2]
     
    Választás [1-2]: 2
     
    Bejelentkezés!
    Felhasználónév: Roland
    Jelszó: admin
     
    Sikeres bejelentkezés!
    Kérlek várj...
     
    \------------------------/
            MEGACAR BT
    \------------------------/
     
    Felhasználó: Roland
     
    1. Egyenlegem
    2. Egyenleg feltöltés
    3. Autók listázása
    4. Autó keresése
    5. Vásárlás
    6. Tulajdonaim
    7. Eladott autók listázása
    8. Autók hozzáadása (admin)
    9. Autók eltávolítása (admin)
     
    0. Kilépés
     
    Választás [1-8] - (Admin mód): 8
     
    Admin jogosultság ellenőrízve!
     
    Autó hozzáadása!
    Autó megnevezése: car name
    Rendszám: abc-123
    Köbcenti: 1593
    Évszám: 2001
    Ára: 20000
     
    Sikeresen hozzáadtad ezt az autót:
     
     | Rendszám: ABC-123 | Hengerűrtartalom: 1593 | Évszám: 2001 | Érték: 20000 HUF               // *The problem is in this line, CAR NAME does not adding to the list successfully
     
    Visszalépés a főmenübe: 4 másodperc múlva!

    My batch file contains:
    java -jar "dist\projekt.jar"
    pause

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java run file as batch and write problem to TXT file

    | Rendszám: ABC-123 | Hengerűrtartalom: 1593 | Évszám: 2001 | Érték: 20000 HUF // *The problem is in this line, CAR NAME does not adding to the list successfully
    Ok, can you say what is wrong with that line and show what the correct output should be?

    --- Update ---

    What does the toString() method contain for the Autok class?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java run file as batch and write problem to TXT file

    This is my toString() method:
    @Override
        public String toString() {
            return "Név: " + marka + " | Rendszám: " + rendszam + " | Hengerűrtartalom: " + kobcenti + " | Évszám: " + evszam + " | Érték: " + ertek + " HUF";
        }

    For example to my Scanner input:
    marka = BMW E46
    rendszam = ABC-123
    kobcenti = 1593
    evszam = 2001
    ertek = 20000000

    That's what it should look like on output:
    Név: BMW E46 | Rendszám: ABC-123 | Hengerűrtartalom: 1593 | Évszám: 2001 | Érték: 20000000 HUF

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java run file as batch and write problem to TXT file

    The first part of the String returned by the toString method is: "Név: "
    why don't those 4 characters show up in the printed output? Even if marka contained an empty String, those 4 characters should show.

    Is there other code that is being executed instead of the code you posted here.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java run file as batch and write problem to TXT file

    One problem I see is the use of two Scanner objects that both read from System.in.
    If the Scanner class reads data into a buffer. If there are two Scanner objects, I do not know which object will receive the data that is typed in.

    I recommend changing the code to use just one Scanner object for System.in
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how can i create batch file using java ...
    By santosh tiwary in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 9th, 2013, 10:14 AM
  2. Replies: 1
    Last Post: April 15th, 2013, 06:41 AM
  3. Run a jar file inside a batch file with Windows 7 Task schduler
    By kingnachi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 09:20 AM
  4. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  5. calling a java a class connected to derby from a batch file
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 03:01 PM

Tags for this Thread