New Posts  All Forums:Forum Nav:

im stuck in java - Page 3

post #21 of 35
Not sure to be honest, I don't even remember which one is the numerator lol... isn't that the one on top? But if you have any more java related questions I can help, feel free to send me a PM or something.
GhostBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 (Lapped) @ 3825MHz EVGA 780i FTW 2xSLI - EVGA GTX260 (216) 4x2GB OCZ Reaper @ 5-5-5-18 2.1V 
Hard DriveOptical DriveOSMonitor
320GB Seagate RAID 1: 2TB WD Samsung 20X DVD+R Vista Ultimate x64, Ubuntu 8.10 x64 Acer P244W x2 
KeyboardPowerCaseMouse
IBM Model M Corsair 850HX CoolerMaster Stacker 810 (Modded) Razer Copperhead 
Mouse Pad
SteelSeries QcK 
  hide details  
Reply
GhostBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 (Lapped) @ 3825MHz EVGA 780i FTW 2xSLI - EVGA GTX260 (216) 4x2GB OCZ Reaper @ 5-5-5-18 2.1V 
Hard DriveOptical DriveOSMonitor
320GB Seagate RAID 1: 2TB WD Samsung 20X DVD+R Vista Ultimate x64, Ubuntu 8.10 x64 Acer P244W x2 
KeyboardPowerCaseMouse
IBM Model M Corsair 850HX CoolerMaster Stacker 810 (Modded) Razer Copperhead 
Mouse Pad
SteelSeries QcK 
  hide details  
Reply
post #22 of 35
Thread Starter 
god dammit im starting to hate this how do i get an input from the user i can use the scanner class right but how exactly do i do that
post #23 of 35
first import Scanner at the top above where you declare the class

Code:
import java.util.Scanner;
then create a scanner. I like to create a global scanner

Code:
Scanner scannerName;
next instantiate the scanner (probably in the constructor)

Code:
scannerName = new Scanner(System.in)
finally you'll ask the user for input. Then you assign a variable the value of the input like this:

Code:
System.out.print("Enter an Integer: ");
tempVariable = scannerName.nextInt();
that is only if the variable you're assigning is an int. For strings use next() or nextLine(). For doubles use nextDouble() and so on. If you need other variable types, refer to your API.

Here's what you'll have when your done.

Code:
package packageName

import java.util.Scanner

public Class ClassName
{
   Scanner scannerName;

   public ClassName()
   {
       scannerName = new Scanner(System.in);
    }

    public void getUserInput()
    {
       int tempVariable
       System.out.print("Enter an Integer: ");
       tempVariable = scannerName.nextInt();
    }
}
By the way, what IDE are you using? Some API's can help you with stuff like next() and nextInt() as you type, but you should still learn the syntax as you type.

Another way to get input from a user is the JOptionPane.showMessageDialog(). I like that better, but that uses GUI and I don't know if you want to start with that yet. Definitely learn Scanner first.
Edited by Auroq - 2/17/11 at 8:23am
Watchtower
(13 items)
 
Amy
(10 items)
 
 
CPUMotherboardGraphicsRAM
Q6600 Asus Maximus II HD4870 and 8800GT 4GB Corsair Dominator 
OSMonitorPowerCase
64bit Windows 7 business n, Fedora 14, and Arch Asus 24", LG 22" AOC 19", and Compaq FS7600 600W SilverStone SST-SS60EF RocketFish 
Mouse
Logitech MX Air and Logitech V200 
CPUMotherboardGraphicsHard Drive
Intel Core i7-2630QM ASUSTek G73Sw GeForce GTX 460M Patriot Pyro SE 
Hard DriveOptical DriveOSOS
Segate ST9750420AS ATA Slimtype DVD A DS8A5SH ATA Arch Linux x86_64 Windows 7 Home Premium 64-bit SP1 
MonitorAudio
Generic PnP Monitor Realtek High Definition Audio 
  hide details  
Reply
Watchtower
(13 items)
 
Amy
(10 items)
 
 
CPUMotherboardGraphicsRAM
Q6600 Asus Maximus II HD4870 and 8800GT 4GB Corsair Dominator 
OSMonitorPowerCase
64bit Windows 7 business n, Fedora 14, and Arch Asus 24", LG 22" AOC 19", and Compaq FS7600 600W SilverStone SST-SS60EF RocketFish 
Mouse
Logitech MX Air and Logitech V200 
CPUMotherboardGraphicsHard Drive
Intel Core i7-2630QM ASUSTek G73Sw GeForce GTX 460M Patriot Pyro SE 
Hard DriveOptical DriveOSOS
Segate ST9750420AS ATA Slimtype DVD A DS8A5SH ATA Arch Linux x86_64 Windows 7 Home Premium 64-bit SP1 
MonitorAudio
Generic PnP Monitor Realtek High Definition Audio 
  hide details  
Reply
post #24 of 35
Thread Starter 
in using netbeans and thanx that helps a bit i did it like this??? :S

Code:
public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);

        System.out.println("Enter the numerator of the first fraction (Top Part): ");
        String Top1String = scan.nextLine();
        int Top1 = Integer.parseInt(Top1String);
        
        System.out.println("Enter the denominator of the first fraction (Bottom Part): ");
        String Bottom1String = scan.nextLine();
        int Bottom1 = Integer.parseInt(Bottom1String);

        System.out.println("Enter the numerator of the second fraction (Top Part): ");
        String Top2String = scan.nextLine();
        int Top2 = Integer.parseInt(Top2String);

        System.out.println("Enter the denominator of the second fraction (Bottom Part): ");
        String Bottom2String = scan.nextLine();
        int Bottom2 = Integer.parseInt(Bottom2String);

        System.out.println(Top1);
        System.out.println(Bottom1);
        System.out.println(Top2);
        System.out.println(Bottom2);


    }
post #25 of 35
Thread Starter 
ok last error to work out its the last time lol
Code:
package lab02;
import java.util.Scanner;
/**
 *
 * @author Tyler
 */
public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);

        System.out.println("Enter the numerator of the first fraction (Top Part): ");
        String Top1String = scan.nextLine();
        int Top1 = Integer.parseInt(Top1String);
        
        
        System.out.println("Enter the denominator of the first fraction (Bottom Part): ");
        String Bottom1String = scan.nextLine();
        int Bottom1 = Integer.parseInt(Bottom1String);
        
        System.out.println("Enter the numerator of the second fraction (Top Part): ");
        String Top2String = scan.nextLine();
        int Top2 = Integer.parseInt(Top2String);
        

        System.out.println("Enter the denominator of the second fraction (Bottom Part): ");
        String Bottom2String = scan.nextLine();
        int Bottom2 = Integer.parseInt(Bottom2String);
        Reducing (Top1, Bottom1);
        Reducing (Top2, Bottom2);
        Adding(Top1, Bottom1, Top2, Bottom2);
    }
    public static void Adding(int Top1, int Bottom1, int Top2, int Bottom2){
        int TopFinal1 = Top1 * Bottom2;
        int TopFinal2 = Top2 * Bottom1;
        int TopFinal = TopFinal1 + TopFinal2;
        int BottomFinal = Bottom1 * Bottom2;
        Reducing(TopFinal, BottomFinal);
    }
    public static void Reducing(int Top, int Bottom){
    boolean looper = true;
    while (looper = true){
        int New1 = Top/Bottom;
        if (New1%2==0){
            int TopFinal = Top;
            int BottomFinal = Bottom;
            looper = false;
        }else{
            Top = Bottom;
            Bottom = New1;
            }
    }
    System.out.println(TopFinal + "/" + BottomFinal);
    }
}
its the bolded part
post #26 of 35
Quote:
Originally Posted by Onions View Post
man this is hard stuff lol idont understand the concept of methods and such care to help me out XD
it is hard. im in java RIGHT NOW. lol

Quote:
Originally Posted by Onions View Post
ok last error to work out its the last time lol
Code:
package lab02;
import java.util.Scanner;
/**
 *
 * @author Tyler
 */
public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);

        System.out.println("Enter the numerator of the first fraction (Top Part): ");
        String Top1String = scan.nextLine();
        int Top1 = Integer.parseInt(Top1String);
        
        
        System.out.println("Enter the denominator of the first fraction (Bottom Part): ");
        String Bottom1String = scan.nextLine();
        int Bottom1 = Integer.parseInt(Bottom1String);
        
        System.out.println("Enter the numerator of the second fraction (Top Part): ");
        String Top2String = scan.nextLine();
        int Top2 = Integer.parseInt(Top2String);
        

        System.out.println("Enter the denominator of the second fraction (Bottom Part): ");
        String Bottom2String = scan.nextLine();
        int Bottom2 = Integer.parseInt(Bottom2String);
        Reducing (Top1, Bottom1);
        Reducing (Top2, Bottom2);
        Adding(Top1, Bottom1, Top2, Bottom2);
    }
    public static void Adding(int Top1, int Bottom1, int Top2, int Bottom2){
        int TopFinal1 = Top1 * Bottom2;
        int TopFinal2 = Top2 * Bottom1;
        int TopFinal = TopFinal1 + TopFinal2;
        int BottomFinal = Bottom1 * Bottom2;
        Reducing(TopFinal, BottomFinal);
    }
    public static void Reducing(int Top, int Bottom){
    boolean looper = true;
    while (looper = true){
        int New1 = Top/Bottom;
        if (New1%2==0){
            int TopFinal = Top;
            int BottomFinal = Bottom;
            looper = false;
        }else{
            Top = Bottom;
            Bottom = New1;
            }
    }
    System.out.println(TopFinal + "/" + BottomFinal);
    }
}
its the bolded part
you shouldve tested each part before moving on, saves SO MUCH time going back and forth.
Blipper
(20 items)
 
First LEGIT Rig!
(17 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 2700K @4.5Ghz (1.32v) Asus Maximus V Extreme Asus Strix GTX 1080 OC Edition Kingston HyperX 8GB 1866 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8GB 1866 Samsung Evo 840 500GB Seagate Barracuda ST1000DM003 1TB 7200 RPM Western Digital Black 1TB 7200RPM 
Optical DriveCoolingOSMonitor
ASUS DRW-24B1ST Corsair H100i Windows 10 Professional Dell Gaming S2716DG 27" 
MonitorMonitorKeyboardPower
BenQ GW2760HS 27" BenQ GW2760HS 27" Ducky Shine II (Red LED,Cherry MX Blue) CORSAIR Professional Series Gold AX850  
CaseMouseAudioOther
Corsair 650D Logitech G502 Yamaha R-v703 w/ Yamaha Monitors Obbuto Revolution for Desk/Cockpit 
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q9400 P45 Neo-F (MS-7519) NVIDIA GeForce GTX 460  Value Ram 
RAMRAMHard DriveOptical Drive
Value Ram  Value Ram Western Digital Blue some DVDRW 
CoolingOSMonitorKeyboard
Corsair A70 Win 7 Ultimate 64bit Samsung SyncMaster T260 26" Steelseries 6Gv2 
PowerCaseMouseMouse Pad
Antec TruePower 550 Haf 912 Razer Lachesis Razer Goliathus 
Audio
Yamaha R-V703 w/ ATH-M50 
  hide details  
Reply
Blipper
(20 items)
 
First LEGIT Rig!
(17 items)
 
 
CPUMotherboardGraphicsRAM
Intel Core i7 2700K @4.5Ghz (1.32v) Asus Maximus V Extreme Asus Strix GTX 1080 OC Edition Kingston HyperX 8GB 1866 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8GB 1866 Samsung Evo 840 500GB Seagate Barracuda ST1000DM003 1TB 7200 RPM Western Digital Black 1TB 7200RPM 
Optical DriveCoolingOSMonitor
ASUS DRW-24B1ST Corsair H100i Windows 10 Professional Dell Gaming S2716DG 27" 
MonitorMonitorKeyboardPower
BenQ GW2760HS 27" BenQ GW2760HS 27" Ducky Shine II (Red LED,Cherry MX Blue) CORSAIR Professional Series Gold AX850  
CaseMouseAudioOther
Corsair 650D Logitech G502 Yamaha R-v703 w/ Yamaha Monitors Obbuto Revolution for Desk/Cockpit 
CPUMotherboardGraphicsRAM
Intel Core 2 Quad Q9400 P45 Neo-F (MS-7519) NVIDIA GeForce GTX 460  Value Ram 
RAMRAMHard DriveOptical Drive
Value Ram  Value Ram Western Digital Blue some DVDRW 
CoolingOSMonitorKeyboard
Corsair A70 Win 7 Ultimate 64bit Samsung SyncMaster T260 26" Steelseries 6Gv2 
PowerCaseMouseMouse Pad
Antec TruePower 550 Haf 912 Razer Lachesis Razer Goliathus 
Audio
Yamaha R-V703 w/ ATH-M50 
  hide details  
Reply
post #27 of 35
To make things easier you should separate each method into it's class file. So in the main all you got to do is instantiate the class.

Like

Quote:
Add sam = new Add();
    
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte P67-UD4-B3 GTX 580 GTX 580 
RAMHard DriveOSMonitor
Who cares? Intel SSD  Windows 7 Dell u3011 
MonitorPowerCaseMouse
Dell u3011 Seasonic x1200 800D G700 
AudioAudio
Xonar STX Beyerdynamic DT 990 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
i7 2600k Gigabyte P67-UD4-B3 GTX 580 GTX 580 
RAMHard DriveOSMonitor
Who cares? Intel SSD  Windows 7 Dell u3011 
MonitorPowerCaseMouse
Dell u3011 Seasonic x1200 800D G700 
AudioAudio
Xonar STX Beyerdynamic DT 990 
  hide details  
Reply
post #28 of 35
Just reading through it quickly, I only see one syntax error:

Code:
while (looper = true)
The "=" is an assignment operator, so with that statement you are actually assigning the value "true" to looper. In order to check if looper is true, use "=="

Code:
while (looper == true)
Since you've got the basic functionality now, we should talk about efficiency.
First:
Code:
System.out.println("Enter the numerator of the first fraction (Top Part): ");
        String Top1String = scan.nextLine();
        int Top1 = Integer.parseInt(Top1String);
This works, but scanner can already read in int's. You don't need String Top1String or the parse. I recommend:

Code:
System.out.println("Enter the numerator of the first fraction (Top Part): ");
int Top1 = scan.nextInt();
That will work on all of them, since you're only using int's.


Quote:
To make things easier you should separate each method into it's class file. So in the main all you got to do is instantiate the class.
I don't think they do enough to constitute their own class, but I do think everything deserves a method. If you were to do that tho, you would need to either make Top1 and the others global variables. Or make the methods return int's.
Do something like:

Code:
public int getTop1()
{
   System.out.println("Enter the numerator of the first fraction (Top Part): ");
   int Top1 = scan.nextInt();
   return Top1;
}
In this case I'd recommend just doing global variables though.

Once these all have their own method, the main method would only have to look something like this:
Code:
getTop1()
getBottom1()
getTop2()
getBottom2()
reducing(top1, bottom1);
reducing(top2, bottom2);
adding(top1, bottom1, top2, bottom2);
System.out.println(TopFinal + "/" + BottomFinal);
If you choose to do the method that uses return statements, It could even look as nice as:
Code:
reducing(top1, bottom1);
reducing(getTop2, getTottom2);
adding(getTop1, getBottom1, getTop2, getBottom2);
System.out.println(TopFinal + "/" + BottomFinal);
Finally, I don't know if you've worked with multiple classes yet, but I'd reccommend moving the getTop1 and similar methods to their own class. Then moving reducing and adding to their own class. Then you could just have the main method be in a class called runner that and have it just execute everything. Do that and you are now doing object oriented programming. Congratulations.

I know that's kind of a lot to take in. It took me 20 min just to write this post, so if you have any questions. Please feel free to ask. I'm very willing to help! Good Luck.
Watchtower
(13 items)
 
Amy
(10 items)
 
 
CPUMotherboardGraphicsRAM
Q6600 Asus Maximus II HD4870 and 8800GT 4GB Corsair Dominator 
OSMonitorPowerCase
64bit Windows 7 business n, Fedora 14, and Arch Asus 24", LG 22" AOC 19", and Compaq FS7600 600W SilverStone SST-SS60EF RocketFish 
Mouse
Logitech MX Air and Logitech V200 
CPUMotherboardGraphicsHard Drive
Intel Core i7-2630QM ASUSTek G73Sw GeForce GTX 460M Patriot Pyro SE 
Hard DriveOptical DriveOSOS
Segate ST9750420AS ATA Slimtype DVD A DS8A5SH ATA Arch Linux x86_64 Windows 7 Home Premium 64-bit SP1 
MonitorAudio
Generic PnP Monitor Realtek High Definition Audio 
  hide details  
Reply
Watchtower
(13 items)
 
Amy
(10 items)
 
 
CPUMotherboardGraphicsRAM
Q6600 Asus Maximus II HD4870 and 8800GT 4GB Corsair Dominator 
OSMonitorPowerCase
64bit Windows 7 business n, Fedora 14, and Arch Asus 24", LG 22" AOC 19", and Compaq FS7600 600W SilverStone SST-SS60EF RocketFish 
Mouse
Logitech MX Air and Logitech V200 
CPUMotherboardGraphicsHard Drive
Intel Core i7-2630QM ASUSTek G73Sw GeForce GTX 460M Patriot Pyro SE 
Hard DriveOptical DriveOSOS
Segate ST9750420AS ATA Slimtype DVD A DS8A5SH ATA Arch Linux x86_64 Windows 7 Home Premium 64-bit SP1 
MonitorAudio
Generic PnP Monitor Realtek High Definition Audio 
  hide details  
Reply
post #29 of 35
Thread Starter 
ok so i have yet ot work with class's i assume those are like objects that i will be calling am i correct? for now i would like to stick with the way i have it as thats what i am being tested on. The scanner for int i had trouble with but you cleared that up thnx a ton also the while condition iss fine. Its a condition so its nto assigning anything its testing if its true. it does work with one or two equal signs tho so no biggy ill change it cause you are correct it should have two.
post #30 of 35
Ya classes are objects, but definitely stick to whatever you're being graded on. That's definitely important. On the while condition, I know it will work either way, but on some things it does make a difference. I just think it's easier to distinguish between assignments and conditions. That way you don't get confused when you're working with 10 classes that all contain 10 methods plus constructors, getters and setters at 3 in the morning. @.@ I know we've all been there, and I'm sure most of us hate finishing at 3, but staying up 'til 4 to fix the one character that screws the problem over.

So, good job on your program and feel free to pm with others.
Watchtower
(13 items)
 
Amy
(10 items)
 
 
CPUMotherboardGraphicsRAM
Q6600 Asus Maximus II HD4870 and 8800GT 4GB Corsair Dominator 
OSMonitorPowerCase
64bit Windows 7 business n, Fedora 14, and Arch Asus 24", LG 22" AOC 19", and Compaq FS7600 600W SilverStone SST-SS60EF RocketFish 
Mouse
Logitech MX Air and Logitech V200 
CPUMotherboardGraphicsHard Drive
Intel Core i7-2630QM ASUSTek G73Sw GeForce GTX 460M Patriot Pyro SE 
Hard DriveOptical DriveOSOS
Segate ST9750420AS ATA Slimtype DVD A DS8A5SH ATA Arch Linux x86_64 Windows 7 Home Premium 64-bit SP1 
MonitorAudio
Generic PnP Monitor Realtek High Definition Audio 
  hide details  
Reply
Watchtower
(13 items)
 
Amy
(10 items)
 
 
CPUMotherboardGraphicsRAM
Q6600 Asus Maximus II HD4870 and 8800GT 4GB Corsair Dominator 
OSMonitorPowerCase
64bit Windows 7 business n, Fedora 14, and Arch Asus 24", LG 22" AOC 19", and Compaq FS7600 600W SilverStone SST-SS60EF RocketFish 
Mouse
Logitech MX Air and Logitech V200 
CPUMotherboardGraphicsHard Drive
Intel Core i7-2630QM ASUSTek G73Sw GeForce GTX 460M Patriot Pyro SE 
Hard DriveOptical DriveOSOS
Segate ST9750420AS ATA Slimtype DVD A DS8A5SH ATA Arch Linux x86_64 Windows 7 Home Premium 64-bit SP1 
MonitorAudio
Generic PnP Monitor Realtek High Definition Audio 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming