Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help Diagnosing Weird Problem (Java)
New Posts  All Forums:Forum Nav:

Help Diagnosing Weird Problem (Java)

post #1 of 8
Thread Starter 
Hi guys, so I have an interesting problem with Scanner. It seems that no matter what I do, it have to enter my input twice for scanner to read it, or Scanner only reads the second line of input. If I enter "aaaa", return, return, "aaaa", the Scanner will not read anything, because the second line is blank.

If you run this program, you should be able to see this issue by running it, waiting at the timer, then it will lead you to a a prompt. Enter anything you want, the menu isn't coded at all yet so it doesn't matter what you put in.

Thanks!
Code:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;

class Global {
        public static int stop = -1;
}

public class DataSort {
        
        public static void main(String[] args){

                Timer timer = new Timer();
                Direct swit = new Direct();
                Run mprog = new Run();
                Help hlp = new Help();

                String newline = System.getProperty("line.separator");

                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                Scanner console = new Scanner(System.in);

                System.out.println(newline);
                System.out.println("Welcome to Data Sort! This Program is designed to sort information about targets discoverd by UAV and place the data in a table." + newline);
                System.out.print("For help, press any key. To continue, please wait. ");
                timer.schedule(swit, 3000);
                
                try {
                        Global.stop = in.read();
                } 

                catch(IOException e) {
                        e.printStackTrace();
                }

                try {
                        in.close();
                }

                catch(IOException e) {
                        e.printStackTrace();
                }
        }
}

class Direct extends TimerTask {
        
        public void run() {

                Run mprog = new Run();
                Help hlp = new Help();
                
                if(Global.stop != -1){
                        System.out.println("Help");
                        hlp.run();
                }

                if(Global.stop == -1) {
                        System.out.println("Main");
                        mprog.run();
                }
        }
}


class Help {

        public static void run() {
                String newline = System.getProperty("line.separator");

                System.out.print(newline);
                System.out.println("Entering Help Mode!" + newline);

                System.out.println("Entered Help Class");
                //String help = console.nextLine();
        }
}

class Run {

        public static void run() {

                /*EnterAll eall = new EnterAll();
                EnterCoords ecoords = new EnterCoords();
                EnterRelation erelat = new EnterRelation();
                EnterColor ecolor = new EnterColor();
                EnterShape eshape = new EnterShape();
                Coordinates coords = new Coordinates();
                Relation relat = new Relation();
                Color color = new Color();
                Shape shape = new Shape();
                List list = new List();
                Save save = new Save();
                SaveAs saveas = new SaveAs();*/

                String newline = System.getProperty("line.separator");
                Scanner console = new Scanner(System.in);

                System.out.print(newline);
                System.out.println("Initializing Main Program." + newline);
                System.out.println("************************** MAIN MENU *************************" + newline);
                System.out.println("Enter Coords \t Enter Relat \t Enter Color \t Enter Shape"+newline);
                System.out.println("Coordinates \t Relation \t Color \t \t Shape" + newline);
                System.out.println("Help \t \t List \t \t Save \t \t Save As" + newline);
                System.out.println("**************************************************************" + newline);

                System.out.print("Enter your selection or type All to enter lines consecutively: ");
                
                String raw = console.nextLine();
                
                System.out.println(raw);

                String select = errorCheck(raw);

                if (select.equals("All")){
                }

                if (select.equals("Enter Coords")){
                }

                if (select.equals("Enter Relat")){
                }

                if (select.equals("Enter Color")){
                }

                if (select.equals("Enter Shape")){
                }

                if (select.equals("Coordinates")){
                }

                if (select.equals("Relation")){
                }

                if (select.equals("Color")){
                }

                if (select.equals("Shape")){
                }

                if (select.equals("Help")){
                }

                if (select.equals("List")){
                }

                if (select.equals("Save")){
                }

                if (select.equals("Save As")){
                }
        }

        private static String errorCheck(String raw) {
                
                String select = raw;
                return select;
        }
}
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
post #2 of 8
I'll give it a look when I get home shortly.
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
post #3 of 8
Well disregarding the fact your code is really messy (as in your entire program should not be in one file because it makes it very hard to read and trace). The error is coming at 28th line of code:
Code:
timer.schedule(swit, 3000);

to test it for yourself bypass the timer completely by commenting it out and typing:
Code:
Run.run();

below it and it will work.

I am not exactly sure why this is happening, but it is tied into the command:
Code:
timer.schedule(swit, 3000);

If you trace though your code that command will be at the root of all other class calls. What you have effectively done is create an pyramid with that one command being the top of pyramid and being responsible for running the entire program. I haven't used the timer class a lot, but from what I can see that can only cause problems as you are starting your program off because of a parameter call.

I would suggest creating separate class files for each class including a main class whose sole responsibility is to call all other classes and effectively run the entire program so it will be easier to read and it will also make bugs easier to trace. Also I should note doing it this way is standard practice and would be enforced if you were to get a job programming in a object oriented language like Java or C.
Edited by Bobicon - 3/31/13 at 12:22am
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
post #4 of 8
Thread Starter 
Ahh, thanks! Yea it was because the Scanner was hanging when you didn't enter input and just let it go to the Run class.

Also, fairly new to this, don't spend a long time explaining, but how would I go about, or what do you mean by using separate files for everything?

+Rep
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
post #5 of 8
Quote:
Originally Posted by snelan View Post

Ahh, thanks! Yea it was because the Scanner was hanging when you didn't enter input and just let it go to the Run class.

Also, fairly new to this, don't spend a long time explaining, but how would I go about, or what do you mean by using separate files for everything?

+Rep

In general every class should represent its own .java file

for example you should have files named:

Global.java
Direct.java
Help.java
Run.java

with each respected class in their own file all of which will be ran from Run.java.

I wouldn't worry about it all that much if you are just trying to get the hang of programming, but it is something you should do with all new projects going forward.
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
post #6 of 8
Thread Starter 
Ok - I actually was assuming that that is what you meant, and I have gotten it to work like that (but just wanted clarification that that was in fact what you meant). I do see how it could be much simpler, and am sure as I use it more and more it will become second nature.

Just a quick question, should the main method, where the program would normally start now be used to start the other classes/etc, or is it still in good practice to leave the main method in the first called class, unless I am going about this wrong.

Here is the program now if you have time to take a look: https://github.com/josmek/Dev-Cards-Repository/tree/master/DataSortClasses
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
post #7 of 8
Quote:
Originally Posted by snelan View Post

Ok - I actually was assuming that that is what you meant, and I have gotten it to work like that (but just wanted clarification that that was in fact what you meant). I do see how it could be much simpler, and am sure as I use it more and more it will become second nature.

Just a quick question, should the main method, where the program would normally start now be used to start the other classes/etc, or is it still in good practice to leave the main method in the first called class, unless I am going about this wrong.

Here is the program now if you have time to take a look: https://github.com/josmek/Dev-Cards-Repository/tree/master/DataSortClasses

I would suggest just merging your your DataSort and Main class (by making DataSort the main class) as your DataSort class is just a menu interface that calls all other classes. Other than that I would say it looks great.

Also here is a short read (about 10 pages) explaining object oriented programming.

http://sepwww.stanford.edu/data/media/public/sep/jon/family/jos/oop/oop1.htm
Edited by Bobicon - 4/1/13 at 10:06pm
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
Bob's Computer
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500K MSI P67A-G45 MSI 6970 Lightning 2GB G Skill Ripjaws X (2x4GB) 1866mhz 9-10-9-28 
Hard DriveOptical DriveCoolingOS
Corsair Force 3 SSD (60GB), X1 WD5000AAKS, X1 W... ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner Noctua NH-C14 Windows 7 64 bit 
MonitorKeyboardPowerCase
Asus VH238H Logitech G510 HALE90 750W NZXT Phantom Black 
MouseMouse PadAudioOther
Logitech G9x SteelSeries 4HD ASUS Xonar DGX / Audio-Technica ATH-AD700 Hauppauge! HVR-1250 TV Tuner 
  hide details  
Reply
post #8 of 8
Thread Starter 
Awesome! Will read, thanks!
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 3770k Gigabyte Z77-UD5H-WB Golden GTX 670 Windforce 3X 2 x 4GB GSkill RipJawsX 
Hard DriveCoolingOSMonitor
OCZ Vertex 4 64GB- WD Caviar Black 1TB Noctua NH-D14 Windows 7 Dell U2311H IPS 
KeyboardPowerCaseMouse
xArmor iOne U9BL MX Blues Seasonic x750 LIAN LI PC-T60B Logitech G500 
Audio
Modded JVC HA-RX900 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help Diagnosing Weird Problem (Java)