Results 1 to 5 of 5
Thread: Issue with scanners and strings
- 12-13-2011, 12:19 PM #1
Member
- Join Date
- Nov 2011
- Location
- Australia
- Posts
- 28
- Rep Power
- 0
Issue with scanners and strings
Hi everyone,
So I have written a code which allows for a user to input a name through the use of a scanner and then using a while statement, an array is searched through to see if the name already exists within the array. My problem here is that whenever I input a name into the scanner that I know is on the array the code seems to disregard it and report that the name does not exist within the array. Any help would be appreciated.
import java.util.*;
public class test
{
private static int size = 20;
private String customerName[];
public void customerName()
{
customerName = new String[size];
customerName[0] = "Joshua";
customerName[1] = "Sandee";
customerName[2] = "Sam";
customerName[3] = "Laure";
customerName[4] = "Joseph";
int place = 0;
String name;
Scanner in = new Scanner(System.in);
System.out.print("What is the customers name:");
name = in.nextLine();
System.out.print("Your name is " +name);
while (place <customerName.length && name != customerName[place])
{
place++;
//This is just in place to make sure the while statment goes through the entire array
System.out.println("Hi");
}
if (place == customerName.length)
{
System.out.println("Unknown member.");
}
else
{
System.out.print("Already a member.");
}
}
}
- 12-13-2011, 02:25 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: Issue with scanners and strings
Java Code:name != customerName[place]
- 12-13-2011, 02:28 PM #3
Member
- Join Date
- Nov 2011
- Location
- Australia
- Posts
- 28
- Rep Power
- 0
Re: Issue with scanners and strings
Oh okay thank you and one more question is there an equivlaent of != too in words, if you understand what I'm asking. Sorry I'm still only starting Java.
- 12-13-2011, 04:02 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: Issue with scanners and strings
!someString.equals(someOtherString)
- 12-14-2011, 04:55 AM #5
Member
- Join Date
- Nov 2011
- Location
- Australia
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Problem with Scanners
By Sharoo in forum New To JavaReplies: 2Last Post: 11-11-2011, 06:17 AM -
Strings, scanners and IFs
By JavaGame in forum New To JavaReplies: 4Last Post: 09-08-2011, 12:58 AM -
Random automation of sports teams (things) using arrays, scanners, and randomNumbers
By Sizzlewump in forum New To JavaReplies: 6Last Post: 08-22-2011, 06:32 AM -
Buffered Reader and scanners
By nevermiind in forum New To JavaReplies: 8Last Post: 05-04-2010, 07:22 PM -
Scanners / Printers
By cytech in forum New To JavaReplies: 0Last Post: 03-17-2008, 06:54 PM
Bookmarks