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 16 of 16

Thread: when they write " . " it stops

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question when they write " . " it stops

    Hi, i have some homework and i am really struggling with it. assignment is The user enters some text into the console , which you write to the file. As soon as the user enters only the dot (line "."), You will no longer type it and exit the program. We are working in Netbeans. Can someone please help me? Thank you

  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: when they write " . " it stops

    If the dot is read into a String, the String class has methods to test what a String contains.
    if(input String is a .) exit the program
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2021
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: when they write " . " it stops

    can you please show me how would the code looks like? thx

    --- Update ---

    because i have this


    public static void main(String[] args) {

    String s = "123";
    int i = Integer.parseInt(s); /**POTŘEBA K ÚKOLU*/

    try {
    FileWriter w = new FileWriter("mytext.txt");
    w.write("Ahoj, tohle je text co zapisuji do souboru./n");
    w.write("další řádek text....");
    if(inputString.compareTo(".") == 0)
    return;

    } catch (IOException ex) {
    System.out.println("Cant open mytext.txt.");
    }


    and its not workin

  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: when they write " . " it stops

    That code could work if it had a way to read something from the user. The Scanner class has methods for reading user input.
    Another way to compare Strings is the equals() method.
    its not workin
    Please explain what happens.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: when they write " . " it stops

    I cant send it here like zip file idk why or what am i doin wrong, so im gonna send the code at least. Thank you so much for helping and if you had time, can you please take a look on my other parts of homeworks (they are like note in the code)

     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package textfilewrite;
     
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author nicolsevcikova
     */
    public class TextFileWrite {
     
     
         /**Homework is : 1) The user enters some text into the console in the cycle, which you write to the file. As soon as the user enters only the dot (line "."), You will no longer type it and exit the program.
    2) Load a text file and write down whether or not it was *.
    3) Read individual lines from the text file that contain numbers. Finally, write the largest number and the average of the numbers.
            */
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
             String s = "123";
            int i = Integer.parseInt(s); /**needed for homework*/  
     
            try {
                FileWriter w = new FileWriter("mujtext.txt");
                w.write("Hi this is some text./n");
                w.write("next line of text....");
                if(inputString.compareTo(".") == 0) 
      return;
     
            } catch (IOException ex) {
                System.out.println("Cant open mytext.txt.");
            }
     
     
            try {
                Scanner file = new Scanner(new File("mytext.txt"));
                int largest = file.nextInt();
     
     
                while(file.hasNextInt()) {
                    int number = file.nextInt();
     
                    if(number > largest) {
                        largest = number;
                    }
     
                    System.out.println(number);
                }
     
                file.close();
     
                System.out.println("Biggest nubmer is : " + largest);
            } catch(IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }










    here it is, this is one class. A have also class named Scanner which is empty and class inputString which is also empty.

    Thank you for helping me

  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: when they write " . " it stops

    What happens when you compile and execute the program?
    If there are any error messages that you need help with, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2021
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: when they write " . " it stops

    this happend

     
    Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
    	at textfilewrite.inputString.compareTo(inputString.java:15)
    	at textfilewrite.TextFileWrite.main(TextFileWrite.java:42)
    /Users/nicolsevcikova/Library/Caches/NetBeans/12.0/executor-snippets/run.xml:111: The following error occurred while executing this line:
    /Users/nicolsevcikova/Library/Caches/NetBeans/12.0/executor-snippets/run.xml:94: Java returned: 1
    BUILD FAILED (total time: 0 seconds)



    and Homework is : 1) The user enters some text into the console in the cycle, which you write to the file. As soon as the user enters only the dot (line "."), You will no longer type it and exit the program.
    2) Load a text file and write down whether or not it was *.
    3) Read individual lines from the text file that contain numbers. Finally, write the largest number and the average of the numbers.

  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: when they write " . " it stops

    Where is the variable: inputString declared?
    Where is it assigned a value?

    at textfilewrite.inputString.compareTo(inputString.java:15)
    The error message says inputString is a class. The posted code does not show that
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2021
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: when they write " . " it stops

    Quote Originally Posted by Norm View Post
    Where is the variable: inputString declared?
    Where is it assigned a value?


    The error message says inputString is a class. The posted code does not show that
    a told you, that i have also 2 empty classes. One of them is inputString. In it is just

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package textfilewrite;
     
    /**
     *
     * @author nicolsevcikova
     */
    class inputString {
     
        static int compareTo(String string) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
     
    }


    because i dont know what towrite in it


    Homework is : 1) The user enters some text into the console in the cycle, which you write to the file. As soon as the user enters only the dot (line "."), You will no longer type it and exit the program.
    2) Load a text file and write down whether or not it was *.
    3) Read individual lines from the text file that contain numbers. Finally, write the largest number and the average of the numbers.
    */

  10. #10
    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: when they write " . " it stops

    i dont know what towrite in it
    That depends on what it is supposed to do. Please explain what the compareTo method is supposed to do. What can it do with a single argument?

    Note: Java specification recommends that class names start with an uppercase letter. So inputStream should be named InputStream,

    Why are you writing your own method to compare two Strings instead of using the equals method?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2021
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: when they write " . " it stops

    well im suposed to do this : when someone write in the text " . " the loop will end.

  12. #12
    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: when they write " . " it stops

    when someone write in the text " . "
    Use the equals method to detect that.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2021
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: when they write " . " it stops

    well i tried this but its doin nothing so

     String input = scanner.nextLine();
        if (input.equals(" . ")) {
           return;

  14. #14
    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: when they write " . " it stops

    its doin nothing so
    That code is testing for a space, a dot and another space. Is that what is in the String input?
    Print it out immediately after reading it to see:
       System.out.println(">"+input + "<");  // show input with ><s
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2021
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: when they write " . " it stops

    Can you please just write me the code how it is suposed to be? Thank you

  16. #16
    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: when they write " . " it stops

    Did you try what I suggested? What was printed on the console?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: August 20th, 2019, 07:07 AM
  2. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  3. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  4. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  5. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM