Current Position:Home > Please help: more than one case won't work

Please help: more than one case won't work

Update:11-30Source: network consolidation
Advertisement
I am working on an assignment for my class and I am trying to make it so that when a name like "Fred" or "Jonathan" is inputed, it will read a .txt file and output the nick name from the .txt file; but also, if the input does not match the first name, it will look at the next and so on. Right now it will ask me a name and let me input, but it will not find the case and print the correct line. Please help.
Here is my code so far:
import java.util.*;
import java.io.*;
public class Nickname {
     private static int compareNames(){
          Scanner name1 = new Scanner (System.in);
          String fred = name1.next().trim() ;
          String jon = name1.next().trim() ;
          String noMatch = name1.next();
          int name = 0;
          if (fred.equals("fred")) { name = 1 ;}
               else if (jon.equals("jonathan")) { name = 2 ;}
               else if (noMatch.equals("")) {name = 77;}
          return (name);
     public static void main(String[] args) throws Exception {
          Scanner fre = new Scanner (new File ("fred.txt"));
          Scanner jo = new Scanner (new File ("jonathan.txt"));
          Scanner ger = new Scanner (new File ("gertrude.txt"));
          Scanner bar = new Scanner (new File ("bartholemew.txt"));
          System.out.print ("Tell me your name: ");
          switch (compareNames()){
          case 1:
               String fred = fre.next();
               {System.out.print("I think I will call you " + fred + " instead.") ; break ; }
          case 2:
               String jon = jo.next();
               {System.out.print("I think I will call you " + jon + " instead.") ; break ; }
          case 77:
               {System.out.print ("I hope this works") ; break ; }
}

The Best Answer

Advertisement
I tried the above code, and I code not get it to work for me. We haven' t learned a lot of that yet. This is what my teacher is asking for:
2. Nickname
First prompt the user for a name, which will be a single word. If the user types in Fred, for
example, the user's nickname will be stored in a file located in your current directory and with the
name fred-nickname.txt. You will read the user's nickname from that file. Suppose the file
fred.txt contains the word "FREDDY." Your program would then print the following message to
the console:
I think I will call you FREDDY instead.
In the assignment directory there are four sample "nickname" files that you should download to
the same directory containing this application:
jonathan.txt that contains the nickname "Jonathan"
fred.txt that contains the nickname "FREDDY"
bartholemew.txt that contains the nickname "bart"
gertrude.txt that contains the nickname "Gertie."
If those files were in your program's directory, your program should work like this (these are four
separate runs of the program):
Tell me a name: Jonathan
I think I will call you Jon instead.
Tell me a name: Fred
I think I will call you FREDDY instead.
Tell me a name: BARTholemew
I think I will call you bart instead.
Tell me a name: Gertrude
I think I will call you Gertie instead.
Notice that your program has to work regardless of whether the name is entered in upper case,
lower case, or some combination. Also notice that the program displays the name in the file
exactly as it appears in the file, without changing its case. You can assume that the user types the
name without leading or trailing white space, and that every name the user types in has a
corresponding nickname file. (In other words, your program does not have to handle the case
where a user types in an unrecognized name.) Also note that although you are supplied with four
examples of "nickname" files, your program should work any time the user types in a name that
has a corresponding "nickname" file in the application's working directory. You should write
several additional nickname files and test your program on those.
This application should be named Nickname.java. (project, class, etc.)
And this is my reworked code:
import java.util.*;
import java.io.*;
public class Nickname {
     private static Scanner input;     
     //private static Scanner nname;
     private static String name1 = "fred";
     private static String name2 = "jonathan";
     private static String name3 = "gertrede";
     private static String name4 = "bartholemew";
     private static String getNames() throws Exception{
          //System.out.print("Tell me your name :") ;
          input = new Scanner (System.in);
          String ans = input.next().trim();
          if (ans.equals(name1)) {
          Scanner fre = new Scanner (new File ("fred.txt"));}
          if (ans.equals(name2)) ;
          if (ans.equals(name3)) ;
          if (ans.equals(name4)) ;
          return (ans);
     public static void main(String args[]) throws Exception {
          //Scanner fre = new Scanner (new File ("fred.txt"));
          //Scanner jo = new Scanner (new File ("jonathan.txt"));
          //Scanner ger = new Scanner (new File ("gertrude.txt"));
          //Scanner bar = new Scanner (new File ("bartholemew.txt"));
          System.out.print ("Tell me your name: ");
          System.out.print(" your name is " + getNames());
right now it just prints what I type in.
I would appreciate any help.