Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [JAVA] Multiple Scanners in Java AND Mutliple strings/Int in one println?
New Posts  All Forums:Forum Nav:

[JAVA] Multiple Scanners in Java AND Mutliple strings/Int in one println?

post #1 of 18
Thread Starter 
Hey guys,

I know I've been asking a lot about Java here lately and that it is rather easy..
But how does one simply get two scanners working? I.E

I have two files that will work individually and I paste the body (the println for input, the scanner and the output) into another file and it will only ask the for the first input..?
I'm obviously missing something, - but what?

UPDATE: New problem has appeared.
Basically I must make another program.. I'm now right at the end and the new problem is that it will not allow me to use a println with more then two int/strings.
Code:
System.out.println(a + b + "is" + c  "on" + month + day "of the year" + d);

That isn't my code, but fairly similar. It just refuses to let me create that unless there is no c or month variables then it compiles fine just with them missing.

TIA
Edited by Matt-Matt - 4/7/13 at 4:19am
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #2 of 18
Not sure I follow what you are specifically trying to do, but Scanner is no different than anything else. Create the first scanner, call it sc1, and give it a file. Create the second scanner, call it sc2, and give it a file. Then you can read both files by using sc1 and sc2.
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
post #3 of 18
Thread Starter 
Quote:
Originally Posted by frozne View Post

Not sure I follow what you are specifically trying to do, but Scanner is no different than anything else. Create the first scanner, call it sc1, and give it a file. Create the second scanner, call it sc2, and give it a file. Then you can read both files by using sc1 and sc2.

Yes, but for some reason it just ignores the rest of the code...?
I'll paste it here, I am really stuck on why it will not work at all.
Code:
/* Int, Double, Boolean assignment by Matthew Wilson 2013™ */
 import java.util.Scanner;
        
 public class Int
 {
        public static void main(String[] args)
                
                        /* Basic Integer scanner and display */
                        { int num1;
                        System.out.println("Enter an Integer, then press enter.");
                        Scanner scan = new Scanner(System.in);
                        num1 = scan.nextInt();
                        System.out.println("The Integer that you entered was:" + num1);
                        }
                                
                        
                        {
                        /* Basic Double scanner and display */
                        double num2;
                        System.out.println("Enter an Double, then press enter.");
                        Scanner scan = new Scanner(System.in);
                        num2 = scan.nextDouble();
                        System.out.println("The Double that you entered was:" + num2);
                        }

 }

The idea is to show that I can get a scanner working to read an Int, Double and Boolean. I have all of those made but I can't get them all working in the same file for some reason. rolleyes.gif
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #4 of 18
That code works for me. What is happening when you run it?
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
Sab Tower
(12 items)
 
  
CPUMotherboardGraphicsRAM
i7 3770k ASRock Z77 OC Formula HIS IceQ Radeon 7950 Corsair Dominator Platinum 
Hard DriveHard DriveCoolingOS
Samsung Samsung 830 XSPC Raystorm EX360 OpenSUSE 12.2 
MonitorPowerCaseMouse
Acer 21 Inch Flat Screen Seasonic x650 NZXT Switch 810 Logitech MX510 
  hide details  
Reply
post #5 of 18
Why do you need two scanners, if you want to take two inputs you can use a single scanner, even for different data types...
Code:
public class test
{
    public static void main (String args[])
    {
        Scanner input = new Scanner(System.in);
        double num1;
        int num2;
        
        System.out.println("Enter a double");
        num1 = input.nextDouble();

        System.out.println("Enter an integer");
        num2 = input.nextInt();

        //Rest of your code goes here
    }
}
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II MSI GTX560 Ti TwinFrozr II 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 OCZ Vertex 3 Western Digital Caviar Black Western Digital Caviar Green 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro Lite-On 24x DVD-RW CoolerMaster V8 Windows 8.1 Professional 
OSMonitorMonitorMonitor
Debian 7.1 Samsung S22B350H Samsung S22B350H Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II MSI GTX560 Ti TwinFrozr II 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 OCZ Vertex 3 Western Digital Caviar Black Western Digital Caviar Green 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro Lite-On 24x DVD-RW CoolerMaster V8 Windows 8.1 Professional 
OSMonitorMonitorMonitor
Debian 7.1 Samsung S22B350H Samsung S22B350H Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
post #6 of 18
Thread Starter 
Quote:
Originally Posted by frozne View Post

That code works for me. What is happening when you run it?

It works.. It only records the first data type, in this case an Int.
Quote:
Originally Posted by dushan24 View Post

Why do you need two scanners, if you want to take two inputs you can use a single scanner, even for different data types...
Code:
public class test
{
    public static void main (String args[])
    {
        Scanner input = new Scanner(System.in);
        double num1;
        int num2;
        
        System.out.println("Enter a double");
        num1 = input.nextDouble();

        System.out.println("Enter an integer");
        num2 = input.nextInt();

        //Rest of your code goes here
    }
}

Cheers, will look at this later. (Y)
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #7 of 18
The reason why your first version did work is because you wrote your main function incorrectly.

This is how you think you wrote it:
Code:
/* Int, Double, Boolean assignment by Matthew Wilson 2013™ */
 import java.util.Scanner;
        
 public class Int
 {
        public static void main(String[] args)
                
                        /* Basic Integer scanner and display */
                        { int num1;
                        System.out.println("Enter an Integer, then press enter.");
                        Scanner scan = new Scanner(System.in);
                        num1 = scan.nextInt();
                        System.out.println("The Integer that you entered was:" + num1);

                        /* Basic Double scanner and display */
                        double num2;
                        System.out.println("Enter an Double, then press enter.");
                        Scanner scan = new Scanner(System.in);
                        num2 = scan.nextDouble();
                        System.out.println("The Double that you entered was:" + num2);
                        }

 }

However, you ended you main function just after:
Code:
System.out.println("The Integer that you entered was:" + num1);
which is why it only worked for the first number, and i'm surprised the java compiler/interpreter didn't complain at you.


Also, you don't need 2 scanner objects. You only need one since you're not doing any threading/parallelism stuff.
Router
(12 items)
 
  
Reply
Router
(12 items)
 
  
Reply
post #8 of 18
Forgot this in my previous code example
Code:
import java.util.Scanner;

Put it at the top and my code will work :-)
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II MSI GTX560 Ti TwinFrozr II 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 OCZ Vertex 3 Western Digital Caviar Black Western Digital Caviar Green 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro Lite-On 24x DVD-RW CoolerMaster V8 Windows 8.1 Professional 
OSMonitorMonitorMonitor
Debian 7.1 Samsung S22B350H Samsung S22B350H Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
Intel Core i7 860 Asus P7P55D-E Pro MSI GTX560 Ti TwinFrozr II MSI GTX560 Ti TwinFrozr II 
RAMHard DriveHard DriveHard Drive
Corsair 8GB DDR3 OCZ Vertex 3 Western Digital Caviar Black Western Digital Caviar Green 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro Lite-On 24x DVD-RW CoolerMaster V8 Windows 8.1 Professional 
OSMonitorMonitorMonitor
Debian 7.1 Samsung S22B350H Samsung S22B350H Samsung S22B350H 
KeyboardPowerCaseMouse
Ducky Shine II Corsair HX850 CoolerMaster Storm Enforcer Logitech M500 
Mouse PadAudio
Razer Goliathus Microsoft LifeChat LX 3000 
  hide details  
Reply
post #9 of 18
Thread Starter 
Just did a quick 20 second fix! Thanks guys! smile.gif

** I ended up just using a single scanner **
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
 
Seven Seven Five
(12 items)
 
 
CPUMotherboardGraphicsRAM
Convertible i5 3570k (De-Lidded) Gigabyte Z77x-D3H XFX R9 290 With Koolance Block (1179/1450) 16GB G-Skill Ares 1866 
Hard DriveHard DriveHard DriveHard Drive
Samsung 840 Evo Seagate Barracuda 2TB Western Digital Green 2TB Samsung Spinpoint 1TB 
Hard DriveOptical DriveCoolingCooling
Western Digital Green 640GB 2x OEM DVD Drives Heatkiller Rev 3.0 XSPC RX360 
CoolingCoolingCoolingOS
Laing D5 + XSPC Res Primochill Bloodshed Red Tubing Koolance Fullcover R9 290x Block Windows 10 Pro x64 
MonitorMonitorMonitorKeyboard
Asus VEH228H Asus VE228H Aavara TS742 Dual Monitor Stand Ducky Shine II Cherry Blue RED Backlit Mechanic... 
PowerCaseMouseMouse Pad
Silverstone Strider 850W HAF 932 Logitech G400 Gigabyte Ghost 
AudioOther
Microlab Solo6c Logitech C100 Webcam 
  hide details  
Reply
post #10 of 18
you can create two different scanner objects

Scanner scan1 = new Scanner;
Scanner scan2 = new Scanner;

EDIT: ah okay glad you got it sorted thumb.gif
    
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Gigabyte GA-Z68XP-UD4 Gigabyte GTX 970 G1 Gaming G-Skill Ripjaws 1600MHz 8gb (2x4gb)  
Hard DriveHard DriveHard DriveHard Drive
Sandisk Extreme 240GB WD Green 500GB WD Blue 500GB Seagate 500GB 
Optical DriveCoolingOSMonitor
Samsung DVD Noctua NH-D14 Windows 7 Home Premium x64 Crossover 27Q LED-P 
MonitorKeyboardPowerCase
ASUS VS248 Ducky Shine Zero D2108S (Blue-LED) (Cherry MX B... Corsair TX-750 Silverstone Raven RV01 
MouseMouse PadAudio
Logitech G9x Roccat Taito Audio Technica ATH-M50 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Gigabyte GA-Z68XP-UD4 Gigabyte GTX 970 G1 Gaming G-Skill Ripjaws 1600MHz 8gb (2x4gb)  
Hard DriveHard DriveHard DriveHard Drive
Sandisk Extreme 240GB WD Green 500GB WD Blue 500GB Seagate 500GB 
Optical DriveCoolingOSMonitor
Samsung DVD Noctua NH-D14 Windows 7 Home Premium x64 Crossover 27Q LED-P 
MonitorKeyboardPowerCase
ASUS VS248 Ducky Shine Zero D2108S (Blue-LED) (Cherry MX B... Corsair TX-750 Silverstone Raven RV01 
MouseMouse PadAudio
Logitech G9x Roccat Taito Audio Technica ATH-M50 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [JAVA] Multiple Scanners in Java AND Mutliple strings/Int in one println?