473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

true/false

Hey guys.

I'm having trouble returning true and false things in java.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class StringUtil 
  4. {
  5.     public static void main(String args[]) throws IOException
  6.     {
  7.         BufferedReader reverseString = new BufferedReader(new InputStreamReader(System.in));
  8.         String revString;
  9.         BufferedReader palindromeString = new BufferedReader(new InputStreamReader(System.in));
  10.         String palString;
  11.         BufferedReader pigLatinString = new BufferedReader(new InputStreamReader(System.in));
  12.         String plString;
  13.         BufferedReader shortHandString = new BufferedReader(new InputStreamReader(System.in));
  14.         String shString;
  15.         int i = 0;
  16.         int indexPalString = 0;
  17.  
  18.         System.out.println("Enter a potential Palindrome:");
  19.             palString = palindromeString.readLine();
  20.             indexPalString = palString.length() - 1;
  21.  
  22.         for(i = 0; i <= indexPalString; i++)
  23.         {
  24.             if (palString.charAt(i) == palString.charAt(indexPalString - i))
  25.             {
  26.                 return true;
  27.             }
  28.             else{
  29.                 return false;
  30.             }
  31.         }
  32.     }
  33. }
  34.  

I want it to check whether or not it is a palindrome. It HAS to return true or false.
And is there anyway that i could ignore case, spaces, and punctuation?

Thank You
Nov 17 '07 #1
11 2274
JosAH
11,448 Expert 8TB
Two small questions to you:

1) What are all those BufferedReaders doing there?
2) your main method is supposed to return nothing (a void); why HAS it supposed
to return a boolean value?

kind regards,

Jos
Nov 17 '07 #2
Two small questions to you:

1) What are all those BufferedReaders doing there?
2) your main method is supposed to return nothing (a void); why HAS it supposed
to return a boolean value?

kind regards,

Jos
The buffered readers are there because that's what i used in one of my other program for inputted characters.

So should i put all of the calculations before the main method?
Nov 17 '07 #3
Laharl
849 Expert 512MB
From the sounds of it, you either need to write a function to check for palindromes that would return a boolean, and call it in your main, or print whether or not it is a palindrome in your main rather than trying to return something to a void function.
Nov 17 '07 #4
From the sounds of it, you either need to write a function to check for palindromes that would return a boolean, and call it in your main, or print whether or not it is a palindrome in your main rather than trying to return something to a void function.
import java.util.Scanner;
import java.io.*;

public class StringUtil
{
public static void main(String args[])
{
Scanner reverseString = new Scanner(System.in);
String revString;
Scanner palindromeString = new Scanner(System.in));
String palString;
Scanner pigLatinString = new Scanner(System.in));
String plString;
Scanner shortHandString = new Scanner(System.in));
String shString;
int i = 0;
int indexPalString = 0;

System.out.println("Enter a potential Palindrome:");
palString = palindromeString.readLine();
indexPalString = palString.length() - 1;

for(i = 0; i <= indexPalString; i++)
{
if (palString.charAt(i).equals(palString.charAt(index PalString - i)))
{
System.out.println("True");
}
else{
System.out.println("False");
}
}


}
}


When i run it, it says true/false for every letter. how do i stop that?
Nov 18 '07 #5
Laharl
849 Expert 512MB
Move one of those printing statements outside the loop, but leave the other where it is, and use a boolean to cover whether or not it is a palindrome. You can use the break; keyword to leave the loop at any time, assuming it works the same as in C++.

Eg:
Expand|Select|Wrap|Line Numbers
  1.  \\Code that checks if two strings are equal, and prints out if they are-assumes they have the same length
  2. String s = "Hi";
  3. String t = "Bi";
  4. for (int a = 0; a < s.length(); a++){
  5.     if (s.charAt(a) != t.charAt(a)){
  6.        System.out.println(s+" and " +t+" are not the same.");
  7.        break;
  8.    }
  9. }
  10. if (a == s.length())
  11.       System.out.println("These are the same.");
  12.  
Can you extrapolate the code you need from here?
Nov 18 '07 #6
Move one of those printing statements outside the loop, but leave the other where it is, and use a boolean to cover whether or not it is a palindrome. You can use the break; keyword to leave the loop at any time, assuming it works the same as in C++.

Eg:
Expand|Select|Wrap|Line Numbers
  1.  \\Code that checks if two strings are equal, and prints out if they are-assumes they have the same length
  2. String s = "Hi";
  3. String t = "Bi";
  4. for (int a = 0; a < s.length(); a++){
  5.     if (s.charAt(a) != t.charAt(a)){
  6.        System.out.println(s+" and " +t+" are not the same.");
  7.        break;
  8.    }
  9. }
  10. if (a == s.length())
  11.       System.out.println("These are the same.");
  12.  
Can you extrapolate the code you need from here?
hmmm. ill try adding the break statement, and see if it works

It does.

import java.util.Scanner;
import java.io.*;

public class StringUtil
{
public static void main(String args[]) throws IOException
{
BufferedReader reverseString = new BufferedReader(new InputStreamReader(System.in));
String revString;
BufferedReader palindromeString = new BufferedReader(new InputStreamReader(System.in));
String palString;
BufferedReader pigLatinString = new BufferedReader(new InputStreamReader(System.in));
String plString;
BufferedReader shortHandString = new BufferedReader(new InputStreamReader(System.in));
String shString;
int i = 0;
int indexPalString = 0;

System.out.println("Enter a potential Palindrome:");
palString = palindromeString.readLine();
indexPalString = palString.length() - 1;

for(i = 0; i <= indexPalString; i++)
{
if (palString.charAt(i) != palString.charAt(indexPalString - i))
{
System.out.println("False");
break;
}
else{
System.out.println("True");
break;
}
}


}
}


Where do i put the ignore case stuff? And is there a way to ignore punctuation and spaces? So if someone were to write Race Car, it would return true.
Nov 18 '07 #7
Laharl
849 Expert 512MB
You're very close, but your current code will only check the first character against the last and then break the loop because either they are equal, in which case you print that out and break, or they aren't, in which case you print that and break. You only want to break the loop in one of those two cases and can't deal with the other case until after the loop, so you'll need to move your print statements around. Can you see how to do that?
Nov 18 '07 #8
You're very close, but your current code will only check the first character against the last and then break the loop because either they are equal, in which case you print that out and break, or they aren't, in which case you print that and break. You only want to break the loop in one of those two cases and can't deal with the other case until after the loop, so you'll need to move your print statements around. Can you see how to do that?

I don't know how to do that. I tried moving the break around and nothing happened. But i don't know why i have to move the output statements around
Nov 18 '07 #9
Laharl
849 Expert 512MB
You have to move the True print statement outside the loop because you can't conclude true unles it clears the loop without breaking prematurely. Can you see how to check whether or not you've got that case (and thus need to output True)? As a hint, the example code I gave you earlier is close to what you need.
Nov 18 '07 #10
You have to move the True print statement outside the loop because you can't conclude true unles it clears the loop without breaking prematurely. Can you see how to check whether or not you've got that case (and thus need to output True)? As a hint, the example code I gave you earlier is close to what you need.
import java.util.Scanner;
import java.io.*;

public class StringUtil
{
public static void main(String args[]) throws IOException
{
BufferedReader reverseString = new BufferedReader(new InputStreamReader(System.in));
String revString;
BufferedReader palindromeString = new BufferedReader(new InputStreamReader(System.in));
String palString;
BufferedReader pigLatinString = new BufferedReader(new InputStreamReader(System.in));
String plString;
BufferedReader shortHandString = new BufferedReader(new InputStreamReader(System.in));
String shString;
int i = 0;
int indexPalString = 0;

System.out.println("Enter a potential Palindrome:");
palString = palindromeString.readLine();
indexPalString = palString.length() - 1;

for(i = 0; i <= indexPalString; i++)
{
if (palString.charAt(i) != palString.charAt(indexPalString - i))
{
System.out.println("False");
break;
}
}
if (palString.charAt(i) == palString.charAt(indexPalString - 1))
System.out.println("True");
}
}


When i run it, there is an error message for something like "bob". I reviewed the code again and I don't know what's wrong
Nov 18 '07 #11
import java.util.Scanner;
import java.io.*;

public class StringUtil
{
public static void main(String args[]) throws IOException
{
BufferedReader reverseString = new BufferedReader(new InputStreamReader(System.in));
String revString;
BufferedReader palindromeString = new BufferedReader(new InputStreamReader(System.in));
String palString;
BufferedReader pigLatinString = new BufferedReader(new InputStreamReader(System.in));
String plString;
BufferedReader shortHandString = new BufferedReader(new InputStreamReader(System.in));
String shString;
int i = 0;
int indexPalString = 0;

System.out.println("Enter a potential Palindrome:");
palString = palindromeString.readLine();
indexPalString = palString.length() - 1;

for(i = 0; i <= indexPalString; i++)
{
if (palString.charAt(i) != palString.charAt(indexPalString - i))
{
System.out.println("False");
break;
}
}
if (palString.charAt(i) == palString.charAt(indexPalString - 1))
System.out.println("True");
}
}


When i run it, there is an error message for something like "bob". I reviewed the code again and I don't know what's wrong

Never mind, i got it
Nov 19 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
3
by: drs | last post by:
I just upgraded my Python install, and for the first time have True and False rather than 1 and 0. I was playing around at the command line to test how they work (for instance, "if 9:" and "if...
35
by: Steven Bethard | last post by:
I have lists containing values that are all either True, False or None, e.g.: etc. For a given list: * If all values are None, the function should return None.
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
48
by: Skybuck Flying | last post by:
Hi, I came across this C code which I wanted to understand etc it looked like this: if (-1) etc It made me wonder what the result would be... true or false ? In C and Delphi
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
4
by: Wayne Wengert | last post by:
I am using VB in a VSNET 2003 Windows application. I've run into a situation where, when trying to set a bit value in a SQL Server 2000 database I get errors because the values extracted from a...
59
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True ...
30
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's...
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.