Java Scanner运行时错误

时间:2016-06-02 07:57:38

标签: java

我对编程非常陌生,并决定创建一个程序,将ascii字符串转换为二进制字符串,将二进制字符串转换为ascii字符串。我遇到的问题是,当程序成功编译时,它不允许我在选择是否转换为二进制文件之后输入要翻译的字符串。

import java.util.Scanner;

public class Translator
{  
    public static void main(String[] args)
    {
        int valueoftype; 
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 1 if converting to binary. Enter 0 if converting from binary.");
        valueoftype = in.nextInt();
        System.out.println("What is your input?");
        String input = in.nextLine();
        if(valueoftype == 1)
        { 

            byte[] bytes = input.getBytes();
            StringBuilder binary = new StringBuilder();
            for (byte b : bytes)
            {
                int val = b;
                for (int i = 0; i < 8; i++)
                {
                    binary.append((val & 128) == 0 ? 0 : 1);
                    val <<= 1;
                }
                binary.append(' ');
        }
            System.out.println(input + " in binary is " + binary);
        }
        else if(valueoftype == 0)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 0;i < input.length();i += 8) 
            {
                result.append((char) Integer.parseInt(input.substring(i, i + 8), 2));
            }
            System.out.println(input + "in ascii is"+ result); 
        } 
        else
        {
            System.out.println("Error cannot procede with incorrect value.");
            System.out.println("Enter 1 if converting to binary. Enter 0 if converting from binary.");
            valueoftype = in.nextInt();
    }
  }
}

过去几个小时我一直在试图弄清楚我的代码究竟出现了什么问题,希望我能得到某人的解释。

3 个答案:

答案 0 :(得分:0)

不使用String input = in.nextLine();而只使用String input = in.next();,您可以输入所需的字符串。

请参阅this了解差异

答案 1 :(得分:0)

你可以添加

<Entry id="AA0003">
<Header>
<Code>AA0003</Code>
<Dates>
<CreationDate>31-Mar-1995</CreationDate>
<StrucRevDate>31-Mar-1995</StrucRevDate>
<TextChngDate>30-Sep-2010</TextChngDate>
</Dates>
</Header>
<Names>
<Name>L-asparagine</Name>
<AlternateName>2,4-bis(azanyl)-4-oxobutanoic acid</AlternateName>
<AlternateName>2,4-diamino-4-oxobutanoic acid</AlternateName>
<AlternateName>2-amino-3-carbamoylpropanoic acid</AlternateName>
<AlternateName>2-amino-4-butanediamic acid</AlternateName>
<AlternateName>2-aminosuccinamic acid</AlternateName>
<AlternateName>2-aminosuccinic acid 4-amide</AlternateName>
<AlternateName>alpha-amino-beta-carbamylpropionic acid</AlternateName>
<AlternateName>alpha-aminosuccinamic acid</AlternateName>
<AlternateName>aspartic acid 4-amide</AlternateName>
<AlternateName>aspartic acid beta-amide</AlternateName>
<AlternateName>beta-asparagine</AlternateName>
<SystematicName>(2S)-2-amino-4-butanediamic acid</SystematicName>
<Xref>CAS:70-47-3</Xref>
<Xref>ChEBI:50347</Xref>
<Xref>PDBHET:ASN</Xref>
</Names>
<FormulaBlock>
<Formula>C 4 H 6 N 2 O 2</Formula>
<Weight type="chemical">114.10</Weight>
<Weight type="physical">114.042927</Weight>
</FormulaBlock>
<CorrectionBlock uids="AA0003" label="ASN">
<Formula>C 0 H 0 N 0 O 0</Formula>
<Weight type="chemical">0.00</Weight>
<Weight type="physical">0.000000</Weight>
</CorrectionBlock>
<CorrectionBlock uids="AA0004" label="ASP">
<Formula>C 0 H 1 N 1 O -1</Formula>
<Weight type="chemical">-0.98</Weight>
<Weight type="physical">-0.984016</Weight>
</CorrectionBlock>
<ReferenceBlock>
<Authors>
<Author>Vauquelin, L.N.</Author>
<Author>Robiquet, P.J.</Author>
</Authors>
<Citation>Ann. Chim. 57, 88-93, 1806</Citation>
<Title>
Découverte d'un nouveau principe végétal dans les Asperges (Asparagus sativus, Linn.) [The discovery of a new plant principle in asparagus (Asparagus sativus, L.)].
</Title>
<Note>article in French; isolation and naming</Note>
</ReferenceBlock>
<ReferenceBlock>
<Authors>
<Author>Drenth, J.</Author>
<Author>Jansonius, J.N.</Author>
<Author>Koekoek, R.</Author>
<Author>Swen, H.M.</Author>
<Author>Wolthers, B.G.</Author>
</Authors>
<Citation>Nature 218, 929-932, 1968</Citation>
<Title>Structure of papain.</Title>
<Xref>DOI:10.1038/218929a0</Xref>
<Xref>PMID:5681232</Xref>
<Note>
asparagine as an active site residue in a thiol proteinase
</Note>
</ReferenceBlock>
<ReferenceBlock>
<Authors>
<Anonymous/>
<Group>
IUPAC-IUB Joint Commission on Biochemical Nomenclature (JCBN)
</Group>
</Authors>
<Citation>Eur. J. Biochem. 138, 9-37, 1984</Citation>
<Title>
Nomenclature and symbolism for amino acids and peptides. Recommendations 1983.
</Title>
<Xref>DOI:10.1111/j.1432-1033.1984.tb07877.x</Xref>
<Xref>PMID:6692818</Xref>
<Note>
standard three-letter and one-letter symbols, and nomenclature
</Note>
</ReferenceBlock>
<ReferenceBlock>
<Authors>
<Author>Curnow, A.W.</Author>
<Author>Tumbula, D.L.</Author>
<Author>Pelaschier, J.T.</Author>
<Author>Min, B.</Author>
<Author>Soll, D.</Author>
</Authors>
<Citation>
Proc. Natl. Acad. Sci. U.S.A. 95, 12838-12843, 1998
</Citation>
<Title>
Glutamyl-tRNA(Gln) amidotransferase in Deinococcus radiodurans may be confined to asparagine biosynthesis.
</Title>
<Xref>DOI:10.1073/pnas.95.22.12838</Xref>
<Xref>PMID:9789001</Xref>
<Note>
"mischarged" aspartyl-tRNA(Asn) is amidated by glutamyl-tRNA(Gln) amidotransferase in Deinococcus radiodurans
</Note>
</ReferenceBlock>
<ReferenceBlock>
<Authors>
<Author>Paradisi, F.</Author>
<Author>Dean, J.L.</Author>
<Author>Geoghegan, K.F.</Author>
<Author>Engel, P.C.</Author>
</Authors>
<Citation>Biochemistry 44, 3636-3643, 2005</Citation>
<Title>
Spontaneous chemical reversion of an active site mutation: deamidation of an asparagine residue replacing the catalytic aspartic acid of glutamate dehydrogenase.
</Title>
<Xref>DOI:10.1021/bi047679u</Xref>
<Xref>PMID:15736973</Xref>
<Note>
an Asp to Asn mutation at an enzyme active site seemingly has partial activity; however, the asparagine form is shown to be inactive, and enhanced spontaneous deamidation results in the partial recovery of activity
</Note>
</ReferenceBlock>
<ReferenceBlock>
<Authors>
<Author>Kolodkin-Gal, I.</Author>
<Author>Hazan, R.</Author>
<Author>Gaathon, A.</Author>
<Author>Carmeli, S.</Author>
<Author>Engelberg-Kulka, H.</Author>
</Authors>
<Citation>Science 318, 652-655, 2007</Citation>
<Title>
A linear pentapeptide is a quorum-sensing factor required for mazEF-mediated cell death in Escherichia coli.
</Title>
<Xref>DOI:10.1126/science.1147248</Xref>
<Xref>PMID:17962566</Xref>
<Note>
enzymatic amidation of an aspartic acid residue in a peptide; the enzyme is currently identified by an activity that is different from the new activity
</Note>
</ReferenceBlock>
<Comment>
Some bacteria or archaea incorporate asparagine produced by the amidation of aspartyl-tRNA(Asn).
</Comment>
<GeneratingEnzyme>
<EnzymeName link="ASN">aspartate--tRNA(Asn) ligase (EC 6.1.1.23)</EnzymeName>
<EnzymeName link="ASN">
asparaginyl-tRNA synthase (glutamine-hydrolysing) (EC 6.3.5.6)
</EnzymeName>
<EnzymeName link="ASP">aspartate--ammonia ligase (EC 6.3.1.1)</EnzymeName>
</GeneratingEnzyme>
<SequenceCode link="ASN">
<SequenceSpec>N</SequenceSpec>
<Abbreviation>Asn</Abbreviation>
<Xref>PSI-MOD:00012</Xref>
</SequenceCode>
<SequenceCode link="ASP">
<SequenceSpec>D</SequenceSpec>
<Xref>PSI-MOD:01185</Xref>
</SequenceCode>
<Source>natural</Source>
<Features>
<Feature type="UniProt" key="act_site____n" link="ASN">ACT_SITE</Feature>
<Feature type="UniProt" key="mod_res__amidated aspartic acid__d" link="ASP">MOD_RES Amidated aspartic acid</Feature>
</Features>
<Image src="/images/AA0003.GIF" alt="DUMMY.GIF"/>
<Model src="/models/AA0003.PDB"/>
</Entry>

答案 2 :(得分:0)

问题在于nextInt()。请尝试以下代码:

    System.out.println("What is your input?");
    String input = in.next(); // Don't skip rest of the line
    if (valueoftype == 1) {

您还可以阅读这篇文章了解更多信息: Using scanner.nextLine()