Advertisement
Hexbugman213

Java Note Scale Counting

Dec 8th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 KB | None | 0 0
  1. package music;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class musicc {
  6.     public static void main(String[] args) throws Exception {
  7.         int noteid;
  8.         int notechange;
  9.         boolean direction; //true = up, false = down
  10.         int newnoteid;
  11.         boolean debug = true; //PRINTS DEBUG INFO
  12.         //GETS USER INPUT!
  13.         noteid = musicc.getNoteId(); //gets noteid
  14.         if (debug == true) {
  15.             System.out.println("Note id: " + noteid); //debug
  16.         }
  17.         notechange = musicc.getNoteChange(); //gets note change
  18.         if (debug == true) {
  19.             System.out.println("Note change: " + notechange); //debug
  20.         }
  21.         direction = getDirection();
  22.         if (debug == true) {
  23.             System.out.println("Note direction: " + direction); //debug
  24.         }
  25.         //CALCULATIONS
  26.         System.out.println("Starting Calculations...");
  27.         notechange = notechange - 1;
  28.         if (direction == false) {
  29.             notechange = notechange * -1;
  30.         }
  31.         if (debug == true) {
  32.             System.out.println("Note change/direction togther: " + notechange); //debug
  33.         }
  34.         newnoteid = noteid + notechange;
  35.         if (debug == true) {
  36.             System.out.println("Raw newnoteid: " + newnoteid); //debug
  37.         }
  38.         newnoteid = newnoteid % 7;
  39.         if (debug == true) {
  40.             System.out.println("Note id pre NF: " + newnoteid); //NF = negative fix //debug
  41.         }
  42.         while (newnoteid < 0) {
  43.             newnoteid = newnoteid + 7; //brings value above negative by the octave
  44.         }
  45.         if (debug == true) {
  46.             System.out.println("Note id post NF: " + newnoteid); //debug
  47.         }
  48.         String result = getNewNote(newnoteid);
  49.         if (debug == true) {
  50.             System.out.println("result: " + result); //debug
  51.         }
  52.         //RESULTS
  53.         JOptionPane.showMessageDialog(null,"The note you landed on is " + result,"Results", JOptionPane.INFORMATION_MESSAGE);
  54.     }
  55.  
  56.     public static int getNoteId() {
  57.         String note;
  58.         //asks for note
  59.         Object[] possibilities = {"A", "B", "C", "D", "E", "F", "G"};
  60.         note = (String)JOptionPane.showInputDialog(null,"What note do you want to start on?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,possibilities,"A");
  61.         while (note == null) { //makes sure if cancel is pressed it tries again
  62.             JOptionPane.showMessageDialog(null,"Enter a note!", "Error!", JOptionPane.ERROR_MESSAGE);
  63.             note = (String)JOptionPane.showInputDialog(null,"What note do you want to start on?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,possibilities,"A");
  64.         }
  65.         switch(note) { //note to number
  66.         case "A":return 1;
  67.         case "B":return 2;
  68.         case "C":return 3;
  69.         case "D":return 4;
  70.         case "E":return 5;
  71.         case "F":return 6;
  72.         case "G":return 7;
  73.         }
  74.         return 0;
  75.     }
  76.     public static int getNoteChange() {
  77.         String change;
  78.         int changeInt = 0; //declare int
  79.         boolean ischangeint = false;
  80.         change = (String)JOptionPane.showInputDialog(null,"How much do you want to go up or down?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,null,"1"); //inital input
  81.         while (ischangeint == false) {
  82.             try {
  83.                 changeInt = Integer.parseInt(change); //converts string answer to integer
  84.                 //goes to catch if it's not
  85.                 if (changeInt >= 1) { //checks if its greater than or equal to 1
  86.                     ischangeint = true; //if all conditions satisfied, change ischangeint to true, breaking the while loop
  87.                 } else { //less than 1
  88.                     JOptionPane.showMessageDialog(null,"That is not a positive number!", "Error!", JOptionPane.ERROR_MESSAGE); //error
  89.                     change = (String)JOptionPane.showInputDialog(null,"How much do you want to go up or down?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,null,"1"); //asks again
  90.                 }
  91.             } catch (NumberFormatException e) { //goes to this instead of error if not integer
  92.                 JOptionPane.showMessageDialog(null,"That is not a valid number!", "Error!", JOptionPane.ERROR_MESSAGE); //error
  93.                 change = (String)JOptionPane.showInputDialog(null,"How much do you want to go up or down?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,null,"1"); //asks again
  94.             }
  95.            
  96.         }
  97.         return changeInt; //after checking if it is a valid int, it returns it
  98.     }
  99.     public static boolean getDirection() {
  100.         String directionS;
  101.         Object[] possibilities = {"Up","Down"};
  102.         directionS = (String)JOptionPane.showInputDialog(null,"Which direction do you want to count in?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,possibilities,"Up");
  103.         while (directionS == null) { //makes sure if cancel is pressed it tries again
  104.             JOptionPane.showMessageDialog(null,"Enter a direction!", "Error!", JOptionPane.ERROR_MESSAGE);
  105.             directionS = (String)JOptionPane.showInputDialog(null,"Which direction do you want to count in?","Music Scale",JOptionPane.PLAIN_MESSAGE,null,possibilities,"Up");
  106.         }
  107.         if (directionS == "Up") {
  108.             return true;
  109.         } else {
  110.             return false;
  111.         }
  112.        
  113.     }
  114.     public static String getNewNote(int id) {
  115.         switch(id) { //note to number
  116.         case 1:return "A";
  117.         case 2:return "B";
  118.         case 3:return "C";
  119.         case 4:return "D";
  120.         case 5:return "E";
  121.         case 6:return "F";
  122.         case 0:return "G";
  123.         }
  124.         return "error";
  125.     }
  126. }
  127. //Created by Hexbugman213
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement