Another simple Java Question from a newbie.

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Another simple Java Question from a newbie.

Post by Amidda1217 on Sat Jul 24, 2010 6:51 pm
([msg=42634]see Another simple Java Question from a newbie.[/msg])

I am crashing on my while loop during the second run. The first run goes smoothly. However once it runs for the second time it crashes on my switch statement. Can any one help me find what I did wrong. I'm lost and have been trying to solve it since last night.

Code: Select all

package pg231num1;



import java.util.Scanner;
public class Main {


    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String exclam = "bad", question = "bad";
        String sentence;
        String answer = "no";
        int length;
do {
     
       
    System.out.println("Please enter your question or concern: ");
        sentence = keyboard.nextLine(); //stores input as string

        length = sentence.length(); //stores length of sentence

        switch (sentence.charAt(length - 1))//checks for ! or ?
        {
            case '!' :
                    exclam = "good";
                    break;
            case '?' :
                    question = "good";
                    break;


        }
        if ((length % 2 == 0) && (question.equalsIgnoreCase("good"))){//checks to see if even and question
            System.out.println("Yes");
        }
else if ((length % 2 != 0) && (question.equalsIgnoreCase("good"))){ //checks to see if odd and question
     System.out.println("No");
}
else if (exclam.equalsIgnoreCase("good")){ //checks to see if !
     System.out.println("Wow");

}
        else if((question.equalsIgnoreCase("bad")) && (exclam.equalsIgnoreCase("bad")))//checks to see if neither ! or ? are present
        {
            System.out.println("You always say " + sentence);
        }
System.out.println("Would you like to ask another question?");
System.out.println("Enter yes or no: ");
answer = keyboard.next();
        //System.out.println(sentence.charAt(length - 1)); //prints last char of sentence

    } while (answer.equalsIgnoreCase("yes"));
    }
}
The user can't be patched.
Amidda1217
New User
New User
 
Posts: 31
Joined: Thu Jun 04, 2009 6:49 am
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by sanddbox on Sat Jul 24, 2010 7:10 pm
([msg=42635]see Re: Another simple Java Question from a newbie.[/msg])

Why are you using strings instead of booleans for question and answer in the first place? That's ridiculous and unnecessarily confusing.

Also, there's no point making a separate variable for length - it makes it more confusing and less easy to read. Just use sentence.length - it will make debugging easier and also ensure readers of your code know what you're doing.

EDIT: The problem appears to be with keyboard.nextLine(); - let me read some docs.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2331
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by Amidda1217 on Sat Jul 24, 2010 7:38 pm
([msg=42636]see Re: Another simple Java Question from a newbie.[/msg])

I think I fixed everything you were talking about to make things easier to read. Thank you for that. Also I think I grasp boolean a little bit better.
Code: Select all
package pg231num1;



import java.util.Scanner;
public class Main {


    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        boolean exclam = false;
        boolean   question = false;
        String sentence;
        boolean answer = false;
        String reply;
do {
     
       
    System.out.println("Please enter your question or concern: ");
        sentence = keyboard.nextLine(); //stores input as string

    switch (sentence.charAt(sentence.length() - 1))//checks for ! or ?
        {
            case '!' :
                    exclam = true;
                    break;
            case '?' :
                    question = true;
                    break;


        }
        if ((sentence.length() % 2 == 0) && (question == true)){//checks to see if even and question
            System.out.println("Yes");
        }
else if ((sentence.length() % 2 != 0) && (question == true)){ //checks to see if odd and question
     System.out.println("No");
}
else if (exclam == true){ //checks to see if !
     System.out.println("Wow");

}
        else if((question == false) && (exclam == false))//checks to see if neither ! or ? are present
        {
            System.out.println("You always say " + sentence);
        }
System.out.println("Would you like to ask another question?");
System.out.println("Enter yes or no: ");
reply = keyboard.next();
      if (reply.equalsIgnoreCase("yes")) {
          answer = true; }

        //System.out.println(sentence.charAt(length - 1)); //prints last char of sentence

    } while (answer == true);
    }
}
The user can't be patched.
Amidda1217
New User
New User
 
Posts: 31
Joined: Thu Jun 04, 2009 6:49 am
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by sanddbox on Sat Jul 24, 2010 8:04 pm
([msg=42637]see Re: Another simple Java Question from a newbie.[/msg])

I haven't found anything about the readline() problem on google :/ I'll keep searching.

By the way - to save time, instead of saying if(question==true), you can just say if(question) and get the same thing.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2331
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by Amidda1217 on Sat Jul 24, 2010 8:10 pm
([msg=42639]see Re: Another simple Java Question from a newbie.[/msg])

you're awesome. thank you very much for all your help.

I really don't get why I am crashing. I googled it as much as I could with my mediocre understanding of the possible problem.
The user can't be patched.
Amidda1217
New User
New User
 
Posts: 31
Joined: Thu Jun 04, 2009 6:49 am
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by sanddbox on Sat Jul 24, 2010 8:23 pm
([msg=42641]see Re: Another simple Java Question from a newbie.[/msg])

Fixed it. You have to create a new instance of Scanner every time you go through the loop (don't know why to be honest, but it worked).
Code: Select all
package pg231num1;



import java.util.Scanner;
public class Main {


    public static void main(String[] args) {
        boolean exclam = false;
        boolean   question = false;
        String sentence;
        boolean answer = false;
        String reply;
do {
     
       Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter your question or concern: ");
        sentence = keyboard.nextLine(); //stores input as string

    switch (sentence.charAt(sentence.length() - 1))//checks for ! or ?
        {
            case '!' :
                    exclam = true;
                    break;
            case '?' :
                    question = true;
                    break;


        }
        if ((sentence.length() % 2 == 0) && (question == true)){//checks to see if even and question
            System.out.println("Yes");
        }
else if ((sentence.length() % 2 != 0) && (question == true)){ //checks to see if odd and question
     System.out.println("No");
}
else if (exclam == true){ //checks to see if !
     System.out.println("Wow");

}
        else if((question == false) && (exclam == false))//checks to see if neither ! or ? are present
        {
            System.out.println("You always say " + sentence);
        }
System.out.println("Would you like to ask another question?");
System.out.println("Enter yes or no: ");
reply = keyboard.next();
      if (reply.equalsIgnoreCase("yes")) {
          answer = true; }

        //System.out.println(sentence.charAt(length - 1)); //prints last char of sentence

    } while (answer == true);
    }
}


-- Sat Jul 24, 2010 8:30 pm --

I cleaned it up and fixed a bug where if you answered yes initially and then answered no, the program would keep running.
Code: Select all
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        boolean exclam = false;
        boolean question = false;
        String sentence;
        boolean answer = false;
        String reply;
        do {
            System.out.println("Please enter your question or concern: ");
            sentence = keyboard.nextLine(); //stores input as string

            switch (sentence.charAt(sentence.length() - 1))//checks for ! or ?
            {
                case '!':
                    exclam = true;
                    break;
                case '?':
                    question = true;
                    break;
            }
            if ((sentence.length() % 2 == 0) && (question)) {//checks to see if even and question
                System.out.println("Yes");
            } else if ((sentence.length() % 2 != 0) && (question)) { //checks to see if odd and question
                System.out.println("No");
            } else if (exclam) { //checks to see if !
                System.out.println("Wow");

            } else if ((!question) && (!exclam))//checks to see if neither ! or ? are present
            {
                System.out.println("You always say " + sentence);
            }
            System.out.println("Would you like to ask another question?");
            System.out.println("Enter yes or no: ");
            reply = keyboard.next();
            if (reply.equalsIgnoreCase("yes")) {
                answer = true;
            } else {
                answer = false;
            }
        }
        while (answer);
    }
}
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2331
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by TheMindRapist on Sat Jul 24, 2010 8:34 pm
([msg=42642]see Re: Another simple Java Question from a newbie.[/msg])

There were a few logic errors in addition to the error with using next in place of nextLine at the end.

Code: Select all
   package pg231num1;
import java.util.Scanner;
    public class Main {


        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            boolean exclam = false;
            boolean   question = false;
            String sentence;
            boolean answer = false;
            String reply;
    do {   
        System.out.println("Please enter your question or concern: ");
            sentence = keyboard.nextLine(); //stores input as string

        switch (sentence.charAt(sentence.length() - 1))//checks for ! or ?
        {
                case '!' :
                        exclam = true;
                        question = false;  //Otherwise both could be true
                        break;
                case '?' :
                        question = true;
                        exclam = false;  //Otherwise both could be true
                        break;
         }
            if ((sentence.length() % 2 == 0) && (question == true)){//checks to see if even and question
                System.out.println("Yes");
            }
    else if ((sentence.length() % 2 != 0) && (question == true)){ //checks to see if odd and question
         System.out.println("No");
    }
    else if (exclam == true){ //checks to see if !
         System.out.println("Wow");

    }
            else if((question == false) && (exclam == false))//checks to see if neither ! or ? are present
            {
                System.out.println("You always say " + sentence);
            }
    System.out.println("Would you like to ask another question?");
    System.out.println("Enter yes or no: ");
    reply = keyboard.nextLine();
          if (!reply.equalsIgnoreCase("yes")) { //Before the loop never ended
              answer = false; }

            //System.out.println(sentence.charAt(length - 1)); //prints last char of sentence

        } while (answer == true);
        }
    }


Edit: One or two other things that would yield cleaner code:

Code: Select all
reply = keyboard.nextLine();
          if (!reply.equalsIgnoreCase("yes")) {
              answer = false; }

Can be rewritten as:

Code: Select all
answer = reply.equalsIgnoreCase("yes");

Similarly, you could do

Code: Select all
exclam = (sentence.charAt(sentence.length()-1) == '!');
            question = (sentence.charAt(sentence.length()-1) == '?');

Although if this is for a class you might be required to use switch statements, eh?
Last edited by TheMindRapist on Sat Jul 24, 2010 8:43 pm, edited 1 time in total.
Image
User avatar
TheMindRapist
Contributor
Contributor
 
Posts: 585
Joined: Mon Apr 14, 2008 4:57 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by Amidda1217 on Sat Jul 24, 2010 8:39 pm
([msg=42643]see Re: Another simple Java Question from a newbie.[/msg])

i see you added the else statement to change answer back to false. thank you very much for your help. I would have never thought to move my scanner object into the do statement.

-- Sat Jul 24, 2010 8:41 pm --

TheMindRapist wrote:There were a few logic errors in addition to the error with using next in place of nextLine at the end.

Code: Select all
   package pg231num1;
import java.util.Scanner;
    public class Main {


        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            boolean exclam = false;
            boolean   question = false;
            String sentence;
            boolean answer = false;
            String reply;
    do {   
        System.out.println("Please enter your question or concern: ");
            sentence = keyboard.nextLine(); //stores input as string

        switch (sentence.charAt(sentence.length() - 1))//checks for ! or ?
        {
                case '!' :
                        exclam = true;
                        question = false;  //Otherwise both could be true
                        break;
                case '?' :
                        question = true;
                        exclam = false;  //Otherwise both could be true
                        break;
         }
            if ((sentence.length() % 2 == 0) && (question == true)){//checks to see if even and question
                System.out.println("Yes");
            }
    else if ((sentence.length() % 2 != 0) && (question == true)){ //checks to see if odd and question
         System.out.println("No");
    }
    else if (exclam == true){ //checks to see if !
         System.out.println("Wow");

    }
            else if((question == false) && (exclam == false))//checks to see if neither ! or ? are present
            {
                System.out.println("You always say " + sentence);
            }
    System.out.println("Would you like to ask another question?");
    System.out.println("Enter yes or no: ");
    reply = keyboard.nextLine();
          if (!reply.equalsIgnoreCase("yes")) { //Before the loop never ended
              answer = false; }

            //System.out.println(sentence.charAt(length - 1)); //prints last char of sentence

        } while (answer == true);
        }
    }

in this situation why is next different than nextLine? I trust you. I just want to know to adapt my thinking. Thank you for finding those.
The user can't be patched.
Amidda1217
New User
New User
 
Posts: 31
Joined: Thu Jun 04, 2009 6:49 am
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by sanddbox on Sat Jul 24, 2010 8:46 pm
([msg=42644]see Re: Another simple Java Question from a newbie.[/msg])

I think next() should work just fine.
Image

HTS User Composition:
95% Male
4.98% Female
.01% Monica
.01% Goat
User avatar
sanddbox
Expert
Expert
 
Posts: 2331
Joined: Sat Jul 04, 2009 5:20 pm
Blog: View Blog (0)


Re: Another simple Java Question from a newbie.

Post by Amidda1217 on Sat Jul 24, 2010 8:48 pm
([msg=42645]see Re: Another simple Java Question from a newbie.[/msg])

Here is the final code "I think." I also added a reset of question and exclam at the end of the "do" to make the responses correct when the type of response changes.


Code: Select all
    package pg231num1;



    import java.util.Scanner;
    public class Main {


        public static void main(String[] args) {
            boolean exclam = false;
            boolean   question = false;
            String sentence;
            boolean answer = false;
            String reply;
    do {

           Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter your question or concern: ");
            sentence = keyboard.nextLine(); //stores input as string

        switch (sentence.charAt(sentence.length() - 1))//checks for ! or ?
            {
                case '!' :
                        exclam = true;
                        break;
                case '?' :
                        question = true;
                        break;


            }
            if ((sentence.length() % 2 == 0) && (question == true)){//checks to see if even and question
                System.out.println("Yes");
            }
    else if ((sentence.length() % 2 != 0) && (question == true)){ //checks to see if odd and question
         System.out.println("No");
    }
    else if (exclam == true){ //checks to see if !
         System.out.println("Wow");

    }
            else if((question == false) && (exclam == false))//checks to see if neither ! or ? are present
            {
                System.out.println("You always say " + sentence);
            }
    System.out.println("Would you like to ask another question?");
    System.out.println("Enter yes or no: ");
    reply = keyboard.nextLine();
          if (reply.equalsIgnoreCase("yes")) {
              answer = true; }
else {
        answer = false;}
            //System.out.println(sentence.charAt(length - 1)); //prints last char of sentence
exclam = false;
question = false;

        } while (answer == true);
        }
    }
The user can't be patched.
Amidda1217
New User
New User
 
Posts: 31
Joined: Thu Jun 04, 2009 6:49 am
Blog: View Blog (0)


Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron