• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Testing Strings

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Having a few problems with strings. I am trying to write a program so that I can input two strings, and then test the strings to see whether they are identical or, if not, which comes first in the alphabet.

I have this code and it wont compile.



Any help would be appreciated.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error message are you getting?
 
Timothy Willis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Error message:

Cannot resolve symbol - variable string 1
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you don't declare string1 anywhere, do you? A declaration include the variables type and name and optionally an initializer. You first use "string1" without having every declared it to be a String.

When you do, I suspect you'll have a problem assigning what "getInt()" returns to a String variable -- maybe there's a getString() method you should use instead?
 
Timothy Willis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So like this:

public class Strings
{

public static void main (String[] args)
{
String string1, string2;

System.out.print("Please Enter First String: ");
string1 = EasyIn.getString();
System.out.print("Please Enter Second String: ");
string2 = EasyIn.getString();

if (string1.equals(string2))
System.out.println("The Strings are Identical");
else if (string1.compareTo(string2) >0 )
System.out.println("The First String comes first in alphabetical Order");
else if (string1.compareTo(string2) < 0)
System.out.println("The Second String comes first in alphabetical order");

}
}
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is "EasyIn.getString()"?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic