Code does not enter switch statement

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 782 Views - Last Post: 04 November 2018 - 09:28 AM Rate Topic: -----

#1 MattC231   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-April 17

Code does not enter switch statement

Posted 05 October 2018 - 08:35 PM

import java.util.Scanner;

public class TransferAdvisor
{
    
    
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String school = "";
        String schoolNorm = "";
        String major = "";
        double gpa = 0.0;
        
        System.out.println("Enter the school name you wish to transfer to:");
        schoolNorm = input.nextLine();
        school = schoolNorm.toLowerCase();
        System.out.println("Enter your major:");
        major = input.nextLine().toLowerCase();
        System.out.println("Enter your GPA:");
        gpa = input.nextDouble();
        
        checkCanTransfer(schoolNorm, school, major, gpa);
    }
    
    public static void checkCanTransfer(String schoolNorm, String school, String major, double gpa)
    {
        Scanner ucla = new Scanner("UCLA.txt");
        Scanner ucb = new Scanner("UCB.txt");
        Scanner uci = new Scanner("UCI.txt");
        Scanner ucsd = new Scanner("UCSD.txt");
        double requiredGPA = 0.0;
        //System.out.println(schoolNorm + " " + school + " " + major + " " + gpa + " " + requiredGPA);
        
        switch(school)
        {
            case "ucla":
                  while(ucla.hasNext())
                  {
                      if(ucla.next().equals(major))
                      {
                          requiredGPA = Double.parseDouble(ucla.nextLine());
                          System.out.println(school + " " + major + " " + requiredGPA);
                      }
                  }
                  break;
            case "ucb":
                while(ucb.hasNext())
                  {
                      if(ucb.next().equals(major))
                      {
                          requiredGPA = Double.parseDouble(ucb.nextLine());
                      }
                  }
                break;
            case "uci":
                while(uci.hasNext())
                  {
                      if(uci.next().equals(major))
                      {
                          requiredGPA = Double.parseDouble(uci.nextLine());
                      }
                  }
                break;
            case "ucsd":
                while(ucsd.hasNext())
                  {
                      if(ucsd.next().equals(major))
                      {
                          requiredGPA = Double.parseDouble(ucsd.nextLine());
                      }
                  }
                break;
            default:
                System.out.println(schoolNorm + " is  not in the system.");
        }
    }
}



Can some one tell me why it does not enter the switch statement? I tested that it does pass the correct values to checkCanTransfer.
I will attach the .txt files.
Attached File  UCB.txt (32bytes)
Number of downloads: 9
Attached File  UCI.txt (32bytes)
Number of downloads: 8
Attached File  UCLA.txt (32bytes)
Number of downloads: 11
Attached File  UCSD.txt (32bytes)
Number of downloads: 8

This post has been edited by MattC231: 05 October 2018 - 08:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Code does not enter switch statement

#2 andrewsw   User is offline

  • head thrashing
  • member icon

Reputation: 6645
  • View blog
  • Posts: 27,201
  • Joined: 12-December 12

Re: Code does not enter switch statement

Posted 05 October 2018 - 11:57 PM

What version of Java are you using? Earlier versions did not work with strings in switch statements.
Was This Post Helpful? 0
  • +
  • -

#3 MattC231   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-April 17

Re: Code does not enter switch statement

Posted 06 October 2018 - 12:29 AM

I'm using NetBeans compiler, so version 8 I believe.

Is it a problem in the switch or case statement(s)?

This post has been edited by MattC231: 06 October 2018 - 12:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7327
  • View blog
  • Posts: 15,238
  • Joined: 16-October 07

Re: Code does not enter switch statement

Posted 06 October 2018 - 03:53 AM

You're creating a file reader for every file before you even know what file you want?!? Ouch.

I'd lay out your code like so:
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter the school name you wish to transfer to:");
    String school = input.nextLine();
    System.out.println("Enter your major:");
    String major = input.nextLine().toLowerCase();
    System.out.println("Enter your GPA:");
    double gpa = input.nextDouble();
    double requiredGpa = getRequiredGpa(school, major);
    if (requiredGpa!=0) {
        System.out.println(school + " " + major + " " + requiredGpa);
        if (gpa >= requiredGpa) {
            // your message
        } else {
            // your message
        }
    } else {
        System.out.println("oops");
    }

}

private static String filenamForSchool(String school) { /* your code here */ }

public static double getRequiredGpa(String school, String major)  { /* your code here */ }



Now, without ever opening a file, you can make sure you filename resolve works. And, honestly, all your file read code was identical. Think about it.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 MattC231   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-April 17

Re: Code does not enter switch statement

Posted 06 October 2018 - 07:52 PM

would this work?

public static double getRequiredGPA(String schoolNorm, String school, String major)
    {
        String fileName = "";
        fileName = school.toUpperCase();
        Scanner file = new Scanner(fileName);        
        double gpa = 0.0;
        
        while(file.hasNext())
        {
            if(file.next().equals(major))
            {
                gpa = Double.parseDouble(file.nextLine());
            }
            else
            {
                System.out.println("The entered major, " + major + ",is not offered at " + schoolNorm + ".");
            }
        }
        
        return gpa;
    }



edit: it doesn't to a point, it displays the else statement and gpa = 0.0
edit: also thanks for the stepping stone

This post has been edited by MattC231: 06 October 2018 - 07:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 MattC231   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-April 17

Re: Code does not enter switch statement

Posted 06 October 2018 - 10:59 PM

public static double getRequiredGPA(String schoolNorm, String school, String major)
    {
        String fileName = "";
        fileName = school.toUpperCase() + ".txt";
        Scanner file = new Scanner(fileName);        
        double gpa = 0.0;
        
        while(file.hasNext())
        {
            if(file.nextLine().equals(major))
            {
                gpa = Double.parseDouble(file.next());
            }
            else
            {
                System.out.println("The entered major, " + major + ",is not offered at " + schoolNorm + ".");
            }
        }
        
        return gpa;
    }



Am I using the scanner wrong because I believe the correct file .txt is not opened since gpa is returned as 0.0

Attached File  UCB.txt (32bytes)
Number of downloads: 7
Attached File  UCI.txt (32bytes)
Number of downloads: 7
Attached File  UCLA.txt (32bytes)
Number of downloads: 8
Attached File  UCSD.txt (32bytes)
Number of downloads: 7
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • head thrashing
  • member icon

Reputation: 6645
  • View blog
  • Posts: 27,201
  • Joined: 12-December 12

Re: Code does not enter switch statement

Posted 07 October 2018 - 12:09 AM

What arguments are you providing when calling your function?
Was This Post Helpful? 0
  • +
  • -

#8 MattC231   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-April 17

Re: Code does not enter switch statement

Posted 07 October 2018 - 01:40 AM

this is all before the method gets called

public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String school = "";
        String schoolNorm = "";
        String major = "";
        double gpa = 0.0;
        double requiredGPA = 0.0;
        boolean inSystem = false;
        
        System.out.println('a' + 'c');
        while(!inSystem)
        {
            System.out.println("Enter the school name you wish to transfer to:");
            schoolNorm = input.nextLine();
            inSystem = isSchoolInDB(schoolNorm.toLowerCase());
        }
        school = schoolNorm.toLowerCase();
        
        System.out.println("Enter your major:");
        major = input.nextLine().toLowerCase();
        
        System.out.println("Enter your GPA:");
        gpa = input.nextDouble();        
        requiredGPA  = getRequiredGPA(schoolNorm, school, major);


Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3604
  • View blog
  • Posts: 16,500
  • Joined: 20-September 08

Re: Code does not enter switch statement

Posted 07 October 2018 - 01:56 AM

Quote

else


You can't put that inside the loop. Think about it. Since you're not breaking out of the loop when the search string is found (hint: you should) then the only way your loop code would succeed is if the search string you're looking for happened to be in the penultimate line

btw, please don't post new questions on the same subject

This post has been edited by g00se: 07 October 2018 - 01:59 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#10 MattC231   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-April 17

Re: Code does not enter switch statement

Posted 07 October 2018 - 03:20 AM

Thanks for reminding me that I have to use break; to exit the loop and that I should get rid of the else, I see what you mean. The main issue is that I don't think the correct file is being opened--gpa is returned as 0.0 . Am I using thew Scanner statement correctly? Can I use a string variable to represent thew filename as I have coded?

I apologize for making another topic about the same question

This post has been edited by MattC231: 07 October 2018 - 03:21 AM

Was This Post Helpful? 0
  • +
  • -

#11 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3604
  • View blog
  • Posts: 16,500
  • Joined: 20-September 08

Re: Code does not enter switch statement

Posted 07 October 2018 - 04:41 AM

Well if you post your corrected code, i'll take a look
Was This Post Helpful? 0
  • +
  • -

#12 MattC231   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-April 17

Re: Code does not enter switch statement

Posted 07 October 2018 - 07:33 AM

View Postg00se, on 07 October 2018 - 11:41 AM, said:

Well if you post your corrected code, i'll take a look


Ignore line 17 and 34

import java.util.Scanner;

public class TransferAdvisor
{
    
    
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String school = "";
        String schoolNorm = "";
        String major = "";
        double gpa = 0.0;
        double requiredGPA = 0.0;
        boolean inSystem = false;
        
        System.out.println('a' + 'c');
        while(!inSystem)
        {
            System.out.println("Enter the school name you wish to transfer to:");
            schoolNorm = input.nextLine();
            inSystem = isSchoolInDB(schoolNorm.toLowerCase());
        }
        school = schoolNorm.toLowerCase();
        
        System.out.println("Enter your major:");
        major = input.nextLine().toLowerCase();
        
        System.out.println("Enter your GPA:");
        gpa = input.nextDouble();        
        requiredGPA  = getRequiredGPA(schoolNorm, school, major);
        
        System.out.println(requiredGPA);
        // checkCanTransfer(schoolNorm, school, major, gpa);
    }
    
    public static boolean isSchoolInDB(String school)
    {
        if(!(school.equals("ucla") || school.equals("ucb") || school.equals("uci") || school.equals("ucsd")))
        {
            System.out.println(school + " is not in the system.");
            return false;
        }
        else
        {
            return true;
        }
    }
    
    public static double getRequiredGPA(String schoolNorm, String school, String major)
    {
        String fileName = "";
        fileName = school.toUpperCase() + ".txt";
        Scanner file = new Scanner(fileName);        
        double gpa = 0.0;
        
        while(file.hasNext())
        {
            if(file.next().equals(major))
            {
                gpa = Double.parseDouble(file.next());
                break;
            }
        }
        
        return gpa;
    }
}



Attached File  UCB.txt (32bytes)
Number of downloads: 7
Attached File  UCI.txt (32bytes)
Number of downloads: 7
Attached File  UCLA.txt (32bytes)
Number of downloads: 8
Attached File  UCSD.txt (32bytes)
Number of downloads: 7

This post has been edited by MattC231: 07 October 2018 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#13 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3604
  • View blog
  • Posts: 16,500
  • Joined: 20-September 08

Re: Code does not enter switch statement

Posted 07 October 2018 - 09:49 AM

That particular method is now correct. What's the problem still?

All you need for your db check is:

return (school.equals("ucla") || school.equals("ucb") || school.equals("uci") || school.equals("ucsd"));

This post has been edited by g00se: 07 October 2018 - 10:24 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#14 MattC231   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-April 17

Re: Code does not enter switch statement

Posted 11 October 2018 - 11:46 AM

The main issue is that I don't think the correct file is being opened--gpa is returned as 0.0 and System.out.println(school + " is not in the system."); is getting printed even when the school txt file exists.

This post has been edited by MattC231: 11 October 2018 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#15 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3604
  • View blog
  • Posts: 16,500
  • Joined: 20-September 08

Re: Code does not enter switch statement

Posted 11 October 2018 - 01:27 PM

Quote

Scanner file = new Scanner(fileName);


won't open a file. That will use the actual String as input

You need

Scanner file = new Scanner(new File(fileName));


Also watch the case of your input isn't different from the case in the file
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2