Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Programming Help
New Posts  All Forums:Forum Nav:

Java Programming Help - Page 2

post #11 of 26
Thread Starter 
Quote:
Originally Posted by surfbumb View Post
make a new scanner object right above...should fix it.
I have
Quote:
System.out.print("Please enter a character: ");
String interestedchar = in.nextLine();
inside case 1 before the method is called. Is that what you meant for me to do?


EDIT: NVM! I figured out what you meant. Thanks
Edited by wire - 11/3/11 at 11:20am
post #12 of 26
Fairly easy, mate. Just add one more parameter of type String to the countCharacter(dna), so it'd becomre countCharacter(dna, character).

Then simply call it with the String interestedchar like

countCharacter(dna, interestedchar);

My Rig
(14 items)
 
Ex-wife's Rig
(15 items)
 
 
CPUMotherboardGraphicsRAM
Core i5 4460 AsRock H81M-DG4 Sapphire Rx470 Platinum KVR 1600 16Gb 
Hard DriveHard DriveCoolingOS
2x Seagate 3Tb Samsung 850 EVO 120 Scythe Ninja 3 Rev.B Windows 10 Pro 
MonitorKeyboardPowerCase
Fujitsu Siemens A17-2A Logitech K280e SuperFlower SF-550K12XP Thermaltake Versa H25 
MouseAudio
Logitech G402 Sony MDR XD150 
CPUMotherboardGraphicsRAM
Athlon 750K 4.0Ghz AsRock FM2A75 Pro4+ Sapphire R9 270X Dual-X Kingston 2x4Gb 1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 120  Western Digital 320Gb LiteON DVD-RW CoolerMaster Hyper Z600 
OSMonitorKeyboardPower
Windows 7 Pro x64 Toshiba 32" FullHD TV Logitech FSP Hexa 550 
CaseMouse
DeLUX Logitech 
  hide details  
Reply
My Rig
(14 items)
 
Ex-wife's Rig
(15 items)
 
 
CPUMotherboardGraphicsRAM
Core i5 4460 AsRock H81M-DG4 Sapphire Rx470 Platinum KVR 1600 16Gb 
Hard DriveHard DriveCoolingOS
2x Seagate 3Tb Samsung 850 EVO 120 Scythe Ninja 3 Rev.B Windows 10 Pro 
MonitorKeyboardPowerCase
Fujitsu Siemens A17-2A Logitech K280e SuperFlower SF-550K12XP Thermaltake Versa H25 
MouseAudio
Logitech G402 Sony MDR XD150 
CPUMotherboardGraphicsRAM
Athlon 750K 4.0Ghz AsRock FM2A75 Pro4+ Sapphire R9 270X Dual-X Kingston 2x4Gb 1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 120  Western Digital 320Gb LiteON DVD-RW CoolerMaster Hyper Z600 
OSMonitorKeyboardPower
Windows 7 Pro x64 Toshiba 32" FullHD TV Logitech FSP Hexa 550 
CaseMouse
DeLUX Logitech 
  hide details  
Reply
post #13 of 26
Quote:
Originally Posted by wire View Post
I have inside case 1 before the method is called. Is that what you meant for me to do?

yeah, try it inside case 1...I think the original object is going out of scope somehow...did it work?
Edited by surfbumb - 11/3/11 at 11:24am
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #14 of 26
Just to make it a little easier to read for us..


Code:
import java.util.Scanner;

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

System.out.print("Please enter a DNA sequence ");
String dna = in.nextLine();

//Performs a check to test the validity of the inputed DNA sequence
int i = 0;
while (i < dna.length())
{
char ch = dna.charAt(i);

if (ch == 'a' || ch=='c' || ch=='g' || ch=='t')
{
i++;
}
else
{
System.out.print("Invalid DNA sequence. Please enter a DNA sequence ");
dna = in.nextLine();
}
}

//Prints out a menu
System.out.println("Select one of the following four options: \
"+
"1. Count the number of times a specific character occurs in the DNA\
"+
"2. Count the number of a pair of characters in the DNA sequence\
"+
"3. Create the RNA sequence from the DNA sequence input\
"+
"4. Create the complement of the DNA sequence");

int menu = in.nextInt();

//switch statement to handle menu choice
switch(menu)
{
case 1:
System.out.print("Please enter a character: ");
String interestedchar = in.nextLine();

int number = countCharacter(dna);
System.out.println("The character "+interestedchar+" appears "+number+" times in the dna sequence. ");
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}

/** countCharacter
* computes the number of a character that is in the DNA sequence
* @param String str
* @return count
*/
public static int countCharacter(String str)
{
int count = 0;
for(int i = 0; i<str.length(); i++)
{
char ch = str.charAt(i);
if(ch=='a'||ch=='g'||ch=='c'||ch=='t')
{
count++;
}
}
return count;
}
}
post #15 of 26
Thread Starter 
Okay so I have a new question. I can't quite come up with the logic for doing this next part of the program.

Lets say I have String dna = "acgtacgtacgtttaacgttt"

What I want to do is replace all "a" with "t", all "t" with "a", all "g" with "c", and all "c" with "g". I don't think I can use the replace method with this one.
post #16 of 26
Quote:
Originally Posted by wire View Post
Okay so I have a new question. I can't quite come up with the logic for doing this next part of the program.

Lets say I have String dna = "acgtacgtacgtttaacgttt"

What I want to do is replace all "a" with "t", all "t" with "a", all "g" with "c", and all "c" with "g". I don't think I can use the replace method with this one.
Break it up to an array of chars, iterate, put back together with "+".
My Rig
(14 items)
 
Ex-wife's Rig
(15 items)
 
 
CPUMotherboardGraphicsRAM
Core i5 4460 AsRock H81M-DG4 Sapphire Rx470 Platinum KVR 1600 16Gb 
Hard DriveHard DriveCoolingOS
2x Seagate 3Tb Samsung 850 EVO 120 Scythe Ninja 3 Rev.B Windows 10 Pro 
MonitorKeyboardPowerCase
Fujitsu Siemens A17-2A Logitech K280e SuperFlower SF-550K12XP Thermaltake Versa H25 
MouseAudio
Logitech G402 Sony MDR XD150 
CPUMotherboardGraphicsRAM
Athlon 750K 4.0Ghz AsRock FM2A75 Pro4+ Sapphire R9 270X Dual-X Kingston 2x4Gb 1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 120  Western Digital 320Gb LiteON DVD-RW CoolerMaster Hyper Z600 
OSMonitorKeyboardPower
Windows 7 Pro x64 Toshiba 32" FullHD TV Logitech FSP Hexa 550 
CaseMouse
DeLUX Logitech 
  hide details  
Reply
My Rig
(14 items)
 
Ex-wife's Rig
(15 items)
 
 
CPUMotherboardGraphicsRAM
Core i5 4460 AsRock H81M-DG4 Sapphire Rx470 Platinum KVR 1600 16Gb 
Hard DriveHard DriveCoolingOS
2x Seagate 3Tb Samsung 850 EVO 120 Scythe Ninja 3 Rev.B Windows 10 Pro 
MonitorKeyboardPowerCase
Fujitsu Siemens A17-2A Logitech K280e SuperFlower SF-550K12XP Thermaltake Versa H25 
MouseAudio
Logitech G402 Sony MDR XD150 
CPUMotherboardGraphicsRAM
Athlon 750K 4.0Ghz AsRock FM2A75 Pro4+ Sapphire R9 270X Dual-X Kingston 2x4Gb 1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 850 EVO 120  Western Digital 320Gb LiteON DVD-RW CoolerMaster Hyper Z600 
OSMonitorKeyboardPower
Windows 7 Pro x64 Toshiba 32" FullHD TV Logitech FSP Hexa 550 
CaseMouse
DeLUX Logitech 
  hide details  
Reply
post #17 of 26
Skylake
(10 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core-i7 6700K ASUS ROG Maximus VIII Hero EVGA GTX 970 4GB G.SKILL Ripjaws 4 32GB DDR4 2800 
Hard DriveHard DriveCoolingOS
SAMSUNG SM951 M.2 SAMSUNG 850 EVO Corsair H100i Windows 10 
PowerCase
SeaSonic G-750 Fractal Design Define R4 
  hide details  
Reply
Skylake
(10 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core-i7 6700K ASUS ROG Maximus VIII Hero EVGA GTX 970 4GB G.SKILL Ripjaws 4 32GB DDR4 2800 
Hard DriveHard DriveCoolingOS
SAMSUNG SM951 M.2 SAMSUNG 850 EVO Corsair H100i Windows 10 
PowerCase
SeaSonic G-750 Fractal Design Define R4 
  hide details  
Reply
post #18 of 26
Thread Starter 
Yeah I thought of using that, but if I first replace the a's with t's, then my new string will no longer have any a's. So when I now replace all t's with a's, I'm left with no t's.
post #19 of 26
Quote:
Originally Posted by wire View Post
Yeah I thought of using that, but if I first replace the a's with t's, then my new string will no longer have any a's. So when I now replace all t's with a's, I'm left with no t's.
You could do it by replacing it with an upper case T or something then replace lower case t's with upper case U's or whatever. Dunno what you're doing with these sequences.
Skylake
(10 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core-i7 6700K ASUS ROG Maximus VIII Hero EVGA GTX 970 4GB G.SKILL Ripjaws 4 32GB DDR4 2800 
Hard DriveHard DriveCoolingOS
SAMSUNG SM951 M.2 SAMSUNG 850 EVO Corsair H100i Windows 10 
PowerCase
SeaSonic G-750 Fractal Design Define R4 
  hide details  
Reply
Skylake
(10 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core-i7 6700K ASUS ROG Maximus VIII Hero EVGA GTX 970 4GB G.SKILL Ripjaws 4 32GB DDR4 2800 
Hard DriveHard DriveCoolingOS
SAMSUNG SM951 M.2 SAMSUNG 850 EVO Corsair H100i Windows 10 
PowerCase
SeaSonic G-750 Fractal Design Define R4 
  hide details  
Reply
post #20 of 26
Thread Starter 
Quote:
Originally Posted by K10 View Post
You could do it by replacing it with an upper case T or something then replace lower case t's with upper case U's or whatever. Dunno what you're doing with these sequences.
That's actually a very good idea lol. So in java, is T different from t?
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Programming Help